commit 15c86d14159eae6ce5efd60a179f8d5bdf37f6a5
parent b2e6287e51525cb75388a7db2bf634f4f9725b6e
Author: erai <erai@omiltem.net>
Date: Tue, 17 Sep 2024 09:09:23 -0400
only output if compiling to object
Diffstat:
M | as.c | | | 2 | +- |
M | bufio.c | | | 19 | +++++++++++++++++++ |
M | cc1.c | | | 57 | ++++++++++++++++++++++++++++++++++++++++++++++++++------- |
M | node.c | | | 36 | ++++++++++++++++++------------------ |
4 files changed, 88 insertions(+), 26 deletions(-)
diff --git a/as.c b/as.c
@@ -825,7 +825,7 @@ writeout(c: *assembler, start: *label, kstart: *label) {
var mb_addr: int;
if (!c.out) {
- open_output(c, "a.out");
+ die("output not opened");
}
load_addr = 0x100000;
diff --git a/bufio.c b/bufio.c
@@ -168,6 +168,25 @@ fputb(f: *file, s: *byte, n: int) {
}
}
+fputd(out: *file, n: int) {
+ var a: int;
+
+ if (n < 0) {
+ fputc(out, '-');
+ a = -(n % 10);
+ n = n / -10;
+ } else {
+ a = n % 10;
+ n = n / 10;
+ }
+
+ if (n != 0) {
+ fputd(out, n);
+ }
+
+ fputc(out, '0' + a);
+}
+
fseek(f: *file, off: int) {
f.r = 0;
f.w = 0;
diff --git a/cc1.c b/cc1.c
@@ -15,6 +15,10 @@ struct compiler {
// Namespace
decls: *decl;
+
+ // C Output
+ do_cout: int;
+ cout: *file;
}
cshow_context(c: *compiler) {
@@ -54,9 +58,33 @@ comp_setup(a: *alloc): *compiler {
c.decls = 0:*decl;
+ c.do_cout = 0;
+ c.cout = 0:*file;
+
return c;
}
+open_coutput(c: *compiler, filename: *byte) {
+ var fd: int;
+
+ if (c.cout) {
+ die("multiple output files");
+ }
+
+ unlink(filename);
+
+ fd = open(filename, O_CREAT | O_WRONLY, (6 << 6) + (6 << 3) + 6);
+ if (fd < 0) {
+ die("failed to open output");
+ }
+
+ c.cout = fopen(fd, c.a);
+}
+
+flush_coutput(c: *compiler) {
+ fflush(c.cout);
+}
+
compile(c: *compiler, p: *node) {
var n: *node;
var d: *decl;
@@ -366,6 +394,8 @@ hoist_locals(c: *compiler, d: *decl, n: *node, offset: int): int {
name = n.a.s;
t = prototype(c, n.b);
+ n.t = t;
+
v = find(c, d.name, name, 1);
if (v.var_defined) {
@@ -1943,6 +1973,8 @@ main(argc: int, argv: **byte, envp: **byte) {
var kstart: *label;
var i: int;
var show: int;
+ var filename: *byte;
+ var err: *file;
setup_alloc(&a);
@@ -1950,6 +1982,8 @@ main(argc: int, argv: **byte, envp: **byte) {
show = 0;
+ filename = "a.out";
+
i = 1;
loop {
if (i >= argc) {
@@ -1961,7 +1995,7 @@ main(argc: int, argv: **byte, envp: **byte) {
if (i >= argc) {
die("invalid -o at end of argument list");
}
- open_output(c.as, argv[i]);
+ filename = argv[i];
i = i + 1;
continue;
}
@@ -1973,11 +2007,7 @@ main(argc: int, argv: **byte, envp: **byte) {
}
if (!strcmp(argv[i], "-C")) {
- i = i + 1;
- if (i >= argc) {
- die("invalid -C at end of argument list");
- }
- //open_coutput(c.as, argv[i]);
+ c.do_cout = 1;
i = i + 1;
continue;
}
@@ -1992,11 +2022,22 @@ main(argc: int, argv: **byte, envp: **byte) {
}
if show {
- show_node(p);
+ err = fopen(2, &a);
+ show_node(err, p);
+ fflush(err);
+ return;
}
compile(c, p);
+ if c.do_cout {
+ open_coutput(c, filename);
+
+ ctranslate(c);
+
+ return;
+ }
+
emit_builtin(c);
start = 0: *label;
@@ -2011,5 +2052,7 @@ main(argc: int, argv: **byte, envp: **byte) {
kstart = d.func_label;
}
+ open_output(c.as, filename);
+
writeout(c.as, start, kstart);
}
diff --git a/node.c b/node.c
@@ -182,7 +182,7 @@ node_to_str(kind: int): *byte {
return "(invalid)";
}
-show_node(n: *node) {
+show_node(out: *file, n: *node) {
var i: int;
var ch: int;
var hex: *byte;
@@ -190,15 +190,15 @@ show_node(n: *node) {
if !n {
return;
}
- fdputc(2, '(');
- fdputs(2, node_to_str(n.kind));
+ fputc(out, '(');
+ fputs(out, node_to_str(n.kind));
if n.kind == N_NUM {
- fdputc(2, ' ');
- fdputd(2, n.n);
+ fputc(out, ' ');
+ fputd(out, n.n);
}
if n.s {
- fdputc(2, ' ');
- fdputc(2, '"');
+ fputc(out, ' ');
+ fputc(out, '"');
i = 0;
loop {
ch = n.s[i]:int;
@@ -206,24 +206,24 @@ show_node(n: *node) {
break;
}
if ch < 32 || ch > 127 || ch == '\\' || ch == '"' {
- fdputc(2, '\\');
- fdputc(2, 'x');
- fdputc(2, hex[ch >> 4]:int);
- fdputc(2, hex[ch & 15]:int);
+ fputc(out, '\\');
+ fputc(out, 'x');
+ fputc(out, hex[ch >> 4]:int);
+ fputc(out, hex[ch & 15]:int);
} else {
- fdputc(2, ch);
+ fputc(out, ch);
}
i = i + 1;
}
- fdputc(2, '"');
+ fputc(out, '"');
}
if n.a {
- fdputc(2, ' ');
- show_node(n.a);
+ fputc(out, ' ');
+ show_node(out, n.a);
}
if n.b {
- fdputc(2, ' ');
- show_node(n.b);
+ fputc(out, ' ');
+ show_node(out, n.b);
}
- fdputc(2, ')');
+ fputc(out, ')');
}