commit c2e49e7d23e10f98c1f012afda5f821a09fb68a8
parent 348691894219089559fd214c5e7b021389dac82d
Author: erai <erai@omiltem.net>
Date: Thu, 30 Jan 2025 00:59:01 +0000
add _includ special case
Diffstat:
M | .build.yml | | | 3 | +-- |
M | cc0.c | | | 61 | +++++++++++++++++++++++++++++++++++++++++++------------------ |
M | cc1.om | | | 27 | ++++++++++++++++----------- |
M | ir.om | | | 42 | +++++++++++++++++++++++++++++++++++------- |
4 files changed, 95 insertions(+), 38 deletions(-)
diff --git a/.build.yml b/.build.yml
@@ -1,7 +1,6 @@
---
image: archlinux
-packages:
- - gcc
+packages: []
sources:
- https://git.sr.ht/~erai/os
tasks:
diff --git a/cc0.c b/cc0.c
@@ -159,6 +159,7 @@ struct my_irop {
struct my_irop* my_b;
unsigned long my_n;
unsigned char* my_s;
+ unsigned long my_slen;
struct my_type* my_t;
unsigned long my_mark;
unsigned char* my_filename;
@@ -781,6 +782,7 @@ 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);
struct my_irfunc*( my_func_to_ir)(struct my_compiler* my_c,struct my_node* my_n);
+unsigned char*( my_gather_include)(struct my_compiler* my_c,unsigned char* my_filename,unsigned long* my_slen);
unsigned long( my_get)(struct my_peg* my_c);
unsigned long( my_hex2int)(unsigned char* my_s,unsigned long my_len,unsigned long* my_ok);
unsigned long( my_hexdig)(unsigned long my_ch,unsigned long* my_ok);
@@ -808,8 +810,9 @@ void( my_memcpy)(unsigned char* my_dest,unsigned char* my_src,unsigned long my_s
void( my_memset)(unsigned char* my_dest,unsigned long my_c,unsigned long my_size);
struct my_irblock*( my_mkirblock)(struct my_irfunc* my_ic);
struct my_irop*( my_mkirconst)(struct my_irfunc* my_ic,unsigned long my_n);
+struct my_irop*( my_mkirfunc)(struct my_irfunc* my_ic,unsigned char* my_name);
struct my_irop*( my_mkirop)(struct my_irfunc* my_ic,unsigned long my_kind,struct my_irop* my_a,struct my_irop* my_b);
-struct my_irop*( my_mkirstr)(struct my_irfunc* my_ic,unsigned char* my_s);
+struct my_irop*( my_mkirstr)(struct my_irfunc* my_ic,unsigned char* my_s,unsigned long my_slen);
struct my_irop*( my_mkirtmp)(struct my_irfunc* my_ic,struct my_type* my_t);
struct my_irvar*( my_mkirvar)(struct my_irfunc* my_ic,unsigned char* my_name,struct my_type* my_t);
struct my_label*( my_mklabel)(struct my_assembler* my_c);
@@ -2098,20 +2101,12 @@ void( my_compile_func)(struct my_compiler* my_c,struct my_decl* my_d){
(my_emit_ret)(((my_c)->my_s));
}
void( my_compile_include)(struct my_compiler* my_c,struct my_node* my_n){
- unsigned char* my_filename = 0;
- unsigned long my_fd = 0;
unsigned char* my_blob = 0;
unsigned long my_len = 0;
if ((unsigned long)(((long)((((my_n)->my_b)->my_a)->my_kind))!=((long)(my_N_STR)))) {
(my_die)(((unsigned char *)"non literal include"));
}
- (my_filename)=((((my_n)->my_b)->my_a)->my_s);
- (my_fd)=((my_open)((my_filename),(my_O_RDONLY),(0UL)));
- if ((unsigned long)(((long)(my_fd))<((long)(0UL)))) {
- (my_die)(((unsigned char *)"failed to open include"));
- }
- (my_blob)=((my_readall)((my_fd),(&(my_len)),((my_c)->my_a)));
- (my_close)((my_fd));
+ (my_blob)=((my_gather_include)((my_c),((((my_n)->my_b)->my_a)->my_s),(&(my_len))));
(my_as_opr)(((my_c)->my_s),(my_OP_POPR),(my_R_RAX));
(my_as_opr)(((my_c)->my_s),(my_OP_POPR),(my_R_RDI));
(my_as_opri64)(((my_c)->my_s),(my_OP_MOVABS),(my_R_RAX),(my_len));
@@ -4435,7 +4430,7 @@ struct my_irop*( my_expr_to_ir)(struct my_irfunc* my_ic,struct my_node* my_n){
(my_o)=((my_mkirconst)((my_ic),(my_size)));
return my_o;
} else if ((unsigned long)(((long)(my_kind))==((long)(my_N_STR)))) {
- (my_o)=((my_mkirstr)((my_ic),((my_n)->my_s)));
+ (my_o)=((my_mkirstr)((my_ic),((my_n)->my_s),((my_strlen)(((my_n)->my_s)))));
return my_o;
} else if ((unsigned long)(((long)(my_kind))==((long)(my_N_CALL)))) {
(my_o)=((my_ircall)((my_ic),(my_n)));
@@ -4453,8 +4448,7 @@ struct my_irop*( my_expr_to_ir)(struct my_irfunc* my_ic,struct my_node* my_n){
return my_o;
}
if ((unsigned long)((my_v)&&((my_v)->my_func_defined))) {
- (my_o)=((my_mkirop)((my_ic),(my_IOP_FUNC),((void *)0),((void *)0)));
- ((my_o)->my_s)=((my_n)->my_s);
+ (my_mkirfunc)((my_ic),((my_n)->my_s));
return my_o;
}
(my_cdie)(((my_ic)->my_c),((unsigned char *)"no such symbol"));
@@ -5028,6 +5022,17 @@ struct my_irfunc*( my_func_to_ir)(struct my_compiler* my_c,struct my_node* my_n)
}
return my_ic;
}
+unsigned char*( my_gather_include)(struct my_compiler* my_c,unsigned char* my_filename,unsigned long* my_slen){
+ unsigned long my_fd = 0;
+ unsigned char* my_blob = 0;
+ (my_fd)=((my_open)((my_filename),(my_O_RDONLY),(0UL)));
+ if ((unsigned long)(((long)(my_fd))<((long)(0UL)))) {
+ (my_cdie)((my_c),((unsigned char *)"failed to open include"));
+ }
+ (my_blob)=((my_readall)((my_fd),(my_slen),((my_c)->my_a)));
+ (my_close)((my_fd));
+ return my_blob;
+}
unsigned long( my_get)(struct my_peg* my_c){
unsigned long my_ch = 0;
if ((unsigned long)(((long)((my_c)->my_pos))==((long)((my_c)->my_size)))) {
@@ -5221,6 +5226,21 @@ struct my_irop*( my_ircall)(struct my_irfunc* my_ic,struct my_node* my_n){
struct my_irop* my_fp = 0;
unsigned long my_i = 0;
unsigned long my_count = 0;
+ unsigned long my_slen = 0;
+ unsigned char* my_blob = 0;
+ if ((unsigned long)(((unsigned long)(((long)(((my_n)->my_a)->my_kind))==((long)(my_N_IDENT))))&&((unsigned long)(((long)((my_strcmp)((((my_n)->my_a)->my_s),((unsigned char *)"_include"))))==((long)(0UL)))))) {
+ if ((unsigned long)(((long)((((my_n)->my_b)->my_a)->my_kind))!=((long)(my_N_STR)))) {
+ (my_cdie)(((my_ic)->my_c),((unsigned char *)"non literal include"));
+ }
+ (my_blob)=((my_gather_include)(((my_ic)->my_c),((((my_n)->my_b)->my_a)->my_s),(&(my_slen))));
+ (my_a)=((my_expr_to_ir)((my_ic),((((my_n)->my_b)->my_b)->my_a)));
+ (my_b)=((my_mkirconst)((my_ic),(my_slen)));
+ (my_o)=((my_mkirop)((my_ic),(my_IOP_STORE),(my_a),(my_b)));
+ ((my_o)->my_t)=((((((my_n)->my_b)->my_b)->my_a)->my_t)->my_val);
+ (my_iraddop)((my_ic),(my_o));
+ (my_o)=((my_mkirstr)((my_ic),(my_blob),(my_slen)));
+ return my_o;
+ }
(my_fp)=((my_mkirtmp)((my_ic),(((my_n)->my_a)->my_t)));
(my_b)=((my_expr_to_ir)((my_ic),((my_n)->my_a)));
(my_a)=((my_mkirop)((my_ic),(my_IOP_REF),(my_fp),((void *)0)));
@@ -5846,24 +5866,29 @@ struct my_irop*( my_mkirconst)(struct my_irfunc* my_ic,unsigned long my_n){
((my_o)->my_n)=(my_n);
return my_o;
}
+struct my_irop*( my_mkirfunc)(struct my_irfunc* my_ic,unsigned char* my_name){
+ struct my_irop* my_o = 0;
+ (my_o)=((my_mkirop)((my_ic),(my_IOP_FUNC),((void *)0),((void *)0)));
+ ((my_o)->my_s)=(my_name);
+ ((my_o)->my_slen)=((my_strlen)((my_name)));
+ return my_o;
+}
struct my_irop*( my_mkirop)(struct my_irfunc* my_ic,unsigned long my_kind,struct my_irop* my_a,struct my_irop* my_b){
struct my_irop* my_o = 0;
- (my_o)=((struct my_irop*)(my_alloc)(((my_ic)->my_a),(80UL)));
+ (my_o)=((struct my_irop*)(my_alloc)(((my_ic)->my_a),(88UL)));
((my_o)->my_kind)=(my_kind);
((my_o)->my_a)=(my_a);
((my_o)->my_b)=(my_b);
- ((my_o)->my_n)=(0UL);
- ((my_o)->my_s)=((void *)0);
- ((my_o)->my_t)=((void *)0);
((my_o)->my_filename)=(((my_ic)->my_c)->my_filename);
((my_o)->my_lineno)=(((my_ic)->my_c)->my_lineno);
((my_o)->my_colno)=(((my_ic)->my_c)->my_colno);
return my_o;
}
-struct my_irop*( my_mkirstr)(struct my_irfunc* my_ic,unsigned char* my_s){
+struct my_irop*( my_mkirstr)(struct my_irfunc* my_ic,unsigned char* my_s,unsigned long my_slen){
struct my_irop* my_o = 0;
(my_o)=((my_mkirop)((my_ic),(my_IOP_STR),((void *)0),((void *)0)));
((my_o)->my_s)=(my_s);
+ ((my_o)->my_slen)=(my_slen);
return my_o;
}
struct my_irop*( my_mkirtmp)(struct my_irfunc* my_ic,struct my_type* my_t){
diff --git a/cc1.om b/cc1.om
@@ -1299,27 +1299,32 @@ func hoist_locals(c: *compiler, d: *decl, n: *node, offset: int): int {
return offset;
}
-func compile_include(c: *compiler, n: *node) {
- var filename: *byte;
+func gather_include(c: *compiler, filename: *byte, slen: *int): *byte {
var fd: int;
var blob: *byte;
- var len: int;
-
- if n.b.a.kind != N_STR {
- die("non literal include");
- }
-
- filename = n.b.a.s;
fd = open(filename, O_RDONLY, 0);
if fd < 0 {
- die("failed to open include");
+ cdie(c, "failed to open include");
}
- blob = readall(fd, &len, c.a);
+ blob = readall(fd, slen, c.a);
close(fd);
+ return blob;
+}
+
+func compile_include(c: *compiler, n: *node) {
+ var blob: *byte;
+ var len: int;
+
+ if n.b.a.kind != N_STR {
+ die("non literal include");
+ }
+
+ blob = gather_include(c, n.b.a.s, &len);
+
as_opr(c.s, OP_POPR, R_RAX);
as_opr(c.s, OP_POPR, R_RDI);
as_opri64(c.s, OP_MOVABS, R_RAX, len);
diff --git a/ir.om b/ir.om
@@ -51,6 +51,7 @@ struct irop {
b: *irop;
n: int;
s: *byte;
+ slen: int;
t: *type;
mark: int;
filename: *byte;
@@ -155,9 +156,6 @@ func mkirop(ic: *irfunc, kind: int, a: *irop, b: *irop): *irop {
o.kind = kind;
o.a = a;
o.b = b;
- o.n = 0;
- o.s = nil;
- o.t = nil;
o.filename = ic.c.filename;
o.lineno = ic.c.lineno;
@@ -176,12 +174,24 @@ func mkirconst(ic: *irfunc, n: int): *irop {
return o;
}
-func mkirstr(ic: *irfunc, s: *byte): *irop {
+func mkirstr(ic: *irfunc, s: *byte, slen: int): *irop {
var o: *irop;
o = mkirop(ic, IOP_STR, nil, nil);
o.s = s;
+ o.slen = slen;
+
+ return o;
+}
+
+func mkirfunc(ic: *irfunc, name: *byte): *irop {
+ var o: *irop;
+
+ o = mkirop(ic, IOP_FUNC, nil, nil);
+
+ o.s = name;
+ o.slen = strlen(name);
return o;
}
@@ -298,6 +308,25 @@ func ircall(ic: *irfunc, n: *node): *irop {
var fp: *irop;
var i: int;
var count: int;
+ var slen: int;
+ var blob: *byte;
+
+ if n.a.kind == N_IDENT && strcmp(n.a.s, "_include") == 0 {
+ if n.b.a.kind != N_STR {
+ cdie(ic.c, "non literal include");
+ }
+
+ blob = gather_include(ic.c, n.b.a.s, &slen);
+
+ a = expr_to_ir(ic, n.b.b.a);
+ b = mkirconst(ic, slen);
+ o = mkirop(ic, IOP_STORE, a, b);
+ o.t = n.b.b.a.t.val;
+ iraddop(ic, o);
+
+ o = mkirstr(ic, blob, slen);
+ return o;
+ }
// Evaluate the expression left to right starting with the function
fp = mkirtmp(ic, n.a.t);
@@ -438,7 +467,7 @@ func expr_to_ir(ic: *irfunc, n: *node): *irop {
o = mkirconst(ic, size);
return o;
} else if kind == N_STR {
- o = mkirstr(ic, n.s);
+ o = mkirstr(ic, n.s, strlen(n.s));
return o;
} else if kind == N_CALL {
o = ircall(ic, n);
@@ -462,8 +491,7 @@ func expr_to_ir(ic: *irfunc, n: *node): *irop {
// function
if v && v.func_defined {
- o = mkirop(ic, IOP_FUNC, nil, nil);
- o.s = n.s;
+ mkirfunc(ic, n.s);
return o;
}