From 50f3d320f46bb4f342b0091b89936175d9509d70 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Wed, 10 Feb 2021 19:59:37 -0800 Subject: [PATCH] Add terser Y syntax --- ueforth/arduino/arduino.template.ino | 54 ++++++++++++++-------------- ueforth/common/calling.h | 22 ++++++------ ueforth/common/opcodes.h | 49 +++++++++++++------------ ueforth/posix/posix_main.c | 2 +- ueforth/windows/windows_main.c | 4 +-- 5 files changed, 65 insertions(+), 66 deletions(-) diff --git a/ueforth/arduino/arduino.template.ino b/ueforth/arduino/arduino.template.ino index 8e058db..2e14653 100644 --- a/ueforth/arduino/arduino.template.ino +++ b/ueforth/arduino/arduino.template.ino @@ -43,12 +43,12 @@ #define PLATFORM_OPCODE_LIST \ /* Memory Allocation */ \ - X("MALLOC", MALLOC, tos = (cell_t) malloc(tos)) \ - X("SYSFREE", FREE, free((void *) tos); DROP) \ - X("REALLOC", REALLOC, tos = (cell_t) realloc((void *) *sp, tos); --sp) \ - X("heap_caps_malloc", HEAP_CAPS_MALLOC, tos = (cell_t) heap_caps_malloc(*sp, tos); --sp) \ - X("heap_caps_free", HEAP_CAPS_FREE, heap_caps_free((void *) tos); DROP) \ - X("heap_caps_realloc", HEAP_CAPS_REALLOC, \ + Y(MALLOC, tos = (cell_t) malloc(tos)) \ + Y(SYSFREE, free((void *) tos); DROP) \ + Y(REALLOC, tos = (cell_t) realloc((void *) *sp, tos); --sp) \ + Y(heap_caps_malloc, tos = (cell_t) heap_caps_malloc(*sp, tos); --sp) \ + Y(heap_caps_free, heap_caps_free((void *) tos); DROP) \ + Y(heap_caps_realloc, \ tos = (cell_t) heap_caps_realloc((void *) sp[-1], *sp, tos); sp -= 2) \ /* Serial */ \ X("Serial.begin", SERIAL_BEGIN, Serial.begin(tos); DROP) \ @@ -58,29 +58,29 @@ X("Serial.write", SERIAL_WRITE, tos = Serial.write((const uint8_t *) *sp, tos); --sp) \ X("Serial.flush", SERIAL_FLUSH, Serial.flush()) \ /* Pins and PWM */ \ - X("pinMode", PIN_MODE, pinMode(*sp, tos); --sp; DROP) \ - X("digitalWrite", DIGITAL_WRITE, digitalWrite(*sp, tos); --sp; DROP) \ - X("digitalRead", DIGITAL_READ, tos = (cell_t) digitalRead(tos)) \ - X("analogRead", ANALOG_READ, tos = (cell_t) analogRead(tos)) \ - X("ledcSetup", LEDC_SETUP, \ + Y(pinMode, pinMode(*sp, tos); --sp; DROP) \ + Y(digitalWrite, digitalWrite(*sp, tos); --sp; DROP) \ + Y(digitalRead, tos = (cell_t) digitalRead(tos)) \ + Y(analogRead, tos = (cell_t) analogRead(tos)) \ + Y(ledcSetup, \ tos = (cell_t) (1000000 * ledcSetup(sp[-1], *sp / 1000.0, tos)); sp -= 2) \ - X("ledcAttachPin", ATTACH_PIN, ledcAttachPin(*sp, tos); --sp; DROP) \ - X("ledcDetachPin", DETACH_PIN, ledcDetachPin(tos); DROP) \ - X("ledcRead", LEDC_READ, tos = (cell_t) ledcRead(tos)) \ - X("ledcReadFreq", LEDC_READ_FREQ, tos = (cell_t) (1000000 * ledcReadFreq(tos))) \ - X("ledcWrite", LEDC_WRITE, ledcWrite(*sp, tos); --sp; DROP) \ - X("ledcWriteTone", LEDC_WRITE_TONE, \ + Y(ledcAttachPin, ledcAttachPin(*sp, tos); --sp; DROP) \ + Y(ledcDetachPin, ledcDetachPin(tos); DROP) \ + Y(ledcRead, tos = (cell_t) ledcRead(tos)) \ + Y(ledcReadFreq, tos = (cell_t) (1000000 * ledcReadFreq(tos))) \ + Y(ledcWrite, ledcWrite(*sp, tos); --sp; DROP) \ + Y(ledcWriteTone, \ tos = (cell_t) (1000000 * ledcWriteTone(*sp, tos / 1000.0)); --sp) \ - X("ledcWriteNote", LEDC_WRITE_NOTE, \ + Y(ledcWriteNote, \ tos = (cell_t) (1000000 * ledcWriteNote(sp[-1], (note_t) *sp, tos)); sp -=2) \ /* General System */ \ - X("MS", MS, delay(tos); DROP) \ - X("TERMINATE", TERMINATE, exit(tos)) \ + Y(MS, delay(tos); DROP) \ + Y(TERMINATE, exit(tos)) \ /* File words */ \ X("R/O", R_O, PUSH(O_RDONLY)) \ X("R/W", R_W, PUSH(O_RDWR)) \ X("W/O", W_O, PUSH(O_WRONLY)) \ - X("BIN", BIN, ) \ + Y(BIN, ) \ X("CLOSE-FILE", CLOSE_FILE, tos = close(tos); tos = tos ? errno : 0) \ X("FLUSH-FILE", FLUSH_FILE, fsync(tos); /* fsync has no impl and returns ENOSYS :-( */ tos = 0) \ X("OPEN-FILE", OPEN_FILE, cell_t mode = tos; DROP; cell_t len = tos; DROP; \ @@ -134,15 +134,15 @@ # include "esp_camera.h" # define OPTIONAL_CAMERA_SUPPORT \ /* Camera */ \ - X("esp_camera_init", ESP_CAMERA_INIT, \ + Y(esp_camera_init, \ tos = esp_camera_init((const camera_config_t *) tos)) \ - X("esp_camera_deinit", ESP_CAMERA_DEINIT, \ + Y(esp_camera_deinit, \ DUP; tos = esp_camera_deinit()) \ - X("esp_camera_fb_get", ESP_CAMERA_FB_GET, \ + Y(esp_camera_fb_get, \ DUP; tos = (cell_t) esp_camera_fb_get()) \ - X("esp_camera_db_return", ESP_CAMERA_FB_RETURN, \ + Y(esp_camera_db_return, \ esp_camera_fb_return((camera_fb_t *) tos); DROP) \ - X("esp_camera_sensor_get", ESP_CAMERA_SENSOR_GET, \ + Y(esp_camera_sensor_get, \ DUP; tos = (cell_t) esp_camera_sensor_get()) #endif @@ -218,7 +218,7 @@ X("SerialBT.connected", SERIALBT_CONNECTED, tos = ((BluetoothSerial *) tos)->connected(*sp); --sp) \ X("SerialBT.isReady", SERIALBT_IS_READY, tos = ((BluetoothSerial *) tos)->isReady(sp[-1], *sp); sp -= 2) \ /* Bluetooth */ \ - X("esp_bt_dev_get_address", ESP_BT_DEV_GET_ADDRESS, DUP; tos = (cell_t) esp_bt_dev_get_address()) + Y(esp_bt_dev_get_address, DUP; tos = (cell_t) esp_bt_dev_get_address()) #endif #ifndef ENABLE_WIFI_SUPPORT diff --git a/ueforth/common/calling.h b/ueforth/common/calling.h index 03dc267..6939618 100644 --- a/ueforth/common/calling.h +++ b/ueforth/common/calling.h @@ -4,15 +4,15 @@ typedef cell_t (CALLTYPE *call_t)(); #define CALLING_OPCODE_LIST \ - X("CALL0", OP_CALL0, tos = ((call_t) tos)()) \ - X("CALL1", OP_CALL1, tos = ((call_t) tos)(*sp); --sp) \ - X("CALL2", OP_CALL2, tos = ((call_t) tos)(sp[-1], *sp); sp -= 2) \ - X("CALL3", OP_CALL3, tos = ((call_t) tos)(sp[-2], sp[-1], *sp); sp -= 3) \ - X("CALL4", OP_CALL4, tos = ((call_t) tos)(sp[-3], sp[-2], sp[-1], *sp); sp -= 4) \ - X("CALL5", OP_CALL5, tos = ((call_t) tos)(sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 5) \ - X("CALL6", OP_CALL6, tos = ((call_t) tos)(sp[-5], sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 6) \ - X("CALL7", OP_CALL7, tos = ((call_t) tos)(sp[-6], sp[-5], sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 7) \ - X("CALL8", OP_CALL8, tos = ((call_t) tos)(sp[-7], sp[-6], sp[-5], sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 8) \ - X("CALL9", OP_CALL9, tos = ((call_t) tos)(sp[-8], sp[-7], sp[-6], sp[-5], sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 9) \ - X("CALL10", OP_CALL10, tos = ((call_t) tos)(sp[-9], sp[-8], sp[-7], sp[-6], sp[-5], sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 10) \ + Y(CALL0, tos = ((call_t) tos)()) \ + Y(CALL1, tos = ((call_t) tos)(*sp); --sp) \ + Y(CALL2, tos = ((call_t) tos)(sp[-1], *sp); sp -= 2) \ + Y(CALL3, tos = ((call_t) tos)(sp[-2], sp[-1], *sp); sp -= 3) \ + Y(CALL4, tos = ((call_t) tos)(sp[-3], sp[-2], sp[-1], *sp); sp -= 4) \ + Y(CALL5, tos = ((call_t) tos)(sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 5) \ + Y(CALL6, tos = ((call_t) tos)(sp[-5], sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 6) \ + Y(CALL7, tos = ((call_t) tos)(sp[-6], sp[-5], sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 7) \ + Y(CALL8, tos = ((call_t) tos)(sp[-7], sp[-6], sp[-5], sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 8) \ + Y(CALL9, tos = ((call_t) tos)(sp[-8], sp[-7], sp[-6], sp[-5], sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 9) \ + Y(CALL10, tos = ((call_t) tos)(sp[-9], sp[-8], sp[-7], sp[-6], sp[-5], sp[-4], sp[-3], sp[-2], sp[-1], *sp); sp -= 10) \ diff --git a/ueforth/common/opcodes.h b/ueforth/common/opcodes.h index fb2c16b..e07ca26 100644 --- a/ueforth/common/opcodes.h +++ b/ueforth/common/opcodes.h @@ -6,6 +6,7 @@ typedef intptr_t cell_t; typedef uintptr_t ucell_t; +#define Y(op, code) X(#op, id ## op, code) #define DUP *++sp = tos #define DROP tos = *sp-- #define COMMA(n) *g_sys.heap++ = (n) @@ -34,13 +35,13 @@ typedef int64_t dcell_t; X("U/MOD", USMOD, w = *sp; *sp = (ucell_t) w % (ucell_t) tos; \ tos = (ucell_t) w / (ucell_t) tos) \ X("*/MOD", SSMOD, SSMOD_FUNC) \ - X("AND", AND, tos &= *sp--) \ - X("OR", OR, tos |= *sp--) \ - X("XOR", XOR, tos ^= *sp--) \ - X("DUP", DUP, DUP) \ - X("SWAP", SWAP, w = tos; tos = *sp; *sp = w) \ - X("OVER", OVER, DUP; tos = sp[-1]) \ - X("DROP", DROP, DROP) \ + Y(AND, tos &= *sp--) \ + Y(OR, tos |= *sp--) \ + Y(XOR, tos ^= *sp--) \ + Y(DUP, DUP) \ + Y(SWAP, w = tos; tos = *sp; *sp = w) \ + Y(OVER, DUP; tos = sp[-1]) \ + Y(DROP, DROP) \ X("@", AT, tos = *(cell_t *) tos) \ X("L@", LAT, tos = *(int32_t *) tos) \ X("C@", CAT, tos = *(uint8_t *) tos) \ @@ -54,30 +55,28 @@ typedef int64_t dcell_t; X(">R", TOR, *++rp = tos; DROP) \ X("R>", FROMR, DUP; tos = *rp; --rp) \ X("R@", RAT, DUP; tos = *rp) \ - X("EXECUTE", EXECUTE, w = tos; DROP; JMPW) \ - X("BRANCH", BRANCH, ip = (cell_t *) *ip) \ - X("0BRANCH", ZBRANCH, if (!tos) ip = (cell_t *) *ip; else ++ip; DROP) \ - X("DONEXT", DONEXT, *rp = *rp - 1; \ - if (~*rp) ip = (cell_t *) *ip; else (--rp, ++ip)) \ - X("DOLIT", DOLIT, DUP; tos = *ip++) \ - X("ALITERAL", ALITERAL, COMMA(g_sys.DOLIT_XT); COMMA(tos); DROP) \ - X("CELL", CELL, DUP; tos = sizeof(cell_t)) \ - X("FIND", FIND, tos = find((const char *) *sp, tos); --sp) \ - X("PARSE", PARSE, DUP; tos = parse(tos, sp)) \ + Y(EXECUTE, w = tos; DROP; JMPW) \ + Y(BRANCH, ip = (cell_t *) *ip) \ + Y(0BRANCH, if (!tos) ip = (cell_t *) *ip; else ++ip; DROP) \ + Y(DONEXT, *rp = *rp - 1; if (~*rp) ip = (cell_t *) *ip; else (--rp, ++ip)) \ + Y(DOLIT, DUP; tos = *ip++) \ + Y(ALITERAL, COMMA(g_sys.DOLIT_XT); COMMA(tos); DROP) \ + Y(CELL, DUP; tos = sizeof(cell_t)) \ + Y(FIND, tos = find((const char *) *sp, tos); --sp) \ + Y(PARSE, DUP; tos = parse(tos, sp)) \ X("S>NUMBER?", CONVERT, tos = convert((const char *) *sp, tos, sp); \ if (!tos) --sp) \ - X("CREATE", CREATE, DUP; DUP; tos = parse(32, sp); \ - create((const char *) *sp, tos, 0, ADDR_DOCREATE); \ - COMMA(0); --sp; DROP) \ + Y(CREATE, DUP; DUP; tos = parse(32, sp); \ + create((const char *) *sp, tos, 0, ADDR_DOCREATE); \ + COMMA(0); --sp; DROP) \ X("DOES>", DOES, DOES(ip); ip = (cell_t *) *rp; --rp) \ - X("IMMEDIATE", IMMEDIATE, DOIMMEDIATE()) \ + Y(IMMEDIATE, DOIMMEDIATE()) \ X("'SYS", SYS, DUP; tos = (cell_t) &g_sys) \ - X("YIELD", YIELD, PARK; return rp) \ + Y(YIELD, PARK; return rp) \ X(":", COLON, DUP; DUP; tos = parse(32, sp); \ create((const char *) *sp, tos, SMUDGE, ADDR_DOCOLON); \ g_sys.state = -1; --sp; DROP) \ - X("EVALUATE1", EVALUATE1, DUP; sp = evaluate1(sp); w = *sp--; DROP; \ - if (w) JMPW) \ - X("EXIT", EXIT, ip = (cell_t *) *rp--) \ + Y(EVALUATE1, DUP; sp = evaluate1(sp); w = *sp--; DROP; if (w) JMPW) \ + Y(EXIT, ip = (cell_t *) *rp--) \ X(";", SEMICOLON, UNSMUDGE(); COMMA(g_sys.DOEXIT_XT); g_sys.state = 0) \ diff --git a/ueforth/posix/posix_main.c b/ueforth/posix/posix_main.c index 9b9284c..f90c9cb 100644 --- a/ueforth/posix/posix_main.c +++ b/ueforth/posix/posix_main.c @@ -8,7 +8,7 @@ #define STACK_SIZE (16 * 1024) #define PLATFORM_OPCODE_LIST \ - X("DLSYM", DLSYM, tos = (cell_t) dlsym((void *) *sp, (void *) tos); --sp) \ + Y(DLSYM, tos = (cell_t) dlsym((void *) *sp, (void *) tos); --sp) \ CALLING_OPCODE_LIST \ #include "common/core.h" diff --git a/ueforth/windows/windows_main.c b/ueforth/windows/windows_main.c index 41e2f60..3067aca 100644 --- a/ueforth/windows/windows_main.c +++ b/ueforth/windows/windows_main.c @@ -20,9 +20,9 @@ #define STACK_SIZE (64 * 1024) #define PLATFORM_OPCODE_LIST \ - X("GETPROCADDRESS", GETPROCADDRESS, \ + Y(GETPROCADDRESS, \ tos = (cell_t) GetProcAddress((HMODULE) *sp, (LPCSTR) tos); --sp) \ - X("LOADLIBRARYA", LOADLIBRARYA, \ + Y(LOADLIBRARYA, \ tos = (cell_t) LoadLibraryA((LPCSTR) tos)) \ CALLING_OPCODE_LIST \