10 #define MAXLOCALS (1 << 10)
11 #define MAXARGS (1 << 5)
12 #define print(s) write(2, (s), strlen(s));
14 #define TYPE_BT(t) ((t)->ptr ? 8 : (t)->bt)
15 #define TYPE_SZ(t) ((t)->ptr ? 8 : (t)->bt & BT_SZMASK)
16 #define TYPE_DEREF_BT(t) ((t)->ptr > 1 ? 8 : (t)->bt)
17 #define TYPE_DEREF_SZ(t) ((t)->ptr > 1 ? 8 : (t)->bt & BT_SZMASK)
28 static struct type ts
[MAXTMP
];
31 static void ts_push_bt(unsigned bt
)
38 static void ts_push(struct type
*type
)
43 static void ts_pop(struct type
*type
)
57 static void local_add(char *name
, long addr
, struct type type
)
59 strcpy(locals
[nlocals
].name
, name
);
60 locals
[nlocals
].addr
= addr
;
61 locals
[nlocals
].type
= type
;
65 static void die(char *s
)
71 static int tok_jmp(int tok
)
79 static void tok_expect(int tok
)
82 die("syntax error\n");
85 static void readexpr(void);
87 static int basetype(struct type
*type
)
118 tok_expect(TOK_NAME
);
130 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
135 static void readptrs(struct type
*type
)
137 while (!tok_jmp('*'))
141 static int readtype(struct type
*type
)
149 static void readprimary(void)
152 if (!tok_jmp(TOK_NUM
)) {
153 ts_push_bt(4 | BT_SIGNED
);
154 o_num(atoi(tok_id()), 4 | BT_SIGNED
);
157 if (!tok_jmp(TOK_NAME
)) {
158 for (i
= 0; i
< nlocals
; i
++) {
159 struct type
*t
= &locals
[nlocals
- 1].type
;
160 if (!strcmp(locals
[i
].name
, tok_id())) {
162 o_local(locals
[i
].addr
, TYPE_BT(t
));
163 if (t
->flags
& T_ARRAY
)
169 o_symaddr(tok_id(), 8);
179 static void readpost(void)
188 o_arrayderef(TYPE_DEREF_BT(&t1
));
195 unsigned bt
[MAXARGS
];
196 if (tok_see() != ')') {
198 bt
[argc
++] = 4 | BT_SIGNED
;
201 while (!tok_jmp(',')) {
203 bt
[argc
++] = 4 | BT_SIGNED
;
208 o_call(argc
, bt
, 4 | BT_SIGNED
);
209 ts_push_bt(4 | BT_SIGNED
);
213 static void readpre(void)
230 o_deref(TYPE_BT(&type
));
236 static void shifts(int n
)
245 static void ts_binop(void (*o_sth
)(void))
250 if (t1
.ptr
&& !t2
.ptr
) {
256 if (!t1
.ptr
&& t2
.ptr
)
257 if (TYPE_DEREF_SZ(&t2
) > 1) {
258 o_num(shifts(TYPE_DEREF_SZ(&t2
)), 1);
262 if (t1
.ptr
&& t2
.ptr
) {
263 o_num(shifts(TYPE_DEREF_SZ(&t1
)), 1);
265 ts_push_bt(4 | BT_SIGNED
);
271 static void readadd(void)
289 static void readcexpr(void)
294 long l1
= o_jzstub();
299 /* this is needed to fix tmp stack position */
306 static void readexpr(void)
311 o_assign(4 | BT_SIGNED
);
315 static void readstmt(void)
317 struct type base
= {0};
325 if (!basetype(&base
)) {
326 struct type type
= base
;
330 tok_expect(TOK_NAME
);
331 strcpy(name
, tok_id());
336 type
.flags
= T_ARRAY
;
339 local_add(name
, o_mklocal(TYPE_SZ(&type
) * n
), type
);
342 struct type
*t
= &locals
[nlocals
- 1].type
;
343 o_local(locals
[nlocals
- 1].addr
, TYPE_BT(t
));
346 ts_push_bt(TYPE_BT(t
));
347 o_assign(TYPE_BT(t
));
352 if (!tok_jmp(TOK_IF
)) {
359 if (!tok_jmp(TOK_ELSE
)) {
369 if (!tok_jmp(TOK_WHILE
)) {
381 if (!tok_jmp(TOK_RETURN
)) {
382 int ret
= tok_see() != ';';
386 o_ret(4 | BT_SIGNED
);
393 static void readdecl(void)
398 tok_expect(TOK_NAME
);
399 strcpy(name
, tok_id());
404 char args
[MAXARGS
][NAMELEN
];
405 struct type types
[MAXARGS
];
408 while (tok_see() != ')') {
409 readtype(&types
[nargs
]);
410 if (!tok_jmp(TOK_NAME
))
411 strcpy(args
[nargs
++], tok_id());
419 for (i
= 0; i
< nargs
; i
++)
420 local_add(args
[i
], o_arg(i
, TYPE_BT(&types
[i
])),
426 die("syntax error\n");
429 static void parse(void)
431 while (tok_see() != TOK_EOF
)
435 int main(int argc
, char *argv
[])
440 ifd
= open(src
, O_RDONLY
);
446 obj
[strlen(obj
) - 1] = 'o';
447 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);