forked from Wilfred/babyc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassembly_unary_op.c
93 lines (83 loc) · 3 KB
/
assembly_unary_op.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* ----------------------------------------------------------------
*
* BabyC Toy compiler for educational purposes
*
* ---------------------------------------------------------------- */
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ast_annotate.h"
#include "assembly.h"
extern bool peephole_optimize;
/* -----------------------------------------------------------
* Helper functions
*
* write unary operation when operand is an expression
* - evaluate expression into eax
* - emit the binary opcode op value, eax
*
* write binary operation when operand is immediate
* - evaluate the expression
* - emit an opcode to set eax
* ----------------------------------------------------------- */
ProcessorFlags write_unary_syntax(FILE *out, UnaryExpressionType unary_type,
Syntax *expression, Context *ctx) {
ProcessorFlags flag = FLAG_NONE;
if (peephole_optimize) {
if (expression->type == IMMEDIATE) {
AstInteger value;
ast_integer_unary_operation(&value, &expression->immediate->value,
unary_type);
if (ast_integer_is_zero(&value)) {
emit_instr(out, "xorl", "%eax, %eax");
flag = FLAG_Z_VALID;
} else if (ast_integer_is_one(&value)) {
emit_instr(out, "movl", "$1, %eax");
flag = FLAG_BOOL_VALID;
} else {
unsigned v = ast_integer_get_uint(&value);
emit_instr_format(out, "movl", "$%u, %%eax", v);
}
return flag;
}
}
flag = write_condition_syntax(out, expression, ctx);
if (unary_type == CAST) {
// TODO
} else if (unary_type == BITWISE_NEGATION) {
emit_instr(out, "notl", "%eax");
flag = FLAG_Z_VALID;
} else if (unary_type == ARITHMETIC_NEGATION) {
emit_instr(out, "negl", "%eax");
flag = FLAG_Z_VALID;
} else if (unary_type == LOGICAL_NEGATION) {
if (flag == FLAG_Z_VALID) {
emit_instr(out, "testl", "%eax, %eax");
emit_instr(out, "setz", "%al");
emit_instr(out, "movzbl", "%al, %eax");
flag = FLAG_Z_BOOL;
} else if (flag == FLAG_Z_BOOL) {
emit_instr(out, "xorl", "$1, %eax");
flag = FLAG_Z_BOOL;
} else if (flag == FLAG_NZ_BOOL) {
emit_instr(out, "xorl", "$1, %eax");
flag = FLAG_NZ_BOOL;
} else if (flag == FLAG_BOOL_VALID) {
emit_instr(out, "xorl", "$1, %eax");
flag = FLAG_Z_BOOL;
} else if (!syntax_is_boolean(expression)) {
emit_instr(out, "testl", "%eax, %eax");
emit_instr(out, "setz", "%al");
emit_instr(out, "movzbl", "%al, %eax");
flag = FLAG_Z_BOOL;
} else {
emit_instr(out, "xorl", "$1, %eax");
flag = FLAG_Z_BOOL;
}
}
return flag;
}