From 4a01b8296f774a95080bacd208ddf31073d165f8 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 2 Feb 2014 14:55:46 +0900 Subject: [PATCH] emit OP_NOT --- include/picrin.h | 2 +- src/bool.c | 1 - src/codegen.c | 16 +++++++++++++++- src/state.c | 1 + 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/include/picrin.h b/include/picrin.h index 1f7c66d5..30ca337c 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -90,7 +90,7 @@ typedef struct { pic_sym sDEFINE_LIBRARY, sIMPORT, sEXPORT; pic_sym sCONS, sCAR, sCDR, sNILP; pic_sym sADD, sSUB, sMUL, sDIV, sMINUS; - pic_sym sEQ, sLT, sLE, sGT, sGE; + pic_sym sEQ, sLT, sLE, sGT, sGE, sNOT; struct xhash *sym_tbl; const char **sym_pool; diff --git a/src/bool.c b/src/bool.c index 09a80216..fa56fa31 100644 --- a/src/bool.c +++ b/src/bool.c @@ -57,7 +57,6 @@ pic_bool_equal_p(pic_state *pic) return pic_bool_value(pic_equal_p(pic, x, y)); } -/* TODO: replace it with native opcode */ static pic_value pic_bool_not(pic_state *pic) { diff --git a/src/codegen.c b/src/codegen.c index c2db013d..83fcd23b 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -91,7 +91,7 @@ typedef struct analyze_state { analyze_scope *scope; pic_sym rCONS, rCAR, rCDR, rNILP; pic_sym rADD, rSUB, rMUL, rDIV; - pic_sym rEQ, rLT, rLE, rGT, rGE; + pic_sym rEQ, rLT, rLE, rGT, rGE, rNOT; pic_sym sCALL, sTAILCALL, sREF; } analyze_state; @@ -137,6 +137,7 @@ new_analyze_state(pic_state *pic) register_renamed_symbol(pic, state, rLE, stdlib, "<="); register_renamed_symbol(pic, state, rGT, stdlib, ">"); register_renamed_symbol(pic, state, rGE, stdlib, ">="); + register_renamed_symbol(pic, state, rNOT, stdlib, "not"); register_symbol(pic, state, sCALL, "call"); register_symbol(pic, state, sTAILCALL, "tail-call"); @@ -521,6 +522,10 @@ analyze_node(analyze_state *state, pic_value obj, bool tailpos) ARGC_ASSERT(2); return CONSTRUCT_OP2(pic->sGE); } + else if (sym == state->rNOT) { + ARGC_ASSERT(1); + return CONSTRUCT_OP1(pic->sNOT); + } } return analyze_call(state, obj, tailpos); } @@ -1273,6 +1278,12 @@ codegen(codegen_state *state, pic_value obj) cxt->clen++; return; } + else if (sym == pic->sNOT) { + codegen(state, pic_list_ref(pic, obj, 1)); + cxt->code[cxt->clen].insn = OP_NOT; + cxt->clen++; + return; + } else if (sym == state->sCALL || sym == state->sTAILCALL) { int len = pic_length(pic, obj); pic_value elt; @@ -1526,6 +1537,9 @@ print_code(pic_state *pic, struct pic_code c) case OP_JMPIF: printf("OP_JMPIF\t%d\n", c.u.i); break; + case OP_NOT: + puts("OP_NOT"); + break; case OP_CALL: printf("OP_CALL\t%d\n", c.u.i); break; diff --git a/src/state.c b/src/state.c index 8236d2e9..f1a78dc5 100644 --- a/src/state.c +++ b/src/state.c @@ -112,6 +112,7 @@ pic_open(int argc, char *argv[], char **envp) register_core_symbol(pic, sLE, "<="); register_core_symbol(pic, sGT, ">"); register_core_symbol(pic, sGE, ">="); + register_core_symbol(pic, sNOT, "not"); pic_gc_arena_restore(pic, ai); pic_init_core(pic);