Skip to content

Commit

Permalink
impl macro unary nodes + fixing a not operator codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Sep 8, 2024
1 parent 5bac2ee commit 8813506
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
7 changes: 7 additions & 0 deletions codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,13 @@ void codegen_generate_normal_unary(struct node *node, struct history *history) {
"eax", STACK_FRAME_ELEMENT_TYPE_PUSHED_VALUE, "result_value", 0,
&(struct stack_frame_data){.dtype = last_dtype});
}
} else if (S_EQ(node->unary.op, "!")) {
asm_push("cmp eax, 0");
asm_push("sete al");
asm_push("movzx eax, al");
asm_push_ins_push_with_data(
"eax", STACK_FRAME_ELEMENT_TYPE_PUSHED_VALUE, "result_value", 0,
&(struct stack_frame_data){.dtype = last_dtype});
}
}

Expand Down
2 changes: 1 addition & 1 deletion expressionable.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void expressionable_parse_parentheses(struct expressionable *expressionable) {

void expressionable_parse_for_normal_unary(
struct expressionable *expressionable) {
const char *unary_op = expressionable_peek_next(expressionable)->sval;
const char *unary_op = expressionable_token_next(expressionable)->sval;
expressionable_parse(expressionable);

void *unary_operand_node = expressionable_node_pop(expressionable);
Expand Down
22 changes: 22 additions & 0 deletions preprocessor/preprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,24 @@ int preprocessor_evaluate_exp(struct compile_process *compiler,
node->exp.op);
}

int preprocessor_evaluate_unary(struct compile_process *compiler,
struct preprocessor_node *node) {
int res = 0;
const char *op = node->unary_node.op;
struct preprocessor_node *right_operand = node->unary_node.operand;
if (S_EQ(op, "!")) {
res = !preprocessor_evaluate(compiler, right_operand);
} else if (S_EQ(op, "~")) {
res = ~preprocessor_evaluate(compiler, right_operand);
} else if (S_EQ(op, "-")) {
res = -preprocessor_evaluate(compiler, right_operand);
} else {
compiler_error(compiler, "unknown unary operator");
}

return res;
}

int preprocessor_evaluate(struct compile_process *compiler,
struct preprocessor_node *root_node) {
struct preprocessor_node *current = root_node;
Expand All @@ -1354,6 +1372,10 @@ int preprocessor_evaluate(struct compile_process *compiler,
case PREPROCESSOR_EXPRESSION_NODE:
res = preprocessor_evaluate_exp(compiler, current);
break;

case PREPROCESSOR_UNARY_NODE:
res = preprocessor_evaluate_unary(compiler, current);
break;
}

return res;
Expand Down
12 changes: 5 additions & 7 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#define TEST_FUNC(s) #s
#define ABC !1
#if ABC > 0
#error "ABC > 0"
#endif

int printf(const char *format, ...);

int main() {
const char *s = TEST_FUNC(Hello World !);
printf("%s\n", s);
}
int main() { return !0; }

0 comments on commit 8813506

Please sign in to comment.