Skip to content

Commit 33fcb55

Browse files
committed
A few more fixes to get test-stage2 working with nasm
It seems that nasm confuses references to an extern named "wait" with the wait instruction (at least in some contexts) so add a workaround for that for now (compiled by host compiler). Posted to nasm-devel, but I'm not sure about a patch to nasm yet. Additionally fix an error in common symbols (two statics named output_file, oops!) and few more att<->nasm syntax changes and now test-stage2 is all working.
1 parent d2fae63 commit 33fcb55

File tree

4 files changed

+37
-30
lines changed

4 files changed

+37
-30
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CFLAGS=-std=c11 -g -fno-common -Wall -Wno-switch
22

3-
SRCS=$(wildcard *.c)
3+
SRCS=codegen.c hashmap.c main.c parse.c preprocess.c strings.c tokenize.c type.c unicode.c
44
OBJS=$(SRCS:.c=.o)
55

66
TEST_SRCS=$(wildcard test/*.c)
@@ -9,13 +9,13 @@ TESTS=$(TEST_SRCS:.c=.exe)
99
# Stage 1
1010

1111
chibicc: $(OBJS)
12-
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
12+
$(CC) $(CFLAGS) -o $@ $^ wait_hack.c $(LDFLAGS)
1313

1414
$(OBJS): chibicc.h
1515

1616
test/%.exe: chibicc test/%.c
1717
./chibicc -Iinclude -Itest -c -o test/$*.o test/$*.c
18-
$(CC) -pthread -o $@ test/$*.o -xc test/common
18+
$(CC) -pthread -o $@ test/$*.o wait_hack.c -xc test/common
1919

2020
test: $(TESTS)
2121
for i in $^; do echo $$i; ./$$i || exit 1; echo; done
@@ -26,7 +26,7 @@ test-all: test test-stage2
2626
# Stage 2
2727

2828
stage2/chibicc: $(OBJS:%=stage2/%)
29-
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
29+
$(CC) $(CFLAGS) -o $@ wait_hack.c $^ $(LDFLAGS)
3030

3131
stage2/%.o: chibicc %.c
3232
mkdir -p stage2/test

codegen.c

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ static char f80i32[] = FROM_F80_1 "fistp dword" FROM_F80_2 "mov eax, [rsp-24]";
390390
static char f80u32[] = FROM_F80_1 "fistp dword" FROM_F80_2 "mov eax, [rsp-24]";
391391
static char f80i64[] = FROM_F80_1 "fistp qword" FROM_F80_2 "mov rax, [rsp-24]";
392392
static char f80u64[] = FROM_F80_1 "fistp qword" FROM_F80_2 "mov rax, [rsp-24]";
393-
static char f80f32[] = "fstps [rsp-8]\n movss xmm0, [rsp-8]";
393+
static char f80f32[] = "fstp dword [rsp-8]\n movss xmm0, [rsp-8]";
394394
static char f80f64[] = "fstp qword [rsp-8]\n movsd xmm0, [rsp-8]";
395395

396396
static char *cast_table[][11] = {
@@ -1425,36 +1425,35 @@ static void emit_data(Obj *prog) {
14251425
continue;
14261426
}
14271427

1428-
if (!(opt_fcommon && var->is_tentative)) {
1429-
if (var->is_static)
1430-
println(" static %s:data", var->name);
1431-
else
1432-
println(" global %s:data", var->name);
1433-
}
1428+
if (var->is_static)
1429+
println(" static %s:data", var->name);
1430+
else if (!(opt_fcommon && var->is_tentative))
1431+
println(" global %s:data", var->name);
14341432

14351433
int align = (var->ty->kind == TY_ARRAY && var->ty->size >= 16)
1436-
? MAX(16, var->align) : var->align;
1434+
? MAX(16, var->align)
1435+
: var->align;
14371436

14381437
// Common symbol
1439-
if (opt_fcommon && var->is_tentative) {
1438+
if (opt_fcommon && var->is_tentative && !var->is_static) {
14401439
println(" common %s %d:%d", var->name, var->ty->size, align);
14411440
continue;
14421441
}
14431442

14441443
// .data or .tdata
14451444
if (var->init_data) {
14461445
if (var->is_tls)
1447-
println(" section .tdata"); // TODO: ,\"awT\",@progbits");
1446+
println(" section .tdata"); // TODO: ,\"awT\",@progbits");
14481447
else
1449-
println(" section .data");
1448+
println(" section .data align=%d", align);
14501449

1451-
//TODO: necessary?
1452-
//println(" .type %s, @object", var->name);
1453-
//println(" .size %s, %d", var->name, var->ty->size);
1454-
println(" align %d", align);
1450+
// TODO: necessary?
1451+
// println(" .type %s, @object", var->name);
1452+
// println(" .size %s, %d", var->name, var->ty->size);
1453+
// println(" align %d", align);
14551454
println("%s:", var->name);
14561455

1457-
Relocation *rel = var->rel;
1456+
Relocation* rel = var->rel;
14581457
int pos = 0;
14591458
while (pos < var->ty->size) {
14601459
if (rel && rel->offset == pos) {
@@ -1472,9 +1471,9 @@ static void emit_data(Obj *prog) {
14721471
if (var->is_tls)
14731472
println(" section .tbss"); // TODO: ,\"awT\",@nobits");
14741473
else
1475-
println(" section .bss");
1474+
println(" section .bss align=%d", align);
14761475

1477-
println(" align %d", align);
1476+
// println(" align %d", align);
14781477
println("%s:", var->name);
14791478
println(" resb %d", var->ty->size);
14801479
}
@@ -1498,7 +1497,7 @@ static void store_gp(int r, int offset, int sz) {
14981497
println(" mov [rbp+%d], %s", offset, argreg8[r]);
14991498
return;
15001499
case 2:
1501-
println(" mov [rbp+%d], %s", offset, argreg16[r]);
1500+
println(" mov [rbp+%d], %s", offset, argreg16[r]);
15021501
return;
15031502
case 4:
15041503
println(" mov [rbp+%d], %s", offset, argreg32[r]);
@@ -1516,7 +1515,7 @@ static void store_gp(int r, int offset, int sz) {
15161515
}
15171516

15181517
static void emit_text(Obj *prog) {
1519-
for (Obj *fn = prog; fn; fn = fn->next) {
1518+
for (Obj* fn = prog; fn; fn = fn->next) {
15201519
if (!fn->is_function)
15211520
continue;
15221521

@@ -1536,8 +1535,8 @@ static void emit_text(Obj *prog) {
15361535
println(" global %s:function", fn->name);
15371536

15381537
println(" section .text");
1539-
//TODO: necessary?
1540-
//println(" .type %s, @function", fn->name);
1538+
// TODO: necessary?
1539+
// println(" .type %s, @function", fn->name);
15411540
println("%s:", fn->name);
15421541
current_fn = fn;
15431542

@@ -1550,7 +1549,7 @@ static void emit_text(Obj *prog) {
15501549
// Save arg registers if function is variadic
15511550
if (fn->va_area) {
15521551
int gp = 0, fp = 0;
1553-
for (Obj *var = fn->params; var; var = var->next) {
1552+
for (Obj* var = fn->params; var; var = var->next) {
15541553
if (is_flonum(var->ty))
15551554
fp++;
15561555
else
@@ -1560,8 +1559,8 @@ static void emit_text(Obj *prog) {
15601559
int off = fn->va_area->offset;
15611560

15621561
// va_elem
1563-
println(" mov dword [rbp+%d], %d", off, gp * 8); // gp_offset
1564-
println(" mov dword [rbp+%d], %d", off + 4, fp * 8 + 48); // fp_offset
1562+
println(" mov dword [rbp+%d], %d", off, gp * 8); // gp_offset
1563+
println(" mov dword [rbp+%d], %d", off + 4, fp * 8 + 48); // fp_offset
15651564
println(" mov [rbp+%d], rbp", off + 8); // overflow_arg_area
15661565
println(" add qword [rbp+%d], 16", off + 8);
15671566
println(" mov [rbp+%d], rbp", off + 16); // reg_save_area

main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ int main(int argc, char **argv) {
100100
#include <sys/wait.h>
101101
#include <unistd.h>
102102

103+
// Workaround for nasm issue referring to "wait".
104+
pid_t MYwait(int *wstatus);
105+
103106
typedef enum {
104107
FILE_NONE, FILE_C, FILE_ASM, FILE_OBJ, FILE_AR, FILE_DSO,
105108
} FileType;
@@ -506,7 +509,7 @@ static void run_subprocess(char **argv) {
506509

507510
// Wait for the child process to finish.
508511
int status;
509-
while (wait(&status) > 0);
512+
while (MYwait(&status) > 0);
510513
if (status != 0)
511514
exit(1);
512515
}

wait_hack.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <sys/wait.h>
2+
3+
pid_t MYwait(int *wstatus) {
4+
return wait(wstatus);
5+
}

0 commit comments

Comments
 (0)