2 * neatcc - A small and simple x86_64 C compiler
4 * Copyright (C) 2010 Ali Gholami Rudi
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, as published by the
8 * Free Software Foundation.
15 #include <sys/types.h>
19 #define MAXLOCALS (1 << 10)
20 #define MAXGLOBALS (1 << 10)
21 #define MAXARGS (1 << 5)
22 #define print(s) write(2, (s), strlen(s));
24 #define TYPE_BT(t) ((t)->ptr ? 8 : (t)->bt)
25 #define TYPE_SZ(t) ((t)->ptr ? 8 : (t)->bt & BT_SZMASK)
38 int id
; /* for structs, functions and arrays */
42 static struct type ts
[MAXTMP
];
45 static void ts_push_bt(unsigned bt
)
52 static void ts_push(struct type
*t
)
54 memcpy(&ts
[nts
++], t
, sizeof(*t
));
55 if (t
->flags
& (T_FUNC
| T_ARRAY
) && !t
->ptr
)
59 static void ts_pop(struct type
*type
)
72 static struct name locals
[MAXLOCALS
];
74 static struct name globals
[MAXGLOBALS
];
77 static void local_add(struct name
*name
)
79 memcpy(&locals
[nlocals
++], name
, sizeof(*name
));
82 static int global_find(char *name
)
85 for (i
= 0; i
< nglobals
; i
++)
86 if (!strcmp(name
, globals
[i
].name
))
91 static void global_add(struct name
*name
)
93 int found
= global_find(name
->name
);
94 int i
= found
== -1 ? nglobals
++ : found
;
95 memcpy(&globals
[i
], name
, sizeof(*name
));
98 static void die(char *s
)
104 #define MAXENUMS (1 << 10)
106 static struct enumval
{
112 static void enum_add(char *name
, int val
)
114 struct enumval
*ev
= &enums
[nenums
++];
115 strcpy(ev
->name
, name
);
119 static int enum_find(int *val
, char *name
)
122 for (i
= nenums
- 1; i
>= 0; --i
)
123 if (!strcmp(name
, enums
[i
].name
)) {
130 #define MAXTYPEDEFS (1 << 5)
132 static struct typdefinfo
{
135 } typedefs
[MAXTYPEDEFS
];
136 static int ntypedefs
;
138 static void typedef_add(char *name
, struct type
*type
)
140 struct typdefinfo
*ti
= &typedefs
[ntypedefs
++];
141 strcpy(ti
->name
, name
);
142 memcpy(&ti
->type
, type
, sizeof(*type
));
145 static int typedef_find(char *name
)
148 for (i
= ntypedefs
- 1; i
>= 0; --i
)
149 if (!strcmp(name
, typedefs
[i
].name
))
154 #define MAXARRAYS (1 << 5)
156 static struct array
{
162 static int array_add(struct type
*type
, int n
)
164 struct array
*a
= &arrays
[narrays
++];
165 memcpy(&a
->type
, type
, sizeof(*type
));
170 static void array2ptr(struct type
*t
)
172 if (!(t
->flags
& T_ARRAY
) || t
->ptr
)
174 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
178 #define MAXTYPES (1 << 7)
179 #define MAXFIELDS (1 << 5)
181 static struct structinfo
{
183 struct name fields
[MAXFIELDS
];
190 static int struct_find(char *name
, int isunion
)
193 for (i
= nstructs
- 1; i
>= 0; --i
)
194 if (!strcmp(name
, structs
[i
].name
) &&
195 structs
[i
].isunion
== isunion
)
197 die("struct not found\n");
200 static struct name
*struct_field(int id
, char *name
)
202 struct structinfo
*si
= &structs
[id
];
204 for (i
= 0; i
< si
->nfields
; i
++)
205 if (!strcmp(name
, si
->fields
[i
].name
))
206 return &si
->fields
[i
];
207 die("field not found\n");
210 #define MAXBREAK (1 << 7)
211 static long breaks
[MAXBREAK
];
213 static long continues
[MAXBREAK
];
214 static int ncontinues
;
216 static void break_fill(long addr
, int till
)
219 for (i
= till
; i
< nbreaks
; i
++)
220 o_filljmp2(breaks
[i
], addr
);
224 static void continue_fill(long addr
, int till
)
227 for (i
= till
; i
< ncontinues
; i
++)
228 o_filljmp2(continues
[i
], addr
);
232 static int type_totsz(struct type
*t
)
236 if (t
->flags
& T_ARRAY
)
237 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
238 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: BT_SZ(t
->bt
);
241 static unsigned type_szde(struct type
*t
)
243 if (t
->flags
& T_ARRAY
)
244 return t
->ptr
> 0 ? 8 : TYPE_SZ(&arrays
[t
->id
].type
);
246 return t
->ptr
> 1 ? 8 : BT_SZ(t
->bt
);
249 static int tok_jmp(int tok
)
251 if (tok_see() != tok
)
257 static void tok_expect(int tok
)
259 if (tok_get() != tok
)
260 die("syntax error\n");
263 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
265 unsigned s1
= BT_SZ(bt1
);
266 unsigned s2
= BT_SZ(bt2
);
267 return (bt1
| bt2
) & BT_SIGNED
| (s1
> s2
? s1
: s2
);
270 static void ts_binop(void (*o_sth
)(void))
276 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
279 static int shifts(int n
)
288 static void ts_binop_add(void (*o_sth
)(void))
295 if (!t1
.ptr
&& !t2
.ptr
) {
297 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
300 if (t1
.ptr
&& !t2
.ptr
) {
306 if (!t1
.ptr
&& t2
.ptr
)
307 if (type_szde(&t2
) > 1) {
308 o_num(shifts(type_szde(&t2
)), 1);
312 if (t1
.ptr
&& t2
.ptr
) {
313 o_num(shifts(type_szde(&t1
)), 1);
315 ts_push_bt(4 | BT_SIGNED
);
321 static void structdef(void *data
, struct name
*name
, unsigned flags
)
323 struct structinfo
*si
= data
;
326 if (si
->size
< type_totsz(&name
->type
))
327 si
->size
= type_totsz(&name
->type
);
329 name
->addr
= si
->size
;
330 si
->size
+= type_totsz(&name
->type
);
332 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
335 static int readdefs(void (*def
)(void *, struct name
*, unsigned f
), void *data
);
337 static int struct_create(char *name
, int isunion
)
340 struct structinfo
*si
= &structs
[id
];
341 strcpy(si
->name
, name
);
342 si
->isunion
= isunion
;
344 while (tok_jmp('}')) {
345 readdefs(structdef
, si
);
351 static void readexpr(void);
353 static void enum_create(void)
357 while (tok_jmp('}')) {
359 tok_expect(TOK_NAME
);
360 strcpy(name
, tok_id());
361 if (tok_see() == '=') {
366 die("const expr expected!\n");
373 static int basetype(struct type
*type
, unsigned *flags
)
412 isunion
= tok_get() == TOK_UNION
;
413 tok_expect(TOK_NAME
);
414 strcpy(name
, tok_id());
415 if (tok_see() == '{')
416 type
->id
= struct_create(name
, isunion
);
418 type
->id
= struct_find(name
, isunion
);
419 type
->flags
|= T_STRUCT
;
424 tok_expect(TOK_NAME
);
425 if (tok_see() == '{')
427 type
->bt
= 4 | BT_SIGNED
;
430 if (tok_see() == TOK_NAME
) {
431 int id
= typedef_find(tok_id());
434 memcpy(type
, &typedefs
[id
].type
,
447 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
451 static void readptrs(struct type
*type
)
453 while (!tok_jmp('*'))
457 static int readtype(struct type
*type
)
460 if (basetype(type
, &flags
))
466 static void readpre(void);
468 static void readprimary(void)
472 if (!tok_jmp(TOK_NUM
)) {
473 ts_push_bt(4 | BT_SIGNED
);
474 o_num(tok_num(), 4 | BT_SIGNED
);
477 if (!tok_jmp(TOK_STR
)) {
481 t
.bt
= 1 | BT_SIGNED
;
486 o_symaddr(o_mkdat(NULL
, buf
, len
, 0), TYPE_BT(&t
));
490 if (!tok_jmp(TOK_NAME
)) {
492 for (i
= nlocals
- 1; i
>= 0; --i
) {
493 struct type
*t
= &locals
[i
].type
;
494 if (!strcmp(locals
[i
].name
, tok_id())) {
495 o_local(locals
[i
].addr
, TYPE_BT(t
));
500 if ((n
= global_find(tok_id())) != -1) {
501 struct type
*t
= &globals
[n
].type
;
502 o_symaddr(globals
[n
].addr
, TYPE_BT(t
));
506 if (!enum_find(&n
, tok_id())) {
507 ts_push_bt(4 | BT_SIGNED
);
508 o_num(n
, 4 | BT_SIGNED
);
511 strcpy(name
.name
, tok_id());
512 name
.addr
= o_mkundef(name
.name
);
515 o_symaddr(name
.addr
, 8);
526 if (!t
.ptr
|| !o
.ptr
)
536 void arrayderef(struct type
*t
)
538 int sz
= type_totsz(t
);
547 static void inc_post(void (*op
)(void))
549 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
563 static void readfield(void)
567 tok_expect(TOK_NAME
);
569 field
= struct_field(t
.id
, tok_id());
571 o_num(field
->addr
, 4);
574 o_deref(TYPE_BT(&field
->type
));
575 ts_push(&field
->type
);
578 #define MAXFUNCS (1 << 10)
580 static struct funcinfo
{
581 struct type args
[MAXFIELDS
];
586 static unsigned ret_bt
;
588 static int func_create(struct type
*ret
, struct name
*args
, int nargs
)
590 struct funcinfo
*fi
= &funcs
[nfuncs
++];
592 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
593 for (i
= 0; i
< nargs
; i
++)
594 memcpy(&fi
->args
[i
], &args
[i
].type
, sizeof(*ret
));
599 static void readcall(void)
602 unsigned bt
[MAXARGS
];
606 if (tok_see() != ')') {
609 bt
[argc
++] = TYPE_BT(&t
);
611 while (!tok_jmp(',')) {
614 bt
[argc
++] = TYPE_BT(&t
);
618 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
620 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
622 for (i
= 0; i
< fi
->nargs
; i
++)
623 bt
[i
] = TYPE_BT(&fi
->args
[i
]);
624 o_call(argc
, bt
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
628 ts_push_bt(4 | BT_SIGNED
);
631 static void readpost(void)
651 if (!tok_jmp(TOK2("++"))) {
655 if (!tok_jmp(TOK2("--"))) {
664 if (!tok_jmp(TOK2("->"))) {
672 static void inc_pre(void (*op
)(void))
674 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
685 static void readpre(void)
691 if (!(type
.flags
& T_FUNC
) && !type
.ptr
)
702 if (!(t
.flags
& T_FUNC
) || t
.ptr
> 0) {
704 o_deref(TYPE_BT(&t
));
714 ts_push_bt(4 | BT_SIGNED
);
727 if (!tok_jmp(TOK2("++"))) {
731 if (!tok_jmp(TOK2("--"))) {
735 if (!tok_jmp(TOK_SIZEOF
)) {
737 int op
= !tok_jmp('(');
739 int nogen
= !o_nogen();
747 o_num(type_totsz(&t
), 4);
755 static void readmul(void)
778 static void readadd(void)
796 static void shift(void (*op
)(void))
803 ts_push_bt(TYPE_BT(&t
));
806 static void readshift(void)
810 if (!tok_jmp(TOK2("<<"))) {
814 if (!tok_jmp(TOK2(">>"))) {
822 static void cmp(void (*op
)(void))
828 ts_push_bt(4 | BT_SIGNED
);
831 static void readcmp(void)
843 if (!tok_jmp(TOK2("<="))) {
847 if (!tok_jmp(TOK2(">="))) {
855 static void eq(void (*op
)(void))
861 ts_push_bt(4 | BT_SIGNED
);
864 static void readeq(void)
868 if (!tok_jmp(TOK2("=="))) {
872 if (!tok_jmp(TOK2("!="))) {
880 static void readbitand(void)
883 while (!tok_jmp('&')) {
889 static void readxor(void)
892 while (!tok_jmp('^')) {
898 static void readbitor(void)
901 while (!tok_jmp('|')) {
907 #define MAXCOND (1 << 5)
909 static void readand(void)
916 if (tok_see() != TOK2("&&"))
918 conds
[nconds
++] = o_jz(0);
920 while (!tok_jmp(TOK2("&&"))) {
922 conds
[nconds
++] = o_jz(0);
925 o_num(1, 4 | BT_SIGNED
);
928 for (i
= 0; i
< nconds
; i
++)
930 o_num(0, 4 | BT_SIGNED
);
933 ts_push_bt(4 | BT_SIGNED
);
936 static void reador(void)
943 if (tok_see() != TOK2("||"))
945 conds
[nconds
++] = o_jnz(0);
947 while (!tok_jmp(TOK2("||"))) {
949 conds
[nconds
++] = o_jnz(0);
952 o_num(0, 4 | BT_SIGNED
);
955 for (i
= 0; i
< nconds
; i
++)
957 o_num(1, 4 | BT_SIGNED
);
960 ts_push_bt(4 | BT_SIGNED
);
963 static void readcexpr(void)
971 cexpr
= !o_popnum(&c
);
1010 static void opassign(void (*bop
)(void (*op
)(void)), void (*op
)(void))
1012 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
1020 static void doassign(void)
1024 if (!t
.ptr
&& t
.flags
& T_STRUCT
)
1025 o_memcpy(type_totsz(&t
));
1027 o_assign(TYPE_BT(&ts
[nts
- 1]));
1030 static void readexpr(void)
1033 if (!tok_jmp('=')) {
1038 if (!tok_jmp(TOK2("+="))) {
1039 opassign(ts_binop_add
, o_add
);
1042 if (!tok_jmp(TOK2("-="))) {
1043 opassign(ts_binop_add
, o_sub
);
1046 if (!tok_jmp(TOK2("*="))) {
1047 opassign(ts_binop
, o_mul
);
1050 if (!tok_jmp(TOK2("/="))) {
1051 opassign(ts_binop
, o_div
);
1054 if (!tok_jmp(TOK2("%="))) {
1055 opassign(ts_binop
, o_mod
);
1058 if (!tok_jmp(TOK3("<<="))) {
1059 opassign(ts_binop
, o_shl
);
1062 if (!tok_jmp(TOK3(">>="))) {
1063 opassign(ts_binop
, o_shr
);
1066 if (!tok_jmp(TOK3("&="))) {
1067 opassign(ts_binop
, o_and
);
1070 if (!tok_jmp(TOK3("|="))) {
1071 opassign(ts_binop
, o_or
);
1074 if (!tok_jmp(TOK3("^="))) {
1075 opassign(ts_binop
, o_xor
);
1080 static void readestmt(void)
1086 } while (!tok_jmp(','));
1089 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1091 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1093 char *varname
= flags
& F_STATIC
? NULL
: name
->name
;
1094 name
->addr
= o_mkvar(varname
, type_totsz(&name
->type
), F_GLOBAL(flags
));
1098 static void o_localoff(long addr
, int off
, unsigned bt
)
1109 static struct type
*innertype(struct type
*t
)
1111 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1112 return innertype(&arrays
[t
->id
].type
);
1116 static void initexpr(struct type
*t
, long addr
, int off
)
1119 o_localoff(addr
, off
, TYPE_BT(t
));
1127 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1128 struct structinfo
*si
= &structs
[t
->id
];
1130 for (i
= 0; i
< si
->nfields
; i
++) {
1131 struct name
*field
= &si
->fields
[i
];
1132 if (!tok_jmp('.')) {
1133 tok_expect(TOK_NAME
);
1134 field
= struct_field(t
->id
, tok_id());
1137 initexpr(&field
->type
, addr
, off
+ field
->addr
);
1138 if (tok_jmp(',') || tok_see() == '}')
1144 memcpy(&t_de
, t
, sizeof(*t
));
1145 if (t
->flags
& T_ARRAY
)
1148 for (i
= 0; ; i
++) {
1150 struct type
*it
= &t_de
;
1151 if (!tok_jmp('[')) {
1158 if (tok_see() != '{')
1159 it
= innertype(&t_de
);
1160 initexpr(it
, addr
, off
+ type_totsz(it
) * idx
);
1161 if (tok_jmp(',') || tok_see() == '}')
1168 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1170 if (flags
& F_STATIC
) {
1171 globaldef(data
, name
, flags
);
1174 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1176 if (flags
& F_INIT
) {
1177 struct type
*t
= &name
->type
;
1178 if (tok_see() == '{') {
1179 o_local(name
->addr
, TYPE_BT(t
));
1180 o_memset(0, type_totsz(t
));
1183 initexpr(t
, name
->addr
, 0);
1187 static void funcdef(struct name
*name
, struct name
*args
,
1188 int nargs
, unsigned flags
)
1191 name
->addr
= o_func_beg(name
->name
, F_GLOBAL(flags
));
1193 ret_bt
= TYPE_BT(&funcs
[name
->type
.id
].ret
);
1194 for (i
= 0; i
< nargs
; i
++) {
1195 args
[i
].addr
= o_arg(i
, type_totsz(&args
[i
].type
));
1196 local_add(&args
[i
]);
1200 static int readargs(struct name
*args
)
1204 while (tok_see() != ')') {
1205 readtype(&args
[nargs
].type
);
1206 if (!tok_jmp(TOK_NAME
))
1207 strcpy(args
[nargs
++].name
, tok_id());
1215 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1220 if (basetype(&base
, &flags
))
1222 while (tok_see() != ';' && tok_see() != '{') {
1223 struct type tpool
[3];
1226 struct type
*type
= &tpool
[npool
++];
1227 struct type
*func
= NULL
;
1228 struct type
*ret
= NULL
;
1229 memset(tpool
, 0, sizeof(tpool
));
1230 memcpy(type
, &base
, sizeof(base
));
1232 if (!tok_jmp('(')) {
1234 type
= &tpool
[npool
++];
1238 tok_expect(TOK_NAME
);
1239 strcpy(name
.name
, tok_id());
1240 while (!tok_jmp('[')) {
1245 die("const expr expected\n");
1246 type
->id
= array_add(type
, n
);
1247 if (type
->flags
& T_FUNC
)
1248 func
= &arrays
[type
->id
].type
;
1249 type
->flags
= T_ARRAY
;
1256 if (tok_see() == '(') {
1257 struct name args
[MAXARGS
];
1258 int nargs
= readargs(args
);
1262 type
= &tpool
[npool
++];
1265 func
->flags
= T_FUNC
;
1267 func
->id
= func_create(ret
, args
, nargs
);
1268 if (fdef
&& tok_see() == '{') {
1269 memcpy(&name
.type
, func
, sizeof(*func
));
1270 funcdef(&name
, args
, nargs
, flags
);
1274 memcpy(&name
.type
, type
, sizeof(*type
));
1277 def(data
, &name
, flags
);
1283 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1285 typedef_add(name
->name
, &name
->type
);
1288 #define MAXCASES (1 << 7)
1290 static void readstmt(void);
1292 static void readswitch(void)
1294 int break_beg
= nbreaks
;
1295 long val_addr
= o_mklocal(8);
1296 long matched
[MAXCASES
];
1305 o_local(val_addr
, TYPE_BT(&t
));
1307 o_assign(TYPE_BT(&t
));
1311 while (tok_jmp('}')) {
1313 while (tok_see() == TOK_CASE
|| tok_see() == TOK_DEFAULT
) {
1315 matched
[nmatched
++] = o_jmp(0);
1318 if (!tok_jmp(TOK_CASE
)) {
1320 o_local(val_addr
, TYPE_BT(&t
));
1329 if (!tok_jmp(TOK_DEFAULT
)) {
1334 for (i
= 0; i
< nmatched
; i
++)
1335 o_filljmp(matched
[i
]);
1339 o_rmlocal(val_addr
, 8);
1342 break_fill(o_mklabel(), break_beg
);
1345 static void readstmt(void)
1349 if (!tok_jmp('{')) {
1350 int _nlocals
= nlocals
;
1351 int _nglobals
= nglobals
;
1352 int _nenums
= nenums
;
1353 int _ntypedefs
= ntypedefs
;
1354 int _nstructs
= nstructs
;
1355 int _nfuncs
= nfuncs
;
1356 int _narrays
= narrays
;
1357 while (tok_jmp('}'))
1361 ntypedefs
= _ntypedefs
;
1362 nstructs
= _nstructs
;
1365 nglobals
= _nglobals
;
1368 if (!readdefs(localdef
, NULL
)) {
1372 if (!tok_jmp(TOK_TYPEDEF
)) {
1373 readdefs(typedefdef
, NULL
);
1377 if (!tok_jmp(TOK_IF
)) {
1384 if (!tok_jmp(TOK_ELSE
)) {
1394 if (!tok_jmp(TOK_WHILE
)) {
1396 int break_beg
= nbreaks
;
1397 int continue_beg
= ncontinues
;
1406 break_fill(o_mklabel(), break_beg
);
1407 continue_fill(l1
, continue_beg
);
1410 if (!tok_jmp(TOK_DO
)) {
1412 int break_beg
= nbreaks
;
1413 int continue_beg
= ncontinues
;
1416 tok_expect(TOK_WHILE
);
1422 break_fill(o_mklabel(), break_beg
);
1423 continue_fill(l2
, continue_beg
);
1426 if (!tok_jmp(TOK_FOR
)) {
1427 long check
, jump
, end
, body
;
1428 int break_beg
= nbreaks
;
1429 int continue_beg
= ncontinues
;
1431 if (tok_see() != ';')
1434 check
= o_mklabel();
1435 if (tok_see() != ';')
1441 if (tok_see() != ')')
1449 break_fill(o_mklabel(), break_beg
);
1450 continue_fill(jump
, continue_beg
);
1453 if (!tok_jmp(TOK_SWITCH
)) {
1457 if (!tok_jmp(TOK_RETURN
)) {
1458 int ret
= tok_see() != ';';
1465 if (!tok_jmp(TOK_BREAK
)) {
1467 breaks
[nbreaks
++] = o_jmp(0);
1470 if (!tok_jmp(TOK_CONTINUE
)) {
1472 continues
[ncontinues
++] = o_jmp(0);
1479 static void readdecl(void)
1481 if (!tok_jmp(TOK_TYPEDEF
)) {
1482 readdefs(typedefdef
, NULL
);
1486 readdefs(globaldef
, NULL
);
1487 if (tok_see() == '{') {
1496 static void parse(void)
1498 while (tok_see() != TOK_EOF
)
1502 int main(int argc
, char *argv
[])
1507 while (i
< argc
&& argv
[i
][0] == '-')
1510 die("no file given\n");
1511 ifd
= open(argv
[i
], O_RDONLY
);
1516 strcpy(obj
, argv
[i
]);
1517 obj
[strlen(obj
) - 1] = 'o';
1518 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);