commit bf3ea81e5285fe18edfbd74ac05dfb361f83f003
parent 7ed645936f04bb2db8a3670eb92f6145f66f2df3
Author: erai <erai@omiltem.net>
Date: Fri, 24 Jan 2025 15:45:18 +0000
Add lines output to debug segv
Diffstat:
M | .gitignore | | | 1 | + |
M | as.om | | | 40 | ++++++++++++++++++++++++++++++++++++++++ |
M | bootstrap.sh | | | 4 | ++-- |
M | bufio.om | | | 19 | +++++++++++++++++++ |
M | build.sh | | | 24 | ++++++++++++------------ |
M | cc0.c | | | 163 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- |
M | cc1.om | | | 45 | +++++++++++++++++++++++++++++++++++++++------ |
M | cpio.om | | | 54 | +++++++++++++++++++++++++++--------------------------- |
8 files changed, 296 insertions(+), 54 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -25,3 +25,4 @@ peg
peg0
*.out*
*_peg.c
+*.lines
diff --git a/as.om b/as.om
@@ -176,6 +176,7 @@ struct section {
struct assembler {
a: *alloc;
out: *file;
+ lineout: *file;
at: int;
text: *chunk;
text_end: *chunk;
@@ -183,6 +184,11 @@ struct assembler {
symbols: *symbol;
sections: *section;
num_sections: int;
+ lines_out: *file;
+ filename: *byte;
+ lineno: int;
+ prevfilename: *byte;
+ prevlineno: int;
}
struct symbol {
@@ -326,8 +332,38 @@ func reserve(c: *assembler, n: int) {
}
}
+func emit_hook(c: *assembler) {
+ if !c.lines_out {
+ c.prevlineno = c.lineno;
+ c.prevfilename = c.filename;
+ }
+
+ if (
+ c.lineno == c.prevlineno
+ && (
+ c.filename == c.prevfilename
+ || (c.filename && c.prevfilename && !strcmp(c.filename, c.prevfilename))
+ )
+ ) {
+ return;
+ }
+
+ fputh(c.lines_out, c.at + 0x100000);
+ fputs(c.lines_out, "\t");
+ if c.filename {
+ fputs(c.lines_out, c.filename);
+ }
+ fputs(c.lines_out, ":");
+ fputd(c.lines_out, c.lineno);
+ fputs(c.lines_out, "\n");
+
+ c.prevlineno = c.lineno;
+ c.prevfilename = c.filename;
+}
+
// Add a single byte to the output
func emit(c: *assembler, x: int) {
+ emit_hook(c);
reserve(c, 1);
c.text_end.buf[c.text_end.fill] = x as byte;
c.text_end.fill = c.text_end.fill + 1;
@@ -1477,6 +1513,10 @@ func writeout(c: *assembler, start: *label, kstart: *label) {
}
fflush(c.out);
+
+ if c.lines_out {
+ fflush(c.lines_out);
+ }
}
func as_emit(a: *assembler, b: int) {
diff --git a/bootstrap.sh b/bootstrap.sh
@@ -8,10 +8,10 @@ SOURCES="cc1.om type.om parse2.om parse3.om peglib.om as.om decl.om node.om cout
gcc -std=c99 ${BOOTSTRAP} -o cc0
./cc0 -P P_ cc3.peg -o parse3.om
./cc0 -P PEG_ peg.peg -o parsepeg.om
-./cc0 ${LIBS} ${SOURCES} -o cc1
+./cc0 ${LIBS} ${SOURCES} -o cc1 -n cc1.lines
# Double check the bootstrap and self hosting compiler have the same output
-./cc1 ${LIBS} ${SOURCES} -o cc2
+./cc1 ${LIBS} ${SOURCES} -o cc2 -n cc2.lines
cmp cc1 cc2 || echo cc mismatch
# Verify the saved bootstrap matches
diff --git a/bufio.om b/bufio.om
@@ -87,6 +87,11 @@ func ffill(f: *file): void {
}
func fputc(f: *file, ch: int): void {
+ if !f {
+ fdputc(1, ch);
+ return;
+ }
+
if (f.w == f.cap) {
fflush(f);
}
@@ -187,6 +192,20 @@ func fputd(out: *file, n: int) {
fputc(out, '0' + a);
}
+func fputh(out: *file, n: int) {
+ var d: int;
+
+ d = n & 15;
+
+ n = n >> 4;
+
+ if n {
+ fputh(out, n);
+ }
+
+ fputc(out, "0123456789abcdef"[d] as int);
+}
+
func fseek(f: *file, off: int) {
f.r = 0;
f.w = 0;
diff --git a/build.sh b/build.sh
@@ -10,18 +10,18 @@ SHELL="echo.om cmp.om rm.om ls.om cat.om xxd.om mv.om mkdir.om cpio.om sh.om"
BIN="echo cmp rm ls cat xxd mv mkdir cpio sh sshd init cc1 build.sh peg.peg cc3.peg"
ALL="${LIBS} ${CC} ${PEG} ${SSHD} ${KERNEL} ${SHELL} ${BIN}"
-./cc1 ${LIBS} echo.om -o echo
-./cc1 ${LIBS} cmp.om -o cmp
-./cc1 ${LIBS} rm.om -o rm
-./cc1 ${LIBS} mv.om -o mv
-./cc1 ${LIBS} mkdir.om -o mkdir
-./cc1 ${LIBS} ls.om -o ls
-./cc1 ${LIBS} cat.om -o cat
-./cc1 ${LIBS} xxd.om -o xxd
-./cc1 ${LIBS} cpio.om -o cpio
-./cc1 ${LIBS} sh.om -o sh
-./cc1 ${LIBS} ${CRYPTO} sshd.om -o sshd
+./cc1 ${LIBS} echo.om -o echo -n echo.lines
+./cc1 ${LIBS} cmp.om -o cmp -n cmp.lines
+./cc1 ${LIBS} rm.om -o rm -n rm.lines
+./cc1 ${LIBS} mv.om -o mv -n mv.lines
+./cc1 ${LIBS} mkdir.om -o mkdir -n mkdir.lines
+./cc1 ${LIBS} ls.om -o ls -n ls.lines
+./cc1 ${LIBS} cat.om -o cat -n cat.lines
+./cc1 ${LIBS} xxd.om -o xxd -n xxd.lines
+./cc1 ${LIBS} cpio.om -o cpio -n cpio.lines
+./cc1 ${LIBS} sh.om -o sh -n sh.lines
+./cc1 ${LIBS} ${CRYPTO} sshd.om -o sshd -n sshd.lines
for name in ${ALL}; do echo ${name}; done | ./cpio -o > initramfs
-./cc1 kernel.om -o kernel
+./cc1 kernel.om -o kernel -n kernel.lines
diff --git a/cc0.c b/cc0.c
@@ -11,6 +11,12 @@ struct my_compiler;
struct my_decl;
struct my_file;
struct my_fixup;
+struct my_irblock;
+struct my_ircomp;
+struct my_irlabel;
+struct my_irloopctx;
+struct my_irop;
+struct my_irvar;
struct my_label;
struct my_name_node;
struct my_node;
@@ -31,6 +37,7 @@ struct my_alloc {
struct my_assembler {
struct my_alloc* my_a;
struct my_file* my_out;
+ struct my_file* my_lineout;
unsigned long my_at;
struct my_chunk* my_text;
struct my_chunk* my_text_end;
@@ -38,6 +45,11 @@ struct my_assembler {
struct my_symbol* my_symbols;
struct my_section* my_sections;
unsigned long my_num_sections;
+ struct my_file* my_lines_out;
+ unsigned char* my_filename;
+ unsigned long my_lineno;
+ unsigned char* my_prevfilename;
+ unsigned long my_prevlineno;
};
struct my_chunk {
struct my_chunk* my_next;
@@ -105,6 +117,47 @@ struct my_fixup {
unsigned char* my_ptr;
unsigned long my_at;
};
+struct my_irblock {
+ struct my_irop* my_ops;
+ unsigned long my_ops_len;
+ unsigned long my_ops_cap;
+ unsigned long my_done;
+ struct my_irblock* my_out;
+ struct my_irblock* my_alt;
+};
+struct my_ircomp {
+ struct my_compiler* my_c;
+ struct my_alloc* my_a;
+ struct my_irloopctx* my_loopctx;
+ struct my_irblock* my_top;
+ struct my_irblock* my_cur;
+ struct my_irlabel* my_labels;
+ struct my_irvar* my_vars;
+};
+struct my_irlabel {
+ unsigned char* my_name;
+ struct my_irlabel* my_left;
+ struct my_irlabel* my_right;
+ struct my_irblock* my_block;
+};
+struct my_irloopctx {
+ struct my_irloopctx* my_up;
+ struct my_irblock* my_top;
+ struct my_irblock* my_out;
+};
+struct my_irop {
+ unsigned long my_code;
+ struct my_irop* my_a;
+ struct my_irop* my_b;
+ unsigned long my_n;
+ unsigned char* my_s;
+ struct my_type* my_t;
+};
+struct my_irvar {
+ unsigned char* my_name;
+ struct my_irvar* my_left;
+ struct my_irvar* my_right;
+};
struct my_label {
struct my_fixup* my_fix;
unsigned long my_at;
@@ -244,6 +297,36 @@ enum {
my_EINTR = 4,
my_EPIPE = 32,
my_EXACTLY_ONE = 1,
+ my_IOP_ADD = 11,
+ my_IOP_AND = 12,
+ my_IOP_ARG = 7,
+ my_IOP_BRANCH = 28,
+ my_IOP_CALL = 8,
+ my_IOP_CONST = 2,
+ my_IOP_DIV = 15,
+ my_IOP_EQ = 21,
+ my_IOP_FUNC = 1,
+ my_IOP_GE = 24,
+ my_IOP_GT = 23,
+ my_IOP_JUMP = 27,
+ my_IOP_LE = 26,
+ my_IOP_LOAD = 4,
+ my_IOP_LSH = 17,
+ my_IOP_LT = 25,
+ my_IOP_MOD = 16,
+ my_IOP_MUL = 19,
+ my_IOP_NE = 22,
+ my_IOP_NEG = 9,
+ my_IOP_NOT = 10,
+ my_IOP_OR = 13,
+ my_IOP_REF = 6,
+ my_IOP_RETURN = 29,
+ my_IOP_RSH = 18,
+ my_IOP_STORE = 5,
+ my_IOP_STR = 3,
+ my_IOP_SUB = 20,
+ my_IOP_VAR = 0,
+ my_IOP_XOR = 14,
my_LOOK_AND = 2,
my_LOOK_NORMAL = 0,
my_LOOK_NOT = 1,
@@ -621,6 +704,7 @@ void( my_emit_div)(struct my_assembler* my_c);
void( my_emit_eq)(struct my_assembler* my_c);
void( my_emit_ge)(struct my_assembler* my_c);
void( my_emit_gt)(struct my_assembler* my_c);
+void( my_emit_hook)(struct my_assembler* my_c);
void( my_emit_isr)(struct my_compiler* my_c);
void( my_emit_jmp)(struct my_assembler* my_c,struct my_label* my_l);
void( my_emit_jz)(struct my_assembler* my_c,struct my_label* my_l);
@@ -674,6 +758,7 @@ struct my_file*( my_fopen)(unsigned long my_fd,struct my_alloc* my_a);
void( my_fputb)(struct my_file* my_f,unsigned char* my_s,unsigned long my_n);
void( my_fputc)(struct my_file* my_f,unsigned long my_ch);
void( my_fputd)(struct my_file* my_out,unsigned long my_n);
+void( my_fputh)(struct my_file* my_out,unsigned long my_n);
void( my_fputs)(struct my_file* my_f,unsigned char* my_s);
unsigned char*( my_freadall)(struct my_file* my_f,unsigned long* my_size);
void( my_free)(struct my_alloc* my_a,unsigned char* my_p);
@@ -706,6 +791,7 @@ struct my_decl*( my_next_decl)(struct my_compiler* my_c,struct my_decl* my_d);
unsigned char*( my_node_to_str)(unsigned long my_kind);
unsigned long( my_open)(unsigned char* my_name,unsigned long my_flags,unsigned long my_mode);
void( my_open_coutput)(struct my_compiler* my_c,unsigned char* my_filename);
+void( my_open_lines_out)(struct my_compiler* my_c,unsigned char* my_filename);
void( my_open_output)(struct my_assembler* my_c,unsigned char* my_filename);
struct my_node*( my_parse)(struct my_parser* my_c,unsigned char* my_filename);
unsigned long( my_parse_escape)(unsigned char* my_s,unsigned long* my_i,unsigned long my_n);
@@ -886,6 +972,7 @@ void( my_typecheck_stmt)(struct my_compiler* my_c,struct my_decl* my_d,struct my
unsigned long( my_unescape)(unsigned char* my_s,unsigned long* my_i,unsigned long my_len,unsigned long* my_ok);
void( my_unify)(struct my_compiler* my_c,struct my_type* my_a,struct my_type* my_b);
unsigned long( my_unlink)(unsigned char* my_name);
+void( my_update_place)(struct my_compiler* my_c,struct my_node* my_n);
unsigned long( my_write)(unsigned long my_fd,unsigned char* my_buf,unsigned long my_n);
void( my_writeout)(struct my_assembler* my_c,struct my_label* my_start,struct my_label* my_kstart);
unsigned char*( my_PEG_tag_to_str)(unsigned long my_tag){
@@ -1714,9 +1801,7 @@ void( my_compile_expr)(struct my_compiler* my_c,struct my_decl* my_d,struct my_n
struct my_label* my_out = 0;
struct my_decl* my_v = 0;
unsigned long my_kind = 0;
- ((my_c)->my_filename)=((my_n)->my_filename);
- ((my_c)->my_lineno)=((my_n)->my_lineno);
- ((my_c)->my_colno)=((my_n)->my_colno);
+ (my_update_place)((my_c),(my_n));
(my_kind)=((my_n)->my_kind);
if ((unsigned long)(((long)(my_kind))==((long)(my_N_STR)))) {
(my_emit_str)(((my_c)->my_s),((my_n)->my_s));
@@ -1991,9 +2076,7 @@ void( my_compile_stmt)(struct my_compiler* my_c,struct my_decl* my_d,struct my_n
if ((unsigned long)(!(my_n))) {
return;
}
- ((my_c)->my_filename)=((my_n)->my_filename);
- ((my_c)->my_lineno)=((my_n)->my_lineno);
- ((my_c)->my_colno)=((my_n)->my_colno);
+ (my_update_place)((my_c),(my_n));
(my_kind)=((my_n)->my_kind);
if ((unsigned long)(((long)(my_kind))==((long)(my_N_CONDLIST)))) {
(my_ifout)=((my_mklabel)(((my_c)->my_s)));
@@ -2886,6 +2969,8 @@ void( my_defun)(struct my_compiler* my_c,struct my_node* my_n){
((my_c)->my_filename)=(((my_n)->my_a)->my_filename);
((my_c)->my_lineno)=(((my_n)->my_a)->my_lineno);
((my_c)->my_colno)=(((my_n)->my_a)->my_colno);
+ (((my_c)->my_s)->my_filename)=((my_n)->my_filename);
+ (((my_c)->my_s)->my_lineno)=((my_n)->my_lineno);
(my_name)=((((my_n)->my_a)->my_a)->my_s);
(my_t)=((my_prototype)((my_c),(((my_n)->my_a)->my_b)));
(my_v)=((my_find)((my_c),((my_d)->my_name),(my_name),(1UL)));
@@ -2925,6 +3010,7 @@ void( my_die)(unsigned char* my_msg){
(my_exit)((1UL));
}
void( my_emit)(struct my_assembler* my_c,unsigned long my_x){
+ (my_emit_hook)((my_c));
(my_reserve)((my_c),(1UL));
((((my_c)->my_text_end)->my_buf)[((my_c)->my_text_end)->my_fill])=((unsigned char)my_x);
(((my_c)->my_text_end)->my_fill)=((unsigned long)(((unsigned long)(((my_c)->my_text_end)->my_fill))+((unsigned long)(1UL))));
@@ -3543,6 +3629,25 @@ void( my_emit_gt)(struct my_assembler* my_c){
(my_as_modrr)((my_c),((unsigned long)(((unsigned long)(my_OP_SETCC))+((unsigned long)(my_CC_G)))),(0UL),(my_R_RAX));
(my_as_opr)((my_c),(my_OP_PUSHR),(my_R_RAX));
}
+void( my_emit_hook)(struct my_assembler* my_c){
+ if ((unsigned long)(!((my_c)->my_lines_out))) {
+ ((my_c)->my_prevlineno)=((my_c)->my_lineno);
+ ((my_c)->my_prevfilename)=((my_c)->my_filename);
+ }
+ if ((unsigned long)(((unsigned long)(((long)((my_c)->my_lineno))==((long)((my_c)->my_prevlineno))))&&((unsigned long)(((unsigned long)(((long)((my_c)->my_filename))==((long)((my_c)->my_prevfilename))))||((unsigned long)(((my_c)->my_filename)&&((unsigned long)(((my_c)->my_prevfilename)&&((unsigned long)(!((my_strcmp)(((my_c)->my_filename),((my_c)->my_prevfilename))))))))))))) {
+ return;
+ }
+ (my_fputh)(((my_c)->my_lines_out),((unsigned long)(((unsigned long)((my_c)->my_at))+((unsigned long)(1048576UL)))));
+ (my_fputs)(((my_c)->my_lines_out),((unsigned char *)"\011"));
+ if ((my_c)->my_filename) {
+ (my_fputs)(((my_c)->my_lines_out),((my_c)->my_filename));
+ }
+ (my_fputs)(((my_c)->my_lines_out),((unsigned char *)":"));
+ (my_fputd)(((my_c)->my_lines_out),((my_c)->my_lineno));
+ (my_fputs)(((my_c)->my_lines_out),((unsigned char *)"\012"));
+ ((my_c)->my_prevlineno)=((my_c)->my_lineno);
+ ((my_c)->my_prevfilename)=((my_c)->my_filename);
+}
void( my_emit_isr)(struct my_compiler* my_c){
struct my_decl* my_d = 0;
struct my_label* my_out = 0;
@@ -4501,6 +4606,10 @@ void( my_fputb)(struct my_file* my_f,unsigned char* my_s,unsigned long my_n){
}
}
void( my_fputc)(struct my_file* my_f,unsigned long my_ch){
+ if ((unsigned long)(!(my_f))) {
+ (my_fdputc)((1UL),(my_ch));
+ return;
+ }
if ((unsigned long)(((long)((my_f)->my_w))==((long)((my_f)->my_cap)))) {
(my_fflush)((my_f));
}
@@ -4525,6 +4634,15 @@ void( my_fputd)(struct my_file* my_out,unsigned long my_n){
}
(my_fputc)((my_out),((unsigned long)(((unsigned long)(48))+((unsigned long)(my_a)))));
}
+void( my_fputh)(struct my_file* my_out,unsigned long my_n){
+ unsigned long my_d = 0;
+ (my_d)=((unsigned long)(((unsigned long)(my_n))&((unsigned long)(15UL))));
+ (my_n)=((unsigned long)(((unsigned long)(my_n))>>((unsigned long)(4UL))));
+ if (my_n) {
+ (my_fputh)((my_out),(my_n));
+ }
+ (my_fputc)((my_out),((unsigned long)((unsigned char *)"0123456789abcdef")[my_d]));
+}
void( my_fputs)(struct my_file* my_f,unsigned char* my_s){
unsigned long my_i = 0;
(my_i)=(0UL);
@@ -4864,6 +4982,15 @@ void( my_main)(unsigned long my_argc,unsigned char** my_argv,unsigned char** my_
(my_i)=((unsigned long)(((unsigned long)(my_i))+((unsigned long)(1UL))));
continue;
}
+ if ((unsigned long)(!((my_strcmp)(((my_argv)[my_i]),((unsigned char *)"-n"))))) {
+ (my_i)=((unsigned long)(((unsigned long)(my_i))+((unsigned long)(1UL))));
+ if ((unsigned long)(((long)(my_i))>=((long)(my_argc)))) {
+ (my_die)(((unsigned char *)"invalid -P at end of argument list"));
+ }
+ (my_open_lines_out)((my_c),((my_argv)[my_i]));
+ (my_i)=((unsigned long)(((unsigned long)(my_i))+((unsigned long)(1UL))));
+ continue;
+ }
if ((unsigned long)(!((my_strcmp)(((my_argv)[my_i]),((unsigned char *)"-P"))))) {
(my_i)=((unsigned long)(((unsigned long)(my_i))+((unsigned long)(1UL))));
if ((unsigned long)(((long)(my_i))>=((long)(my_argc)))) {
@@ -5334,6 +5461,15 @@ void( my_open_coutput)(struct my_compiler* my_c,unsigned char* my_filename){
}
((my_c)->my_cout)=((my_fopen)((my_fd),((my_c)->my_a)));
}
+void( my_open_lines_out)(struct my_compiler* my_c,unsigned char* my_filename){
+ unsigned long my_fd = 0;
+ (my_unlink)((my_filename));
+ (my_fd)=((my_open)((my_filename),((unsigned long)(((unsigned long)(my_O_CREAT))|((unsigned long)(my_O_WRONLY)))),((unsigned long)(((unsigned long)((unsigned long)(((unsigned long)((unsigned long)(((unsigned long)(6UL))<<((unsigned long)(6UL)))))+((unsigned long)((unsigned long)(((unsigned long)(6UL))<<((unsigned long)(3UL))))))))+((unsigned long)(6UL))))));
+ if ((unsigned long)(((long)(my_fd))<((long)(0UL)))) {
+ (my_die)(((unsigned char *)"failed to open output"));
+ }
+ (((my_c)->my_s)->my_lines_out)=((my_fopen)((my_fd),((my_c)->my_a)));
+}
void( my_open_output)(struct my_assembler* my_c,unsigned char* my_filename){
unsigned long my_fd = 0;
if ((my_c)->my_out) {
@@ -9374,7 +9510,7 @@ void( my_setup_alloc)(struct my_alloc* my_c){
}
struct my_assembler*( my_setup_assembler)(struct my_alloc* my_a){
struct my_assembler* my_c = 0;
- (my_c)=((struct my_assembler*)(my_alloc)((my_a),(72UL)));
+ (my_c)=((struct my_assembler*)(my_alloc)((my_a),(120UL)));
((my_c)->my_a)=(my_a);
((my_c)->my_out)=((void *)0);
((my_c)->my_at)=(160UL);
@@ -10338,6 +10474,16 @@ void( my_unify)(struct my_compiler* my_c,struct my_type* my_a,struct my_type* my
unsigned long( my_unlink)(unsigned char* my_name){
return (my_syscall)((87UL),((unsigned long)my_name),(0UL),(0UL),(0UL),(0UL),(0UL));
}
+void( my_update_place)(struct my_compiler* my_c,struct my_node* my_n){
+ if ((unsigned long)(!(my_n))) {
+ return;
+ }
+ ((my_c)->my_filename)=((my_n)->my_filename);
+ ((my_c)->my_lineno)=((my_n)->my_lineno);
+ ((my_c)->my_colno)=((my_n)->my_colno);
+ (((my_c)->my_s)->my_filename)=((my_n)->my_filename);
+ (((my_c)->my_s)->my_lineno)=((my_n)->my_lineno);
+}
unsigned long( my_write)(unsigned long my_fd,unsigned char* my_buf,unsigned long my_n){
return (my_syscall)((1UL),(my_fd),((unsigned long)my_buf),(my_n),(0UL),(0UL),(0UL));
}
@@ -10557,4 +10703,7 @@ void( my_writeout)(struct my_assembler* my_c,struct my_label* my_start,struct my
(my_b)=((my_b)->my_next);
}
(my_fflush)(((my_c)->my_out));
+ if ((my_c)->my_lines_out) {
+ (my_fflush)(((my_c)->my_lines_out));
+ }
}
diff --git a/cc1.om b/cc1.om
@@ -191,6 +191,7 @@ func compile(c: *compiler, p: *node) {
}
if d.func_used && d.func_defined {
+ //func_to_ir(c, d.func_def);
compile_func(c, d);
}
@@ -387,6 +388,8 @@ func defun(c: *compiler, n: *node) {
c.filename = n.a.filename;
c.lineno = n.a.lineno;
c.colno = n.a.colno;
+ c.s.filename = n.filename;
+ c.s.lineno = n.lineno;
name = n.a.a.s;
t = prototype(c, n.a.b);
@@ -1333,9 +1336,7 @@ func compile_expr(c: *compiler, d: *decl, n: *node, rhs: int) {
var v: *decl;
var kind: int;
- c.filename = n.filename;
- c.lineno = n.lineno;
- c.colno = n.colno;
+ update_place(c, n);
kind = n.kind;
if (kind == N_STR) {
@@ -1645,6 +1646,17 @@ func call_check(c: *compiler, n: *node): int {
return result;
}
+func update_place(c: *compiler, n: *node) {
+ if !n {
+ return;
+ }
+ c.filename = n.filename;
+ c.lineno = n.lineno;
+ c.colno = n.colno;
+ c.s.filename = n.filename;
+ c.s.lineno = n.lineno;
+}
+
// Compile a statement
func compile_stmt(c: *compiler, d: *decl, n: *node, top: *label, out: *label) {
var no: *label;
@@ -1656,9 +1668,7 @@ func compile_stmt(c: *compiler, d: *decl, n: *node, top: *label, out: *label) {
return;
}
- c.filename = n.filename;
- c.lineno = n.lineno;
- c.colno = n.colno;
+ update_place(c, n);
kind = n.kind;
if (kind == N_CONDLIST) {
@@ -2564,6 +2574,19 @@ func emit_builtin(c: *compiler) {
}
}
+func open_lines_out(c: *compiler, filename: *byte) {
+ var fd: int;
+
+ unlink(filename);
+
+ fd = open(filename, O_CREAT | O_WRONLY, (6 << 6) + (6 << 3) + 6);
+ if (fd < 0) {
+ die("failed to open output");
+ }
+
+ c.s.lines_out = fopen(fd, c.a);
+}
+
struct name_node {
next: *name_node;
name: *byte;
@@ -2620,6 +2643,16 @@ func main(argc: int, argv: **byte, envp: **byte) {
continue;
}
+ if (!strcmp(argv[i], "-n")) {
+ i = i + 1;
+ if (i >= argc) {
+ die("invalid -P at end of argument list");
+ }
+ open_lines_out(c, argv[i]);
+ i = i + 1;
+ continue;
+ }
+
if (!strcmp(argv[i], "-P")) {
i = i + 1;
if (i >= argc) {
diff --git a/cpio.om b/cpio.om
@@ -95,19 +95,19 @@ func main(argc: int, argv: **byte, envp: **byte) {
// header
fputs(stdout, "070701");
- fputh(stdout, stat.ino & (-1 >> 32));
- fputh(stdout, stat.uid_mode & (-1 >> 32));
- fputh(stdout, 0);
- fputh(stdout, 0);
- fputh(stdout, stat.nlink);
- fputh(stdout, stat.mtime);
- fputh(stdout, stat.size);
- fputh(stdout, stat.dev >> 8);
- fputh(stdout, stat.dev & 255);
- fputh(stdout, stat.rdev >> 8);
- fputh(stdout, (stat.rdev) & 255);
- fputh(stdout, len + 1);
- fputh(stdout, 0);
+ fputh32(stdout, stat.ino & (-1 >> 32));
+ fputh32(stdout, stat.uid_mode & (-1 >> 32));
+ fputh32(stdout, 0);
+ fputh32(stdout, 0);
+ fputh32(stdout, stat.nlink);
+ fputh32(stdout, stat.mtime);
+ fputh32(stdout, stat.size);
+ fputh32(stdout, stat.dev >> 8);
+ fputh32(stdout, stat.dev & 255);
+ fputh32(stdout, stat.rdev >> 8);
+ fputh32(stdout, (stat.rdev) & 255);
+ fputh32(stdout, len + 1);
+ fputh32(stdout, 0);
fputs(stdout, name);
fputc(stdout, 0);
@@ -158,19 +158,19 @@ func main(argc: int, argv: **byte, envp: **byte) {
// trailer
fputs(stdout, "070701");
- fputh(stdout, 0);
- fputh(stdout, 0);
- fputh(stdout, 0);
- fputh(stdout, 0);
- fputh(stdout, 0);
- fputh(stdout, 0);
- fputh(stdout, 0);
- fputh(stdout, 0);
- fputh(stdout, 0);
- fputh(stdout, 0);
- fputh(stdout, 0);
- fputh(stdout, 11);
- fputh(stdout, 0);
+ fputh32(stdout, 0);
+ fputh32(stdout, 0);
+ fputh32(stdout, 0);
+ fputh32(stdout, 0);
+ fputh32(stdout, 0);
+ fputh32(stdout, 0);
+ fputh32(stdout, 0);
+ fputh32(stdout, 0);
+ fputh32(stdout, 0);
+ fputh32(stdout, 0);
+ fputh32(stdout, 0);
+ fputh32(stdout, 11);
+ fputh32(stdout, 0);
fputs(stdout, "TRAILER!!!");
fputc(stdout, 0);
falign(stdout, 13);
@@ -178,7 +178,7 @@ func main(argc: int, argv: **byte, envp: **byte) {
fflush(stdout);
}
-func fputh(f: *file, x: int) {
+func fputh32(f: *file, x: int) {
var i: int;
if x > (-1 >> 32) {