aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-03-19 23:41:13 +0800
committerMistivia <i@mistivia.com>2025-03-19 23:41:38 +0800
commit48f36f70413c944be9c764e846a1017dd00c63ec (patch)
tree4e8321ad9dc4a4716fcd88220d689babe8f397d5
parentafb5ae7905c666eb259288ba0d3b47f71a13958f (diff)
add complete op list
-rw-r--r--Makefile2
-rw-r--r--src/as_op.c97
-rw-r--r--src/as_op.h21
-rw-r--r--src/as_parser.c6
-rw-r--r--src/as_tokenizer.c14
-rw-r--r--tests/test_as_parser.c4
6 files changed, 113 insertions, 31 deletions
diff --git a/Makefile b/Makefile
index 46d01fd..ee68869 100644
--- a/Makefile
+++ b/Makefile
@@ -24,7 +24,7 @@ buildtest: $(tests_bin)
test: buildtest
@echo
@echo "Run tests:"
- @scripts/runall.sh $^
+ @scripts/runall.sh $(tests_bin)
$(obj):%.o:%.c
$(cc) -c $(cflags) $< -MD -MF $@.d -o $@
diff --git a/src/as_op.c b/src/as_op.c
index 88884b8..6cfd8ce 100644
--- a/src/as_op.c
+++ b/src/as_op.c
@@ -5,16 +5,91 @@
struct opTableEntry{
enum op op;
const char* name;
-};
+};
+
struct opTableEntry opTable[] = {
- {ADD, "add"},
- {SUB, "sub"},
- {MUL, "mul"},
- {DIV, "div"},
- {MOD, "mod"},
- {EQ, "eq"},
- {OPEND, NULL}
+ // OP_SP, OP_SSP, OP_BP, OP_SBP, OP_PC, OP_RV, OP_SRV,
+ {OP_SP, "sp"},
+ {OP_SSP, "ssp"},
+ {OP_BP, "bp"},
+ {OP_SBP, "sbp"},
+ {OP_PC, "pc"},
+ {OP_RV, "rv"},
+ {OP_SRV, "srv"},
+ // OP_IMM,
+ {OP_IMM, "imm"},
+ // OP_LD8, OP_LD16, OP_LD32, OP_LD,
+ {OP_LD8, "ld8"},
+ {OP_LD16, "ld16"},
+ {OP_LD32, "ld32"},
+ {OP_LD, "ld"},
+ // OP_ST8, OP_ST16, OP_ST32, OP_ST,
+ {OP_ST8, "st8"},
+ {OP_ST16, "st16"},
+ {OP_ST32, "st32"},
+ {OP_ST, "st"},
+ // OP_DUP, OP_POP, OP_SWAP, OP_OVER, OP_ROT,
+ {OP_DUP, "dup"},
+ {OP_POP, "pop"},
+ {OP_SWAP, "swap"},
+ {OP_OVER, "over"},
+ {OP_ROT, "rot"},
+ // OP_ADD, OP_SUB, OP_DIV, OP_MUL, OP_MOD,
+ {OP_ADD, "add"},
+ {OP_SUB, "sub"},
+ {OP_DIV, "div"},
+ {OP_MUL, "mul"},
+ {OP_MOD, "mod"},
+ // OP_SHR, OP_SHL, OP_SAR,
+ {OP_SHR, "shr"},
+ {OP_SHL, "shl"},
+ {OP_SAR, "sar"},
+ // OP_AND, OP_OR, OP_NOT,
+ {OP_AND, "and"},
+ {OP_OR, "or"},
+ {OP_NOT, "not"},
+ // OP_BITAND, OP_BITOR, OP_XOR, OP_INVERT,
+ {OP_BITAND, "bitand"},
+ {OP_BITOR, "bitor"},
+ {OP_XOR, "xor"},
+ {OP_INVERT, "invert"},
+ // OP_GT, OP_LT, OP_GE, OP_LE, OP_EQ, OP_NEQ,
+ {OP_GT, "gt"},
+ {OP_LT, "lt"},
+ {OP_GE, "ge"},
+ {OP_LE, "le"},
+ {OP_EQ, "eq"},
+ {OP_NEQ, "neq"},
+ // OP_JMP, OP_JZ, OP_JNZ, OP_RET, OP_CALL, OP_SYSCALL,
+ {OP_JMP, "jmp"},
+ {OP_JZ, "jz"},
+ {OP_JNZ, "jnz"},
+ {OP_RET, "ret"},
+ {OP_CALL, "call"},
+ {OP_SYSCALL, "syscall"},
+ // OP_FADD, OP_FSUB, OP_FMUL, OP_FDIV,
+ {OP_FADD, "fadd"},
+ {OP_FSUB, "fsub"},
+ {OP_FMUL, "fmul"},
+ {OP_FDIV, "fdiv"},
+ // OP_FGE, OP_FGT, OP_FLE, OP_FLT, OP_FEQ, OP_FNEQ,
+ {OP_FGE, "fge"},
+ {OP_FGT, "fgt"},
+ {OP_FLT, "flt"},
+ {OP_FEQ, "feq"},
+ {OP_FNEQ, "fneq"},
+ // OP_FTI, OP_ITF,
+ {OP_FTI, "fti"},
+ {OP_ITF, "itf"},
+ // OP_EXIT,
+ {OP_EXIT, "exit"},
+ // OP_LDARG, OP_LDVAR, OP_STARG, OP_STVAR,
+ {OP_LDARG, "ldarg"},
+ {OP_LDVAR, "ldvar"},
+ {OP_STARG, "starg"},
+ {OP_STVAR, "stvar"},
+ {OP_END, NULL},
};
enum op str2op(const char* str) {
@@ -23,5 +98,9 @@ enum op str2op(const char* str) {
return opTable[i].op;
}
}
- return OPEND;
+ return OP_END;
+}
+
+int isOp(const char *str) {
+ return OP_END != str2op(str);
}
diff --git a/src/as_op.h b/src/as_op.h
index dcd77e0..bded9e4 100644
--- a/src/as_op.h
+++ b/src/as_op.h
@@ -2,12 +2,27 @@
#define FVM_AS_OP_H_
enum op {
- ADD, SUB, MUL, DIV, MOD, EQ,
- // place holder for the end of the table
- OPEND
+ OP_SP, OP_SSP, OP_BP, OP_SBP, OP_PC, OP_RV, OP_SRV,
+ OP_IMM,
+ OP_LD8, OP_LD16, OP_LD32, OP_LD,
+ OP_ST8, OP_ST16, OP_ST32, OP_ST,
+ OP_DUP, OP_POP, OP_SWAP, OP_OVER, OP_ROT,
+ OP_ADD, OP_SUB, OP_DIV, OP_MUL, OP_MOD,
+ OP_SHR, OP_SHL, OP_SAR,
+ OP_AND, OP_OR, OP_NOT,
+ OP_BITAND, OP_BITOR, OP_XOR, OP_INVERT,
+ OP_GT, OP_LT, OP_GE, OP_LE, OP_EQ, OP_NEQ,
+ OP_JMP, OP_JZ, OP_JNZ, OP_RET, OP_CALL, OP_SYSCALL,
+ OP_FADD, OP_FSUB, OP_FMUL, OP_FDIV,
+ OP_FGE, OP_FGT, OP_FLE, OP_FLT, OP_FEQ, OP_FNEQ,
+ OP_FTI, OP_ITF,
+ OP_EXIT,
+ OP_LDARG, OP_LDVAR, OP_STARG, OP_STVAR,
+ OP_END
};
enum op str2op(const char *str);
+int isOp(const char *str);
#endif
diff --git a/src/as_parser.c b/src/as_parser.c
index 990bc1c..17d0b87 100644
--- a/src/as_parser.c
+++ b/src/as_parser.c
@@ -14,7 +14,7 @@
// <stmt> ::= <label> <instr> | <instr> | <label>
// <instr> ::= <op> | <op> arg | <op> tag
// <label> ::= tag ":"
-// <op> ::= "add" | "sub" | "mul" | "div" | "mod" | "eq"
+// <op> ::= "add" | "sub" | "mul" | "div" | "mod" | "eq" | ...
Prog parseProg(Allocator alct, TokenStream ts) {
@@ -98,7 +98,7 @@ enum op parseOp(Allocator alct, TokenStream ts) {
enum op op;
if (t->type == OP) {
op = str2op(t->sval);
- if (op == OPEND) {
+ if (op == OP_END) {
fprintf(stderr, "%d:%d Invalid OP.\n", t->line, t->col);
exit(-1);
}
@@ -114,7 +114,7 @@ Instr parseInstr(Allocator alct, TokenStream ts) {
Instr i = allocate(alct, sizeof(struct instr));
i->tagName = NULL;
i->arg = NULL;
- i->op = OPEND;
+ i->op = OP_END;
if (t->type == OP) {
i->op = parseOp(alct, ts);
t = peekToken(alct, ts);
diff --git a/src/as_tokenizer.c b/src/as_tokenizer.c
index 4cb695e..4a5353a 100644
--- a/src/as_tokenizer.c
+++ b/src/as_tokenizer.c
@@ -3,6 +3,7 @@
#include <string.h>
#include "as_tokenizer.h"
+#include "as_op.h"
#include "utils.h"
int InputStream_nextChar(InputStream s) {
@@ -44,19 +45,6 @@ int InputStream_peekChar(InputStream s) {
return s->buf[s->cursor];
}
-char* ops[] = {
- "add", "sub", "mul", "div", "mod", "eq"
-};
-
-int isOp(const char* str) {
- for (int i = 0; i < sizeof(ops) / sizeof(ops[0]); i++) {
- if (strcmp(ops[i], str) == 0) {
- return 1;
- }
- }
- return 0;
-}
-
int isStartOfIndentifier(int c) {
if (c >= 'a' && c <= 'z') {
return 1;
diff --git a/tests/test_as_parser.c b/tests/test_as_parser.c
index e11a353..e71f190 100644
--- a/tests/test_as_parser.c
+++ b/tests/test_as_parser.c
@@ -28,11 +28,11 @@ int main(int argc, char** argv) {
assert(strcmp("start", stmts[0]->label->name) == 0);
assert(stmts[1]->label == NULL);
- assert(stmts[1]->instr->op == ADD);
+ assert(stmts[1]->instr->op == OP_ADD);
assert(stmts[1]->instr->arg->ival == 1);
assert(strcmp("end", stmts[4]->label->name) == 0);
- assert(stmts[4]->instr->op == EQ);
+ assert(stmts[4]->instr->op == OP_EQ);
printf("[PASS] assembler parser\n");
deleteAllocator(alct);