selfhost: drop snake_case locals in dyn/dynout/obj + w6a + cgen + ww driver
This commit is contained in:
332
lib/ww/parse.ww
332
lib/ww/parse.ww
@@ -27,23 +27,23 @@ type parser = struct {
|
|||||||
// nocast: while inside `[...]` we treat ':' as the slice
|
// nocast: while inside `[...]` we treat ':' as the slice
|
||||||
// separator, not the cast operator. Mirrors parse.c's flag.
|
// separator, not the cast operator. Mirrors parse.c's flag.
|
||||||
nocast: i32,
|
nocast: i32,
|
||||||
cur_kind: i32,
|
curkind: i32,
|
||||||
cur_file: str,
|
curfile: str,
|
||||||
cur_line: i32,
|
curline: i32,
|
||||||
cur_col: i32,
|
curcol: i32,
|
||||||
cur_text: str,
|
curtext: str,
|
||||||
cur_uval: u64,
|
curuval: u64,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn refill(p: *parser) void = {
|
fn refill(p: *parser) void = {
|
||||||
let t: tok;
|
let t: tok;
|
||||||
lexnext(p.l, &t);
|
lexnext(p.l, &t);
|
||||||
p.cur_kind = t.kind;
|
p.curkind = t.kind;
|
||||||
p.cur_file = t.file;
|
p.curfile = t.file;
|
||||||
p.cur_line = t.line;
|
p.curline = t.line;
|
||||||
p.cur_col = t.col;
|
p.curcol = t.col;
|
||||||
p.cur_text = t.text;
|
p.curtext = t.text;
|
||||||
p.cur_uval = t.uval;
|
p.curuval = t.uval;
|
||||||
};
|
};
|
||||||
|
|
||||||
export fn parserinit(p: *parser, a: *arena, l: *lex) void = {
|
export fn parserinit(p: *parser, a: *arena, l: *lex) void = {
|
||||||
@@ -57,7 +57,7 @@ export fn parserinit(p: *parser, a: *arena, l: *lex) void = {
|
|||||||
fn advance(p: *parser) void = { refill(p); };
|
fn advance(p: *parser) void = { refill(p); };
|
||||||
|
|
||||||
fn accepttok(p: *parser, k: i32) bool = {
|
fn accepttok(p: *parser, k: i32) bool = {
|
||||||
if (p.cur_kind == k) { advance(p); return true; };
|
if (p.curkind == k) { advance(p); return true; };
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -70,7 +70,7 @@ fn errmsg(p: *parser, msg: str) void = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fn expecttok(p: *parser, k: i32, what: str) bool = {
|
fn expecttok(p: *parser, k: i32, what: str) bool = {
|
||||||
if (p.cur_kind == k) { advance(p); return true; };
|
if (p.curkind == k) { advance(p); return true; };
|
||||||
errmsg(p, what);
|
errmsg(p, what);
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
@@ -78,12 +78,12 @@ fn expecttok(p: *parser, k: i32, what: str) bool = {
|
|||||||
// expectident — consume the current TK_IDENT and return its text.
|
// expectident — consume the current TK_IDENT and return its text.
|
||||||
// Returns the empty str on error (and advances to make progress).
|
// Returns the empty str on error (and advances to make progress).
|
||||||
fn expectident(p: *parser, into: *str) bool = {
|
fn expectident(p: *parser, into: *str) bool = {
|
||||||
if (p.cur_kind != TK_IDENT) {
|
if (p.curkind != TK_IDENT) {
|
||||||
errmsg(p, "expected identifier");
|
errmsg(p, "expected identifier");
|
||||||
advance(p);
|
advance(p);
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
*into = p.cur_text;
|
*into = p.curtext;
|
||||||
advance(p);
|
advance(p);
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
@@ -95,20 +95,20 @@ fn expectident(p: *parser, into: *str) bool = {
|
|||||||
// land in subsequent commits.
|
// land in subsequent commits.
|
||||||
|
|
||||||
fn parsetype(p: *parser) *node = {
|
fn parsetype(p: *parser) *node = {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
|
|
||||||
if (p.cur_kind == TK_STAR) {
|
if (p.curkind == TK_STAR) {
|
||||||
advance(p);
|
advance(p);
|
||||||
let n: *node = newnode(p.a, N_TPTR, pf, pl, pc);
|
let n: *node = newnode(p.a, N_TPTR, pf, pl, pc);
|
||||||
n.lhs = parsetype(p);
|
n.lhs = parsetype(p);
|
||||||
return n;
|
return n;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (p.cur_kind == TK_LBRACK) {
|
if (p.curkind == TK_LBRACK) {
|
||||||
advance(p);
|
advance(p);
|
||||||
if (p.cur_kind == TK_RBRACK) {
|
if (p.curkind == TK_RBRACK) {
|
||||||
advance(p);
|
advance(p);
|
||||||
let n: *node = newnode(p.a, N_TSLICE, pf, pl, pc);
|
let n: *node = newnode(p.a, N_TSLICE, pf, pl, pc);
|
||||||
n.lhs = parsetype(p);
|
n.lhs = parsetype(p);
|
||||||
@@ -121,17 +121,17 @@ fn parsetype(p: *parser) *node = {
|
|||||||
return n;
|
return n;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (p.cur_kind == TK_STRUCT) {
|
if (p.curkind == TK_STRUCT) {
|
||||||
advance(p);
|
advance(p);
|
||||||
expecttok(p, TK_LBRACE, "expected '{' after struct");
|
expecttok(p, TK_LBRACE, "expected '{' after struct");
|
||||||
let n: *node = newnode(p.a, N_TSTRUCT, pf, pl, pc);
|
let n: *node = newnode(p.a, N_TSTRUCT, pf, pl, pc);
|
||||||
let fhead: *node = nil;
|
let fhead: *node = nil;
|
||||||
let ftail: *node = nil;
|
let ftail: *node = nil;
|
||||||
for (p.cur_kind != TK_RBRACE) {
|
for (p.curkind != TK_RBRACE) {
|
||||||
if (p.cur_kind == TK_EOF) { break; };
|
if (p.curkind == TK_EOF) { break; };
|
||||||
let fpf: str = p.cur_file;
|
let fpf: str = p.curfile;
|
||||||
let fpl: i32 = p.cur_line;
|
let fpl: i32 = p.curline;
|
||||||
let fpc: i32 = p.cur_col;
|
let fpc: i32 = p.curcol;
|
||||||
let f: *node = newnode(p.a, N_TFIELD, fpf, fpl, fpc);
|
let f: *node = newnode(p.a, N_TFIELD, fpf, fpl, fpc);
|
||||||
let fid: str;
|
let fid: str;
|
||||||
expectident(p, &fid);
|
expectident(p, &fid);
|
||||||
@@ -147,16 +147,16 @@ fn parsetype(p: *parser) *node = {
|
|||||||
return n;
|
return n;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (p.cur_kind == TK_IDENT) {
|
if (p.curkind == TK_IDENT) {
|
||||||
let n: *node = newnode(p.a, N_TNAME, pf, pl, pc);
|
let n: *node = newnode(p.a, N_TNAME, pf, pl, pc);
|
||||||
n.str = p.cur_text;
|
n.str = p.curtext;
|
||||||
advance(p);
|
advance(p);
|
||||||
// Dotted path collapse (pkg.Type) deferred — fixtures don't
|
// Dotted path collapse (pkg.Type) deferred — fixtures don't
|
||||||
// need it yet.
|
// need it yet.
|
||||||
return n;
|
return n;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (p.cur_kind == TK_LPAREN) {
|
if (p.curkind == TK_LPAREN) {
|
||||||
// (T) or (T, T, ...) or (T | T | ...)
|
// (T) or (T, T, ...) or (T | T | ...)
|
||||||
advance(p);
|
advance(p);
|
||||||
let first: *node = parsetype(p);
|
let first: *node = parsetype(p);
|
||||||
@@ -186,14 +186,14 @@ fn parsetype(p: *parser) *node = {
|
|||||||
tail.next = e;
|
tail.next = e;
|
||||||
tail = e;
|
tail = e;
|
||||||
if (!accepttok(p, TK_COMMA)) { break; };
|
if (!accepttok(p, TK_COMMA)) { break; };
|
||||||
if (p.cur_kind == TK_RPAREN) { break; };
|
if (p.curkind == TK_RPAREN) { break; };
|
||||||
};
|
};
|
||||||
expecttok(p, TK_RPAREN, "expected ')' in tuple type");
|
expecttok(p, TK_RPAREN, "expected ')' in tuple type");
|
||||||
n.list = head;
|
n.list = head;
|
||||||
return n;
|
return n;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (p.cur_kind == TK_FN) {
|
if (p.curkind == TK_FN) {
|
||||||
advance(p);
|
advance(p);
|
||||||
expecttok(p, TK_LPAREN, "expected '(' after fn in type");
|
expecttok(p, TK_LPAREN, "expected '(' after fn in type");
|
||||||
let n: *node = newnode(p.a, N_TFN, pf, pl, pc);
|
let n: *node = newnode(p.a, N_TFN, pf, pl, pc);
|
||||||
@@ -259,42 +259,42 @@ fn isassignop(k: i32) bool = {
|
|||||||
// are resolved by the two-pass checker — no body-less prototypes needed.
|
// are resolved by the two-pass checker — no body-less prototypes needed.
|
||||||
|
|
||||||
fn parseprimary(p: *parser) *node = {
|
fn parseprimary(p: *parser) *node = {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
|
|
||||||
if (p.cur_kind == TK_INT) {
|
if (p.curkind == TK_INT) {
|
||||||
let n: *node = newnode(p.a, N_INTLIT, pf, pl, pc);
|
let n: *node = newnode(p.a, N_INTLIT, pf, pl, pc);
|
||||||
n.uval = p.cur_uval;
|
n.uval = p.curuval;
|
||||||
n.str = p.cur_text;
|
n.str = p.curtext;
|
||||||
advance(p);
|
advance(p);
|
||||||
return n;
|
return n;
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_STR) {
|
if (p.curkind == TK_STR) {
|
||||||
let n: *node = newnode(p.a, N_STRLIT, pf, pl, pc);
|
let n: *node = newnode(p.a, N_STRLIT, pf, pl, pc);
|
||||||
n.str = p.cur_text;
|
n.str = p.curtext;
|
||||||
advance(p);
|
advance(p);
|
||||||
return n;
|
return n;
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_RUNE) {
|
if (p.curkind == TK_RUNE) {
|
||||||
let n: *node = newnode(p.a, N_RUNELIT, pf, pl, pc);
|
let n: *node = newnode(p.a, N_RUNELIT, pf, pl, pc);
|
||||||
n.uval = p.cur_uval;
|
n.uval = p.curuval;
|
||||||
advance(p);
|
advance(p);
|
||||||
return n;
|
return n;
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_TRUE) {
|
if (p.curkind == TK_TRUE) {
|
||||||
advance(p);
|
advance(p);
|
||||||
return newnode(p.a, N_TRUE, pf, pl, pc);
|
return newnode(p.a, N_TRUE, pf, pl, pc);
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_FALSE) {
|
if (p.curkind == TK_FALSE) {
|
||||||
advance(p);
|
advance(p);
|
||||||
return newnode(p.a, N_FALSE, pf, pl, pc);
|
return newnode(p.a, N_FALSE, pf, pl, pc);
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_NIL) {
|
if (p.curkind == TK_NIL) {
|
||||||
advance(p);
|
advance(p);
|
||||||
return newnode(p.a, N_NIL, pf, pl, pc);
|
return newnode(p.a, N_NIL, pf, pl, pc);
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_LPAREN) {
|
if (p.curkind == TK_LPAREN) {
|
||||||
advance(p);
|
advance(p);
|
||||||
let e: *node = parseexpr(p);
|
let e: *node = parseexpr(p);
|
||||||
// Tuple literal: (a, b, ...)
|
// Tuple literal: (a, b, ...)
|
||||||
@@ -303,7 +303,7 @@ fn parseprimary(p: *parser) *node = {
|
|||||||
t.list = e;
|
t.list = e;
|
||||||
let tail: *node = e;
|
let tail: *node = e;
|
||||||
for (true) {
|
for (true) {
|
||||||
if (p.cur_kind == TK_RPAREN) { break; };
|
if (p.curkind == TK_RPAREN) { break; };
|
||||||
let en: *node = parseexpr(p);
|
let en: *node = parseexpr(p);
|
||||||
tail.next = en;
|
tail.next = en;
|
||||||
tail = en;
|
tail = en;
|
||||||
@@ -315,9 +315,9 @@ fn parseprimary(p: *parser) *node = {
|
|||||||
expecttok(p, TK_RPAREN, "expected ')'");
|
expecttok(p, TK_RPAREN, "expected ')'");
|
||||||
return e;
|
return e;
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_IDENT) {
|
if (p.curkind == TK_IDENT) {
|
||||||
let n: *node = newnode(p.a, N_IDENT, pf, pl, pc);
|
let n: *node = newnode(p.a, N_IDENT, pf, pl, pc);
|
||||||
n.str = p.cur_text;
|
n.str = p.curtext;
|
||||||
advance(p);
|
advance(p);
|
||||||
// `IDENT {` — struct literal. Disambiguate: only consume as a
|
// `IDENT {` — struct literal. Disambiguate: only consume as a
|
||||||
// struct lit when we're not in a context where '{' starts a
|
// struct lit when we're not in a context where '{' starts a
|
||||||
@@ -325,17 +325,17 @@ fn parseprimary(p: *parser) *node = {
|
|||||||
// expressions, never directly from cond contexts that need a
|
// expressions, never directly from cond contexts that need a
|
||||||
// block; in stmt parsing, the for/if drivers consume their
|
// block; in stmt parsing, the for/if drivers consume their
|
||||||
// own paren/cond, so this is safe.
|
// own paren/cond, so this is safe.
|
||||||
if (p.cur_kind == TK_LBRACE) {
|
if (p.curkind == TK_LBRACE) {
|
||||||
advance(p);
|
advance(p);
|
||||||
let s: *node = newnode(p.a, N_STRUCTLIT, pf, pl, pc);
|
let s: *node = newnode(p.a, N_STRUCTLIT, pf, pl, pc);
|
||||||
s.lhs = n;
|
s.lhs = n;
|
||||||
let head: *node = nil;
|
let head: *node = nil;
|
||||||
let tail: *node = nil;
|
let tail: *node = nil;
|
||||||
for (p.cur_kind != TK_RBRACE) {
|
for (p.curkind != TK_RBRACE) {
|
||||||
if (p.cur_kind == TK_EOF) { break; };
|
if (p.curkind == TK_EOF) { break; };
|
||||||
let fpf: str = p.cur_file;
|
let fpf: str = p.curfile;
|
||||||
let fpl: i32 = p.cur_line;
|
let fpl: i32 = p.curline;
|
||||||
let fpc: i32 = p.cur_col;
|
let fpc: i32 = p.curcol;
|
||||||
let id: str;
|
let id: str;
|
||||||
expectident(p, &id);
|
expectident(p, &id);
|
||||||
expecttok(p, TK_ASSIGN, "expected '=' in struct lit field");
|
expecttok(p, TK_ASSIGN, "expected '=' in struct lit field");
|
||||||
@@ -353,7 +353,7 @@ fn parseprimary(p: *parser) *node = {
|
|||||||
};
|
};
|
||||||
return n;
|
return n;
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_MATCH) {
|
if (p.curkind == TK_MATCH) {
|
||||||
// match (e) { case let v: T => stmt; case T => stmt; case => stmt; };
|
// match (e) { case let v: T => stmt; case T => stmt; case => stmt; };
|
||||||
advance(p);
|
advance(p);
|
||||||
expecttok(p, TK_LPAREN, "expected '(' after match");
|
expecttok(p, TK_LPAREN, "expected '(' after match");
|
||||||
@@ -363,20 +363,20 @@ fn parseprimary(p: *parser) *node = {
|
|||||||
expecttok(p, TK_LBRACE, "expected '{' to open match body");
|
expecttok(p, TK_LBRACE, "expected '{' to open match body");
|
||||||
let head: *node = nil;
|
let head: *node = nil;
|
||||||
let tail: *node = nil;
|
let tail: *node = nil;
|
||||||
for (p.cur_kind == TK_CASE) {
|
for (p.curkind == TK_CASE) {
|
||||||
let cf: str = p.cur_file;
|
let cf: str = p.curfile;
|
||||||
let cl: i32 = p.cur_line;
|
let cl: i32 = p.curline;
|
||||||
let cc: i32 = p.cur_col;
|
let cc: i32 = p.curcol;
|
||||||
advance(p); // past `case`
|
advance(p); // past `case`
|
||||||
let mc: *node = newnode(p.a, N_MCASE, cf, cl, cc);
|
let mc: *node = newnode(p.a, N_MCASE, cf, cl, cc);
|
||||||
if (p.cur_kind == TK_LET) {
|
if (p.curkind == TK_LET) {
|
||||||
advance(p);
|
advance(p);
|
||||||
let id: str;
|
let id: str;
|
||||||
expectident(p, &id);
|
expectident(p, &id);
|
||||||
mc.str = id;
|
mc.str = id;
|
||||||
expecttok(p, TK_COLON, "expected ':' after match binding");
|
expecttok(p, TK_COLON, "expected ':' after match binding");
|
||||||
mc.lhs = parsetype(p);
|
mc.lhs = parsetype(p);
|
||||||
} else { if (p.cur_kind != TK_FATARROW) {
|
} else { if (p.curkind != TK_FATARROW) {
|
||||||
mc.lhs = parsetype(p);
|
mc.lhs = parsetype(p);
|
||||||
};};
|
};};
|
||||||
expecttok(p, TK_FATARROW, "expected '=>' in match arm");
|
expecttok(p, TK_FATARROW, "expected '=>' in match arm");
|
||||||
@@ -393,9 +393,9 @@ fn parseprimary(p: *parser) *node = {
|
|||||||
return newnode(p.a, N_NONE, pf, pl, pc);
|
return newnode(p.a, N_NONE, pf, pl, pc);
|
||||||
};
|
};
|
||||||
|
|
||||||
fn parsearglist(p: *parser, close_kind: i32, head_out: **node) void = {
|
fn parsearglist(p: *parser, closekind: i32, headout: **node) void = {
|
||||||
*head_out = nil;
|
*headout = nil;
|
||||||
if (p.cur_kind == close_kind) { return; };
|
if (p.curkind == closekind) { return; };
|
||||||
let head: *node = nil;
|
let head: *node = nil;
|
||||||
let tail: *node = nil;
|
let tail: *node = nil;
|
||||||
for (true) {
|
for (true) {
|
||||||
@@ -403,18 +403,18 @@ fn parsearglist(p: *parser, close_kind: i32, head_out: **node) void = {
|
|||||||
if (head == nil) { head = e; tail = e; }
|
if (head == nil) { head = e; tail = e; }
|
||||||
else { tail.next = e; tail = e; };
|
else { tail.next = e; tail = e; };
|
||||||
if (!accepttok(p, TK_COMMA)) { break; };
|
if (!accepttok(p, TK_COMMA)) { break; };
|
||||||
if (p.cur_kind == close_kind) { break; };
|
if (p.curkind == closekind) { break; };
|
||||||
};
|
};
|
||||||
*head_out = head;
|
*headout = head;
|
||||||
};
|
};
|
||||||
|
|
||||||
fn parsepostfix(p: *parser, lhs: *node) *node = {
|
fn parsepostfix(p: *parser, lhs: *node) *node = {
|
||||||
let cur: *node = lhs;
|
let cur: *node = lhs;
|
||||||
for (true) {
|
for (true) {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
if (p.cur_kind == TK_LPAREN) {
|
if (p.curkind == TK_LPAREN) {
|
||||||
advance(p);
|
advance(p);
|
||||||
let n: *node = newnode(p.a, N_CALL, pf, pl, pc);
|
let n: *node = newnode(p.a, N_CALL, pf, pl, pc);
|
||||||
n.lhs = cur;
|
n.lhs = cur;
|
||||||
@@ -425,14 +425,14 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
|||||||
cur = n;
|
cur = n;
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_LBRACK) {
|
if (p.curkind == TK_LBRACK) {
|
||||||
advance(p);
|
advance(p);
|
||||||
// `[ : hi ]` — slice with implicit lo = 0.
|
// `[ : hi ]` — slice with implicit lo = 0.
|
||||||
if (p.cur_kind == TK_COLON) {
|
if (p.curkind == TK_COLON) {
|
||||||
advance(p);
|
advance(p);
|
||||||
let n: *node = newnode(p.a, N_SLICE, pf, pl, pc);
|
let n: *node = newnode(p.a, N_SLICE, pf, pl, pc);
|
||||||
n.lhs = cur;
|
n.lhs = cur;
|
||||||
if (p.cur_kind != TK_RBRACK) {
|
if (p.curkind != TK_RBRACK) {
|
||||||
n.cond = parseexpr(p);
|
n.cond = parseexpr(p);
|
||||||
};
|
};
|
||||||
expecttok(p, TK_RBRACK, "expected ']' in slice");
|
expecttok(p, TK_RBRACK, "expected ']' in slice");
|
||||||
@@ -445,12 +445,12 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
|||||||
p.nocast = 1;
|
p.nocast = 1;
|
||||||
let e: *node = parseexpr(p);
|
let e: *node = parseexpr(p);
|
||||||
p.nocast = prev;
|
p.nocast = prev;
|
||||||
if (p.cur_kind == TK_COLON) {
|
if (p.curkind == TK_COLON) {
|
||||||
advance(p);
|
advance(p);
|
||||||
let n: *node = newnode(p.a, N_SLICE, pf, pl, pc);
|
let n: *node = newnode(p.a, N_SLICE, pf, pl, pc);
|
||||||
n.lhs = cur;
|
n.lhs = cur;
|
||||||
n.rhs = e;
|
n.rhs = e;
|
||||||
if (p.cur_kind != TK_RBRACK) {
|
if (p.curkind != TK_RBRACK) {
|
||||||
n.cond = parseexpr(p);
|
n.cond = parseexpr(p);
|
||||||
};
|
};
|
||||||
expecttok(p, TK_RBRACK, "expected ']' in slice");
|
expecttok(p, TK_RBRACK, "expected ']' in slice");
|
||||||
@@ -464,7 +464,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
|||||||
cur = n;
|
cur = n;
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_DOT) {
|
if (p.curkind == TK_DOT) {
|
||||||
advance(p);
|
advance(p);
|
||||||
let n: *node = newnode(p.a, N_DOT, pf, pl, pc);
|
let n: *node = newnode(p.a, N_DOT, pf, pl, pc);
|
||||||
n.lhs = cur;
|
n.lhs = cur;
|
||||||
@@ -474,7 +474,7 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
|||||||
cur = n;
|
cur = n;
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_COLON) {
|
if (p.curkind == TK_COLON) {
|
||||||
if (p.nocast != 0) {
|
if (p.nocast != 0) {
|
||||||
return cur;
|
return cur;
|
||||||
};
|
};
|
||||||
@@ -491,10 +491,10 @@ fn parsepostfix(p: *parser, lhs: *node) *node = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fn parseunary(p: *parser) *node = {
|
fn parseunary(p: *parser) *node = {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
let k: i32 = p.cur_kind;
|
let k: i32 = p.curkind;
|
||||||
if (k == TK_MINUS) {
|
if (k == TK_MINUS) {
|
||||||
advance(p);
|
advance(p);
|
||||||
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
|
let n: *node = newnode(p.a, N_UN, pf, pl, pc);
|
||||||
@@ -537,17 +537,17 @@ fn parseunary(p: *parser) *node = {
|
|||||||
fn parsebin(p: *parser, lhs: *node, minp: i32) *node = {
|
fn parsebin(p: *parser, lhs: *node, minp: i32) *node = {
|
||||||
let cur: *node = lhs;
|
let cur: *node = lhs;
|
||||||
for (true) {
|
for (true) {
|
||||||
let op: i32 = p.cur_kind;
|
let op: i32 = p.curkind;
|
||||||
let pr: i32 = bprec(op);
|
let pr: i32 = bprec(op);
|
||||||
if (pr == 0) { return cur; };
|
if (pr == 0) { return cur; };
|
||||||
if (pr < minp) { return cur; };
|
if (pr < minp) { return cur; };
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
advance(p);
|
advance(p);
|
||||||
let rhs: *node = parseunary(p);
|
let rhs: *node = parseunary(p);
|
||||||
for (true) {
|
for (true) {
|
||||||
let np: i32 = bprec(p.cur_kind);
|
let np: i32 = bprec(p.curkind);
|
||||||
if (np <= pr) { break; };
|
if (np <= pr) { break; };
|
||||||
rhs = parsebin(p, rhs, np);
|
rhs = parsebin(p, rhs, np);
|
||||||
};
|
};
|
||||||
@@ -560,11 +560,11 @@ fn parsebin(p: *parser, lhs: *node, minp: i32) *node = {
|
|||||||
|
|
||||||
fn parseexpr(p: *parser) *node = {
|
fn parseexpr(p: *parser) *node = {
|
||||||
let e: *node = parsebin(p, parseunary(p), 1);
|
let e: *node = parsebin(p, parseunary(p), 1);
|
||||||
if (isassignop(p.cur_kind)) {
|
if (isassignop(p.curkind)) {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
let op: i32 = p.cur_kind;
|
let op: i32 = p.curkind;
|
||||||
advance(p);
|
advance(p);
|
||||||
let n: *node = newnode(p.a, N_ASSIGN, pf, pl, pc);
|
let n: *node = newnode(p.a, N_ASSIGN, pf, pl, pc);
|
||||||
n.op = op;
|
n.op = op;
|
||||||
@@ -582,9 +582,9 @@ fn parseexpr(p: *parser) *node = {
|
|||||||
// and match arms are not yet wired; tuple-let / multi-let neither.
|
// and match arms are not yet wired; tuple-let / multi-let neither.
|
||||||
|
|
||||||
fn parseletlocal(p: *parser) *node = {
|
fn parseletlocal(p: *parser) *node = {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
advance(p); // past `let`
|
advance(p); // past `let`
|
||||||
let n: *node = newnode(p.a, N_LET, pf, pl, pc);
|
let n: *node = newnode(p.a, N_LET, pf, pl, pc);
|
||||||
let id: str;
|
let id: str;
|
||||||
@@ -601,15 +601,15 @@ fn parseletlocal(p: *parser) *node = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fn parseblock(p: *parser) *node = {
|
fn parseblock(p: *parser) *node = {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
expecttok(p, TK_LBRACE, "expected '{' to open block");
|
expecttok(p, TK_LBRACE, "expected '{' to open block");
|
||||||
let blk: *node = newnode(p.a, N_BLOCK, pf, pl, pc);
|
let blk: *node = newnode(p.a, N_BLOCK, pf, pl, pc);
|
||||||
let head: *node = nil;
|
let head: *node = nil;
|
||||||
let tail: *node = nil;
|
let tail: *node = nil;
|
||||||
for (p.cur_kind != TK_RBRACE) {
|
for (p.curkind != TK_RBRACE) {
|
||||||
if (p.cur_kind == TK_EOF) { break; };
|
if (p.curkind == TK_EOF) { break; };
|
||||||
let s: *node = parsestmt(p);
|
let s: *node = parsestmt(p);
|
||||||
if (s != nil) {
|
if (s != nil) {
|
||||||
if (head == nil) { head = s; tail = s; }
|
if (head == nil) { head = s; tail = s; }
|
||||||
@@ -622,9 +622,9 @@ fn parseblock(p: *parser) *node = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fn parseif(p: *parser) *node = {
|
fn parseif(p: *parser) *node = {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
advance(p); // past `if`
|
advance(p); // past `if`
|
||||||
expecttok(p, TK_LPAREN, "expected '(' after if");
|
expecttok(p, TK_LPAREN, "expected '(' after if");
|
||||||
let n: *node = newnode(p.a, N_IF, pf, pl, pc);
|
let n: *node = newnode(p.a, N_IF, pf, pl, pc);
|
||||||
@@ -632,7 +632,7 @@ fn parseif(p: *parser) *node = {
|
|||||||
expecttok(p, TK_RPAREN, "expected ')' after if condition");
|
expecttok(p, TK_RPAREN, "expected ')' after if condition");
|
||||||
n.body = parseblock(p);
|
n.body = parseblock(p);
|
||||||
if (accepttok(p, TK_ELSE)) {
|
if (accepttok(p, TK_ELSE)) {
|
||||||
if (p.cur_kind == TK_IF) {
|
if (p.curkind == TK_IF) {
|
||||||
n.els = parseif(p);
|
n.els = parseif(p);
|
||||||
} else {
|
} else {
|
||||||
n.els = parseblock(p);
|
n.els = parseblock(p);
|
||||||
@@ -642,9 +642,9 @@ fn parseif(p: *parser) *node = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fn parsefor(p: *parser) *node = {
|
fn parsefor(p: *parser) *node = {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
advance(p); // past `for`
|
advance(p); // past `for`
|
||||||
expecttok(p, TK_LPAREN, "expected '(' after for");
|
expecttok(p, TK_LPAREN, "expected '(' after for");
|
||||||
let n: *node = newnode(p.a, N_FOR, pf, pl, pc);
|
let n: *node = newnode(p.a, N_FOR, pf, pl, pc);
|
||||||
@@ -656,7 +656,7 @@ fn parsefor(p: *parser) *node = {
|
|||||||
// `let` stmt that's the init. Otherwise, parse expr; if next is
|
// `let` stmt that's the init. Otherwise, parse expr; if next is
|
||||||
// ';' it was cond. If we see two ';' total after init, post is
|
// ';' it was cond. If we see two ';' total after init, post is
|
||||||
// next. Simpler: peek for `let` to decide init form.
|
// next. Simpler: peek for `let` to decide init form.
|
||||||
if (p.cur_kind == TK_LET) {
|
if (p.curkind == TK_LET) {
|
||||||
n.lhs = parseletlocal(p); // init (consumes its own ';')
|
n.lhs = parseletlocal(p); // init (consumes its own ';')
|
||||||
n.cond = parseexpr(p);
|
n.cond = parseexpr(p);
|
||||||
expecttok(p, TK_SEMI, "expected ';' after for cond");
|
expecttok(p, TK_SEMI, "expected ';' after for cond");
|
||||||
@@ -679,38 +679,38 @@ fn parsefor(p: *parser) *node = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fn parsestmt(p: *parser) *node = {
|
fn parsestmt(p: *parser) *node = {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
|
|
||||||
// `static` is allowed on local lets per Hare; we accept and skip
|
// `static` is allowed on local lets per Hare; we accept and skip
|
||||||
// it (it doesn't change the AST shape).
|
// it (it doesn't change the AST shape).
|
||||||
if (p.cur_kind == TK_STATIC) { advance(p); };
|
if (p.curkind == TK_STATIC) { advance(p); };
|
||||||
|
|
||||||
if (p.cur_kind == TK_LBRACE) {
|
if (p.curkind == TK_LBRACE) {
|
||||||
let b: *node = parseblock(p);
|
let b: *node = parseblock(p);
|
||||||
expecttok(p, TK_SEMI, "expected ';' after block");
|
expecttok(p, TK_SEMI, "expected ';' after block");
|
||||||
return b;
|
return b;
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_LET) { return parseletlocal(p); };
|
if (p.curkind == TK_LET) { return parseletlocal(p); };
|
||||||
if (p.cur_kind == TK_IF) {
|
if (p.curkind == TK_IF) {
|
||||||
let n: *node = parseif(p);
|
let n: *node = parseif(p);
|
||||||
expecttok(p, TK_SEMI, "expected ';' after if");
|
expecttok(p, TK_SEMI, "expected ';' after if");
|
||||||
return n;
|
return n;
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_FOR) {
|
if (p.curkind == TK_FOR) {
|
||||||
let n: *node = parsefor(p);
|
let n: *node = parsefor(p);
|
||||||
expecttok(p, TK_SEMI, "expected ';' after for");
|
expecttok(p, TK_SEMI, "expected ';' after for");
|
||||||
return n;
|
return n;
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_RETURN) {
|
if (p.curkind == TK_RETURN) {
|
||||||
advance(p);
|
advance(p);
|
||||||
let n: *node = newnode(p.a, N_RETURN, pf, pl, pc);
|
let n: *node = newnode(p.a, N_RETURN, pf, pl, pc);
|
||||||
if (p.cur_kind != TK_SEMI) {
|
if (p.curkind != TK_SEMI) {
|
||||||
let first: *node = parseexpr(p);
|
let first: *node = parseexpr(p);
|
||||||
// Hare-style multi-value: `return a, b;` becomes a
|
// Hare-style multi-value: `return a, b;` becomes a
|
||||||
// tuple expression so codegen sees one rvalue.
|
// tuple expression so codegen sees one rvalue.
|
||||||
if (p.cur_kind == TK_COMMA) {
|
if (p.curkind == TK_COMMA) {
|
||||||
let t: *node = newnode(p.a, N_TUPLE, pf, pl, pc);
|
let t: *node = newnode(p.a, N_TUPLE, pf, pl, pc);
|
||||||
t.list = first;
|
t.list = first;
|
||||||
let tail: *node = first;
|
let tail: *node = first;
|
||||||
@@ -727,19 +727,19 @@ fn parsestmt(p: *parser) *node = {
|
|||||||
expecttok(p, TK_SEMI, "expected ';' after return");
|
expecttok(p, TK_SEMI, "expected ';' after return");
|
||||||
return n;
|
return n;
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_DEFER) {
|
if (p.curkind == TK_DEFER) {
|
||||||
advance(p);
|
advance(p);
|
||||||
let n: *node = newnode(p.a, N_DEFER, pf, pl, pc);
|
let n: *node = newnode(p.a, N_DEFER, pf, pl, pc);
|
||||||
n.lhs = parseexpr(p);
|
n.lhs = parseexpr(p);
|
||||||
expecttok(p, TK_SEMI, "expected ';' after defer");
|
expecttok(p, TK_SEMI, "expected ';' after defer");
|
||||||
return n;
|
return n;
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_BREAK) {
|
if (p.curkind == TK_BREAK) {
|
||||||
advance(p);
|
advance(p);
|
||||||
expecttok(p, TK_SEMI, "expected ';' after break");
|
expecttok(p, TK_SEMI, "expected ';' after break");
|
||||||
return newnode(p.a, N_BREAK, pf, pl, pc);
|
return newnode(p.a, N_BREAK, pf, pl, pc);
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_CONTINUE) {
|
if (p.curkind == TK_CONTINUE) {
|
||||||
advance(p);
|
advance(p);
|
||||||
expecttok(p, TK_SEMI, "expected ';' after continue");
|
expecttok(p, TK_SEMI, "expected ';' after continue");
|
||||||
return newnode(p.a, N_CONTINUE, pf, pl, pc);
|
return newnode(p.a, N_CONTINUE, pf, pl, pc);
|
||||||
@@ -751,11 +751,11 @@ fn parsestmt(p: *parser) *node = {
|
|||||||
// through parsebin(parseunary, 1) so the `=` stays for us to
|
// through parsebin(parseunary, 1) so the `=` stays for us to
|
||||||
// consume — parseexpr would absorb it.
|
// consume — parseexpr would absorb it.
|
||||||
let e: *node = parseexpr(p);
|
let e: *node = parseexpr(p);
|
||||||
if (p.cur_kind == TK_COMMA) {
|
if (p.curkind == TK_COMMA) {
|
||||||
let m: *node = newnode(p.a, N_MASSIGN, pf, pl, pc);
|
let m: *node = newnode(p.a, N_MASSIGN, pf, pl, pc);
|
||||||
let head: *node = e;
|
let head: *node = e;
|
||||||
let tail: *node = e;
|
let tail: *node = e;
|
||||||
for (p.cur_kind == TK_COMMA) {
|
for (p.curkind == TK_COMMA) {
|
||||||
advance(p);
|
advance(p);
|
||||||
let lv: *node = parsebin(p, parseunary(p), 1);
|
let lv: *node = parsebin(p, parseunary(p), 1);
|
||||||
tail.next = lv;
|
tail.next = lv;
|
||||||
@@ -776,9 +776,9 @@ fn parsestmt(p: *parser) *node = {
|
|||||||
// ---- top-level decl parsers ------------------------------------------
|
// ---- top-level decl parsers ------------------------------------------
|
||||||
|
|
||||||
fn parseuse(p: *parser) *node = {
|
fn parseuse(p: *parser) *node = {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
advance(p); // past `use`
|
advance(p); // past `use`
|
||||||
let n: *node = newnode(p.a, N_USE, pf, pl, pc);
|
let n: *node = newnode(p.a, N_USE, pf, pl, pc);
|
||||||
let id: str;
|
let id: str;
|
||||||
@@ -789,9 +789,9 @@ fn parseuse(p: *parser) *node = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fn parsedef(p: *parser, exported: i32) *node = {
|
fn parsedef(p: *parser, exported: i32) *node = {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
advance(p); // past `def`
|
advance(p); // past `def`
|
||||||
let n: *node = newnode(p.a, N_DEF, pf, pl, pc);
|
let n: *node = newnode(p.a, N_DEF, pf, pl, pc);
|
||||||
n.module = p.l.module;
|
n.module = p.l.module;
|
||||||
@@ -808,9 +808,9 @@ fn parsedef(p: *parser, exported: i32) *node = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fn parselet(p: *parser, exported: i32) *node = {
|
fn parselet(p: *parser, exported: i32) *node = {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
advance(p); // past `let`
|
advance(p); // past `let`
|
||||||
let n: *node = newnode(p.a, N_LET, pf, pl, pc);
|
let n: *node = newnode(p.a, N_LET, pf, pl, pc);
|
||||||
n.module = p.l.module;
|
n.module = p.l.module;
|
||||||
@@ -831,10 +831,10 @@ fn parselet(p: *parser, exported: i32) *node = {
|
|||||||
fn parseattrs(p: *parser) *node = {
|
fn parseattrs(p: *parser) *node = {
|
||||||
let head: *node = nil;
|
let head: *node = nil;
|
||||||
let tail: *node = nil;
|
let tail: *node = nil;
|
||||||
for (p.cur_kind == TK_AT) {
|
for (p.curkind == TK_AT) {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
advance(p);
|
advance(p);
|
||||||
let a: *node = newnode(p.a, N_ATTR, pf, pl, pc);
|
let a: *node = newnode(p.a, N_ATTR, pf, pl, pc);
|
||||||
let id: str;
|
let id: str;
|
||||||
@@ -852,13 +852,13 @@ fn parseattrs(p: *parser) *node = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fn parseparams(p: *parser) *node = {
|
fn parseparams(p: *parser) *node = {
|
||||||
if (p.cur_kind == TK_RPAREN) { return nil; };
|
if (p.curkind == TK_RPAREN) { return nil; };
|
||||||
let head: *node = nil;
|
let head: *node = nil;
|
||||||
let tail: *node = nil;
|
let tail: *node = nil;
|
||||||
for (true) {
|
for (true) {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
let n: *node = newnode(p.a, N_PARAM, pf, pl, pc);
|
let n: *node = newnode(p.a, N_PARAM, pf, pl, pc);
|
||||||
// Param form: IDENT ':' type. Anonymous-type-only params (used
|
// Param form: IDENT ':' type. Anonymous-type-only params (used
|
||||||
// in fn type expressions) aren't yet wired here.
|
// in fn type expressions) aren't yet wired here.
|
||||||
@@ -870,15 +870,15 @@ fn parseparams(p: *parser) *node = {
|
|||||||
if (head == nil) { head = n; tail = n; }
|
if (head == nil) { head = n; tail = n; }
|
||||||
else { tail.next = n; tail = n; };
|
else { tail.next = n; tail = n; };
|
||||||
if (!accepttok(p, TK_COMMA)) { break; };
|
if (!accepttok(p, TK_COMMA)) { break; };
|
||||||
if (p.cur_kind == TK_RPAREN) { break; };
|
if (p.curkind == TK_RPAREN) { break; };
|
||||||
};
|
};
|
||||||
return head;
|
return head;
|
||||||
};
|
};
|
||||||
|
|
||||||
fn parsefn(p: *parser, exported: i32, attrs: *node) *node = {
|
fn parsefn(p: *parser, exported: i32, attrs: *node) *node = {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
advance(p); // past `fn`
|
advance(p); // past `fn`
|
||||||
let n: *node = newnode(p.a, N_FNDECL, pf, pl, pc);
|
let n: *node = newnode(p.a, N_FNDECL, pf, pl, pc);
|
||||||
n.module = p.l.module;
|
n.module = p.l.module;
|
||||||
@@ -888,8 +888,8 @@ fn parsefn(p: *parser, exported: i32, attrs: *node) *node = {
|
|||||||
expecttok(p, TK_LPAREN, "expected '(' after fn name");
|
expecttok(p, TK_LPAREN, "expected '(' after fn name");
|
||||||
n.list = parseparams(p);
|
n.list = parseparams(p);
|
||||||
expecttok(p, TK_RPAREN, "expected ')' after params");
|
expecttok(p, TK_RPAREN, "expected ')' after params");
|
||||||
if (p.cur_kind != TK_ASSIGN) {
|
if (p.curkind != TK_ASSIGN) {
|
||||||
if (p.cur_kind != TK_SEMI) {
|
if (p.curkind != TK_SEMI) {
|
||||||
n.lhs = parsetype(p);
|
n.lhs = parsetype(p);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -906,9 +906,9 @@ fn parsefn(p: *parser, exported: i32, attrs: *node) *node = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fn parsetypedecl(p: *parser, exported: i32) *node = {
|
fn parsetypedecl(p: *parser, exported: i32) *node = {
|
||||||
let pf: str = p.cur_file;
|
let pf: str = p.curfile;
|
||||||
let pl: i32 = p.cur_line;
|
let pl: i32 = p.curline;
|
||||||
let pc: i32 = p.cur_col;
|
let pc: i32 = p.curcol;
|
||||||
advance(p); // past `type`
|
advance(p); // past `type`
|
||||||
let n: *node = newnode(p.a, N_TYPEDECL, pf, pl, pc);
|
let n: *node = newnode(p.a, N_TYPEDECL, pf, pl, pc);
|
||||||
n.module = p.l.module;
|
n.module = p.l.module;
|
||||||
@@ -925,38 +925,38 @@ fn parsetypedecl(p: *parser, exported: i32) *node = {
|
|||||||
// ---- file-level loop -------------------------------------------------
|
// ---- file-level loop -------------------------------------------------
|
||||||
|
|
||||||
export fn parsefile(p: *parser) *node = {
|
export fn parsefile(p: *parser) *node = {
|
||||||
let f: *node = newnode(p.a, N_FILE, p.cur_file, p.cur_line, p.cur_col);
|
let f: *node = newnode(p.a, N_FILE, p.curfile, p.curline, p.curcol);
|
||||||
let head: *node = nil;
|
let head: *node = nil;
|
||||||
let tail: *node = nil;
|
let tail: *node = nil;
|
||||||
|
|
||||||
for (p.cur_kind != TK_EOF) {
|
for (p.curkind != TK_EOF) {
|
||||||
let attrs: *node = parseattrs(p);
|
let attrs: *node = parseattrs(p);
|
||||||
let exported: i32 = 0;
|
let exported: i32 = 0;
|
||||||
if (p.cur_kind == TK_EXPORT) { exported = 1; advance(p); };
|
if (p.curkind == TK_EXPORT) { exported = 1; advance(p); };
|
||||||
|
|
||||||
let d: *node = nil;
|
let d: *node = nil;
|
||||||
if (p.cur_kind == TK_USE) {
|
if (p.curkind == TK_USE) {
|
||||||
d = parseuse(p);
|
d = parseuse(p);
|
||||||
} else { if (p.cur_kind == TK_DEF) {
|
} else { if (p.curkind == TK_DEF) {
|
||||||
d = parsedef(p, exported);
|
d = parsedef(p, exported);
|
||||||
} else { if (p.cur_kind == TK_TYPE) {
|
} else { if (p.curkind == TK_TYPE) {
|
||||||
d = parsetypedecl(p, exported);
|
d = parsetypedecl(p, exported);
|
||||||
} else { if (p.cur_kind == TK_LET) {
|
} else { if (p.curkind == TK_LET) {
|
||||||
d = parselet(p, exported);
|
d = parselet(p, exported);
|
||||||
} else { if (p.cur_kind == TK_FN) {
|
} else { if (p.curkind == TK_FN) {
|
||||||
d = parsefn(p, exported, attrs);
|
d = parsefn(p, exported, attrs);
|
||||||
} else {
|
} else {
|
||||||
// Recovery: chew tokens until next ';' or EOF, balancing
|
// Recovery: chew tokens until next ';' or EOF, balancing
|
||||||
// '{' '}' pairs so internal ';'s in unfamiliar forms don't
|
// '{' '}' pairs so internal ';'s in unfamiliar forms don't
|
||||||
// derail us.
|
// derail us.
|
||||||
for (p.cur_kind != TK_SEMI) {
|
for (p.curkind != TK_SEMI) {
|
||||||
if (p.cur_kind == TK_EOF) { break; };
|
if (p.curkind == TK_EOF) { break; };
|
||||||
if (p.cur_kind == TK_LBRACE) {
|
if (p.curkind == TK_LBRACE) {
|
||||||
let depth: i32 = 0;
|
let depth: i32 = 0;
|
||||||
for (true) {
|
for (true) {
|
||||||
if (p.cur_kind == TK_EOF) { break; };
|
if (p.curkind == TK_EOF) { break; };
|
||||||
if (p.cur_kind == TK_LBRACE) { depth += 1; advance(p); continue; };
|
if (p.curkind == TK_LBRACE) { depth += 1; advance(p); continue; };
|
||||||
if (p.cur_kind == TK_RBRACE) {
|
if (p.curkind == TK_RBRACE) {
|
||||||
depth -= 1;
|
depth -= 1;
|
||||||
advance(p);
|
advance(p);
|
||||||
if (depth == 0) { break; };
|
if (depth == 0) { break; };
|
||||||
@@ -968,7 +968,7 @@ export fn parsefile(p: *parser) *node = {
|
|||||||
};
|
};
|
||||||
advance(p);
|
advance(p);
|
||||||
};
|
};
|
||||||
if (p.cur_kind == TK_SEMI) { advance(p); };
|
if (p.curkind == TK_SEMI) { advance(p); };
|
||||||
};};};};};
|
};};};};};
|
||||||
|
|
||||||
if (d != nil) {
|
if (d != nil) {
|
||||||
|
|||||||
@@ -99,21 +99,21 @@ fn emitrex(a: *asm_, regbit: i32, rmbit: i32, w: i32) void = {
|
|||||||
|
|
||||||
// ModR/M + (optional) SIB + displacement for [base+disp].
|
// ModR/M + (optional) SIB + displacement for [base+disp].
|
||||||
// Special-cases SP (needs SIB) and BP (forces explicit disp).
|
// Special-cases SP (needs SIB) and BP (forces explicit disp).
|
||||||
fn emitmodrmmem(a: *asm_, reg_field: i32, base: i32, disp: i64) void = {
|
fn emitmodrmmem(a: *asm_, regfield: i32, base: i32, disp: i64) void = {
|
||||||
let rm: i32 = rcode(base);
|
let rm: i32 = rcode(base);
|
||||||
let needsib: bool = (rm == 4);
|
let needsib: bool = (rm == 4);
|
||||||
let forced_disp: bool = false;
|
let forceddisp: bool = false;
|
||||||
if (rm == 5) { if (disp == 0i64) { forced_disp = true; }; };
|
if (rm == 5) { if (disp == 0i64) { forceddisp = true; }; };
|
||||||
|
|
||||||
let mod: i32 = 2;
|
let mod: i32 = 2;
|
||||||
if (disp == 0i64) {
|
if (disp == 0i64) {
|
||||||
if (!forced_disp) { mod = 0; }
|
if (!forceddisp) { mod = 0; }
|
||||||
else { mod = 1; };
|
else { mod = 1; };
|
||||||
} else {
|
} else {
|
||||||
if (disp >= -128i64) { if (disp <= 127i64) { mod = 1; }; };
|
if (disp >= -128i64) { if (disp <= 127i64) { mod = 1; }; };
|
||||||
};
|
};
|
||||||
|
|
||||||
emitbyte(a, modrmbyte(mod, reg_field, rm));
|
emitbyte(a, modrmbyte(mod, regfield, rm));
|
||||||
if (needsib) {
|
if (needsib) {
|
||||||
emitbyte(a, 36u8); // 0x24: scale=0 idx=4(none) base=4
|
emitbyte(a, 36u8); // 0x24: scale=0 idx=4(none) base=4
|
||||||
};
|
};
|
||||||
@@ -132,17 +132,17 @@ fn encoderr(a: *asm_, opcode: u8, src: i32, dst: i32) void = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// reg→mem(base, disp) (e.g. MOVQ src reg into mem; opcode = 0x89).
|
// reg→mem(base, disp) (e.g. MOVQ src reg into mem; opcode = 0x89).
|
||||||
fn encoderm(a: *asm_, opcode: u8, src_reg: i32, base: i32, disp: i64) void = {
|
fn encoderm(a: *asm_, opcode: u8, srcreg: i32, base: i32, disp: i64) void = {
|
||||||
emitrex(a, rhi(src_reg), rhi(base), 1);
|
emitrex(a, rhi(srcreg), rhi(base), 1);
|
||||||
emitbyte(a, opcode);
|
emitbyte(a, opcode);
|
||||||
emitmodrmmem(a, rcode(src_reg), base, disp);
|
emitmodrmmem(a, rcode(srcreg), base, disp);
|
||||||
};
|
};
|
||||||
|
|
||||||
// mem(base, disp) → reg (e.g. MOVQ mem into reg; opcode = 0x8B).
|
// mem(base, disp) → reg (e.g. MOVQ mem into reg; opcode = 0x8B).
|
||||||
fn encodemr(a: *asm_, opcode: u8, dst_reg: i32, base: i32, disp: i64) void = {
|
fn encodemr(a: *asm_, opcode: u8, dstreg: i32, base: i32, disp: i64) void = {
|
||||||
emitrex(a, rhi(dst_reg), rhi(base), 1);
|
emitrex(a, rhi(dstreg), rhi(base), 1);
|
||||||
emitbyte(a, opcode);
|
emitbyte(a, opcode);
|
||||||
emitmodrmmem(a, rcode(dst_reg), base, disp);
|
emitmodrmmem(a, rcode(dstreg), base, disp);
|
||||||
};
|
};
|
||||||
|
|
||||||
// OPCODE /n imm32 reg form (e.g. ADDQ $imm, reg).
|
// OPCODE /n imm32 reg form (e.g. ADDQ $imm, reg).
|
||||||
@@ -162,29 +162,29 @@ fn encodeunary(a: *asm_, opcode: u8, subop: i32, dst: i32) void = {
|
|||||||
|
|
||||||
// SSE2 helpers. Plan 9 syntax: source first, destination second.
|
// SSE2 helpers. Plan 9 syntax: source first, destination second.
|
||||||
// For ADDSD-style ops we put dst in the reg field, src in r/m.
|
// For ADDSD-style ops we put dst in the reg field, src in r/m.
|
||||||
fn sserr(a: *asm_, prefix: u8, op2: u8, reg_op: i32, rm_op: i32) void = {
|
fn sserr(a: *asm_, prefix: u8, op2: u8, regop: i32, rmop: i32) void = {
|
||||||
if (prefix != 0u8) { emitbyte(a, prefix); };
|
if (prefix != 0u8) { emitbyte(a, prefix); };
|
||||||
emitrex(a, rhi(reg_op), rhi(rm_op), 0);
|
emitrex(a, rhi(regop), rhi(rmop), 0);
|
||||||
emitbyte(a, 15u8); // 0x0F
|
emitbyte(a, 15u8); // 0x0F
|
||||||
emitbyte(a, op2);
|
emitbyte(a, op2);
|
||||||
emitbyte(a, modrmbyte(3, rcode(reg_op), rcode(rm_op)));
|
emitbyte(a, modrmbyte(3, rcode(regop), rcode(rmop)));
|
||||||
};
|
};
|
||||||
|
|
||||||
fn ssemrload(a: *asm_, prefix: u8, op2: u8, reg_op: i32, base: i32, disp: i64) void = {
|
fn ssemrload(a: *asm_, prefix: u8, op2: u8, regop: i32, base: i32, disp: i64) void = {
|
||||||
if (prefix != 0u8) { emitbyte(a, prefix); };
|
if (prefix != 0u8) { emitbyte(a, prefix); };
|
||||||
emitrex(a, rhi(reg_op), rhi(base), 0);
|
emitrex(a, rhi(regop), rhi(base), 0);
|
||||||
emitbyte(a, 15u8);
|
emitbyte(a, 15u8);
|
||||||
emitbyte(a, op2);
|
emitbyte(a, op2);
|
||||||
emitmodrmmem(a, rcode(reg_op), base, disp);
|
emitmodrmmem(a, rcode(regop), base, disp);
|
||||||
};
|
};
|
||||||
|
|
||||||
// REX.W variant of sse_rr (CVTTSD2SI / CVTSI2SD).
|
// REX.W variant of sse_rr (CVTTSD2SI / CVTSI2SD).
|
||||||
fn sserrw(a: *asm_, prefix: u8, op2: u8, reg_op: i32, rm_op: i32) void = {
|
fn sserrw(a: *asm_, prefix: u8, op2: u8, regop: i32, rmop: i32) void = {
|
||||||
if (prefix != 0u8) { emitbyte(a, prefix); };
|
if (prefix != 0u8) { emitbyte(a, prefix); };
|
||||||
emitrex(a, rhi(reg_op), rhi(rm_op), 1);
|
emitrex(a, rhi(regop), rhi(rmop), 1);
|
||||||
emitbyte(a, 15u8);
|
emitbyte(a, 15u8);
|
||||||
emitbyte(a, op2);
|
emitbyte(a, op2);
|
||||||
emitbyte(a, modrmbyte(3, rcode(reg_op), rcode(rm_op)));
|
emitbyte(a, modrmbyte(3, rcode(regop), rcode(rmop)));
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---- label resolution / fixups ----------------------------------------
|
// ---- label resolution / fixups ----------------------------------------
|
||||||
@@ -629,7 +629,7 @@ export fn encode(a: *asm_) i32 = {
|
|||||||
|
|
||||||
// Conditional jumps. 0x0F + cc + rel32.
|
// Conditional jumps. 0x0F + cc + rel32.
|
||||||
let cc: u8 = 0u8;
|
let cc: u8 = 0u8;
|
||||||
let is_jcc: bool = true;
|
let isjcc: bool = true;
|
||||||
if (op == A_JE) { cc = 132u8; } // 0x84
|
if (op == A_JE) { cc = 132u8; } // 0x84
|
||||||
else { if (op == A_JZ) { cc = 132u8; }
|
else { if (op == A_JZ) { cc = 132u8; }
|
||||||
else { if (op == A_JNE) { cc = 133u8; }
|
else { if (op == A_JNE) { cc = 133u8; }
|
||||||
@@ -642,8 +642,8 @@ export fn encode(a: *asm_) i32 = {
|
|||||||
else { if (op == A_JBE) { cc = 134u8; }
|
else { if (op == A_JBE) { cc = 134u8; }
|
||||||
else { if (op == A_JA) { cc = 135u8; }
|
else { if (op == A_JA) { cc = 135u8; }
|
||||||
else { if (op == A_JAE) { cc = 131u8; }
|
else { if (op == A_JAE) { cc = 131u8; }
|
||||||
else { is_jcc = false; };};};};};};};};};};};};
|
else { isjcc = false; };};};};};};};};};};};};
|
||||||
if (is_jcc) {
|
if (isjcc) {
|
||||||
emitbyte(a, 15u8);
|
emitbyte(a, 15u8);
|
||||||
emitbyte(a, cc);
|
emitbyte(a, cc);
|
||||||
addfixup(a, a.textlen, p.to.asym);
|
addfixup(a, a.textlen, p.to.asym);
|
||||||
|
|||||||
@@ -841,12 +841,12 @@ fn parseoperand(a: *asm_, p: *u8, offin: u64, n: u64, out: *aoperand) u64 = {
|
|||||||
|
|
||||||
// number(REG) — possibly signed — or bare $NUM-less constant
|
// number(REG) — possibly signed — or bare $NUM-less constant
|
||||||
let cur: u64 = off;
|
let cur: u64 = off;
|
||||||
let is_num: bool = false;
|
let isnum: bool = false;
|
||||||
if (cur < n) {
|
if (cur < n) {
|
||||||
if (p[cur] == 45u8) { is_num = true; }
|
if (p[cur] == 45u8) { isnum = true; }
|
||||||
else { if (p[cur] >= 48u8) { if (p[cur] <= 57u8) { is_num = true; }; }; };
|
else { if (p[cur] >= 48u8) { if (p[cur] <= 57u8) { isnum = true; }; }; };
|
||||||
};
|
};
|
||||||
if (is_num) {
|
if (isnum) {
|
||||||
let v: i64;
|
let v: i64;
|
||||||
let used: u64;
|
let used: u64;
|
||||||
v, used = parsenum(p + off, n - off);
|
v, used = parsenum(p + off, n - off);
|
||||||
@@ -974,11 +974,11 @@ export fn parse(a: *asm_) i32 = {
|
|||||||
if (line[0u64] != 9u8) {
|
if (line[0u64] != 9u8) {
|
||||||
if (isidstart(line[i]: i32)) {
|
if (isidstart(line[i]: i32)) {
|
||||||
let q: u64 = i;
|
let q: u64 = i;
|
||||||
let scan_id: bool = true;
|
let scanid: bool = true;
|
||||||
for (scan_id) {
|
for (scanid) {
|
||||||
if (q >= n) { scan_id = false; }
|
if (q >= n) { scanid = false; }
|
||||||
else { if (isidcont(line[q]: i32)) { q += 1u64; }
|
else { if (isidcont(line[q]: i32)) { q += 1u64; }
|
||||||
else { scan_id = false; }; };
|
else { scanid = false; }; };
|
||||||
};
|
};
|
||||||
if (q < n) { if (line[q] == 58u8) { // ':'
|
if (q < n) { if (line[q] == 58u8) { // ':'
|
||||||
let nm: str = dupstr(a.a, line + i, q - i);
|
let nm: str = dupstr(a.a, line + i, q - i);
|
||||||
@@ -1023,26 +1023,26 @@ export fn parse(a: *asm_) i32 = {
|
|||||||
if (opc == A_TEXT) {
|
if (opc == A_TEXT) {
|
||||||
// TEXT name,$framesize — find first ',' as the end of name.
|
// TEXT name,$framesize — find first ',' as the end of name.
|
||||||
let q: u64 = r0;
|
let q: u64 = r0;
|
||||||
let comma_pos: u64 = n;
|
let commapos: u64 = n;
|
||||||
let scan_t: bool = true;
|
let scant: bool = true;
|
||||||
for (scan_t) {
|
for (scant) {
|
||||||
if (q >= n) { scan_t = false; }
|
if (q >= n) { scant = false; }
|
||||||
else { if (line[q] == 44u8) { comma_pos = q; scan_t = false; }
|
else { if (line[q] == 44u8) { commapos = q; scant = false; }
|
||||||
else { q += 1u64; }; };
|
else { q += 1u64; }; };
|
||||||
};
|
};
|
||||||
let to_op: *aoperand = pr.to;
|
let toop: *aoperand = pr.to;
|
||||||
to_op.atype = D_EXTERN;
|
toop.atype = D_EXTERN;
|
||||||
to_op.asym = dupstr(a.a, line + r0, comma_pos - r0);
|
toop.asym = dupstr(a.a, line + r0, commapos - r0);
|
||||||
if (comma_pos < n) {
|
if (commapos < n) {
|
||||||
let p2: u64 = comma_pos + 1u64;
|
let p2: u64 = commapos + 1u64;
|
||||||
p2 = skipws(line, p2, n);
|
p2 = skipws(line, p2, n);
|
||||||
if (p2 < n) { if (line[p2] == 36u8) { p2 += 1u64; }; };
|
if (p2 < n) { if (line[p2] == 36u8) { p2 += 1u64; }; };
|
||||||
let v: i64;
|
let v: i64;
|
||||||
let used: u64;
|
let used: u64;
|
||||||
v, used = parsenum(line + p2, n - p2);
|
v, used = parsenum(line + p2, n - p2);
|
||||||
let from_op: *aoperand = pr.from;
|
let fromop: *aoperand = pr.from;
|
||||||
from_op.atype = D_CONST;
|
fromop.atype = D_CONST;
|
||||||
from_op.offset = v;
|
fromop.offset = v;
|
||||||
};
|
};
|
||||||
a.line += 1; continue;
|
a.line += 1; continue;
|
||||||
};
|
};
|
||||||
@@ -1051,31 +1051,31 @@ export fn parse(a: *asm_) i32 = {
|
|||||||
// DATA name(SB),"escaped bytes" — find first '(' as end of name.
|
// DATA name(SB),"escaped bytes" — find first '(' as end of name.
|
||||||
let q: u64 = r0;
|
let q: u64 = r0;
|
||||||
let lparen: u64 = n;
|
let lparen: u64 = n;
|
||||||
let scan_d: bool = true;
|
let scand: bool = true;
|
||||||
for (scan_d) {
|
for (scand) {
|
||||||
if (q >= n) { scan_d = false; }
|
if (q >= n) { scand = false; }
|
||||||
else { if (line[q] == 40u8) { lparen = q; scan_d = false; }
|
else { if (line[q] == 40u8) { lparen = q; scand = false; }
|
||||||
else { q += 1u64; }; };
|
else { q += 1u64; }; };
|
||||||
};
|
};
|
||||||
let to_op: *aoperand = pr.to;
|
let toop: *aoperand = pr.to;
|
||||||
to_op.atype = D_EXTERN;
|
toop.atype = D_EXTERN;
|
||||||
to_op.asym = dupstr(a.a, line + r0, lparen - r0);
|
toop.asym = dupstr(a.a, line + r0, lparen - r0);
|
||||||
// Skip past `(SB)` to land just after ')'.
|
// Skip past `(SB)` to land just after ')'.
|
||||||
let p2: u64 = lparen;
|
let p2: u64 = lparen;
|
||||||
let scan_d2: bool = true;
|
let scand2: bool = true;
|
||||||
for (scan_d2) {
|
for (scand2) {
|
||||||
if (p2 >= n) { scan_d2 = false; }
|
if (p2 >= n) { scand2 = false; }
|
||||||
else { if (line[p2] == 41u8) { p2 += 1u64; scan_d2 = false; }
|
else { if (line[p2] == 41u8) { p2 += 1u64; scand2 = false; }
|
||||||
else { p2 += 1u64; }; };
|
else { p2 += 1u64; }; };
|
||||||
};
|
};
|
||||||
// Skip ws / ',' / tab between `)` and the `"`.
|
// Skip ws / ',' / tab between `)` and the `"`.
|
||||||
let scan_d3: bool = true;
|
let scand3: bool = true;
|
||||||
for (scan_d3) {
|
for (scand3) {
|
||||||
if (p2 >= n) { scan_d3 = false; }
|
if (p2 >= n) { scand3 = false; }
|
||||||
else { if (line[p2] == 32u8) { p2 += 1u64; }
|
else { if (line[p2] == 32u8) { p2 += 1u64; }
|
||||||
else { if (line[p2] == 44u8) { p2 += 1u64; }
|
else { if (line[p2] == 44u8) { p2 += 1u64; }
|
||||||
else { if (line[p2] == 9u8) { p2 += 1u64; }
|
else { if (line[p2] == 9u8) { p2 += 1u64; }
|
||||||
else { scan_d3 = false; }; }; }; };
|
else { scand3 = false; }; }; }; };
|
||||||
};
|
};
|
||||||
if (p2 >= n) { perr(a, "DATA missing payload"); a.line += 1; continue; };
|
if (p2 >= n) { perr(a, "DATA missing payload"); a.line += 1; continue; };
|
||||||
if (line[p2] != 34u8) { perr(a, "DATA expects \"...\""); a.line += 1; continue; };
|
if (line[p2] != 34u8) { perr(a, "DATA expects \"...\""); a.line += 1; continue; };
|
||||||
@@ -1258,21 +1258,21 @@ fn emitrex(a: *asm_, regbit: i32, rmbit: i32, w: i32) void = {
|
|||||||
|
|
||||||
// ModR/M + (optional) SIB + displacement for [base+disp].
|
// ModR/M + (optional) SIB + displacement for [base+disp].
|
||||||
// Special-cases SP (needs SIB) and BP (forces explicit disp).
|
// Special-cases SP (needs SIB) and BP (forces explicit disp).
|
||||||
fn emitmodrmmem(a: *asm_, reg_field: i32, base: i32, disp: i64) void = {
|
fn emitmodrmmem(a: *asm_, regfield: i32, base: i32, disp: i64) void = {
|
||||||
let rm: i32 = rcode(base);
|
let rm: i32 = rcode(base);
|
||||||
let needsib: bool = (rm == 4);
|
let needsib: bool = (rm == 4);
|
||||||
let forced_disp: bool = false;
|
let forceddisp: bool = false;
|
||||||
if (rm == 5) { if (disp == 0i64) { forced_disp = true; }; };
|
if (rm == 5) { if (disp == 0i64) { forceddisp = true; }; };
|
||||||
|
|
||||||
let mod: i32 = 2;
|
let mod: i32 = 2;
|
||||||
if (disp == 0i64) {
|
if (disp == 0i64) {
|
||||||
if (!forced_disp) { mod = 0; }
|
if (!forceddisp) { mod = 0; }
|
||||||
else { mod = 1; };
|
else { mod = 1; };
|
||||||
} else {
|
} else {
|
||||||
if (disp >= -128i64) { if (disp <= 127i64) { mod = 1; }; };
|
if (disp >= -128i64) { if (disp <= 127i64) { mod = 1; }; };
|
||||||
};
|
};
|
||||||
|
|
||||||
emitbyte(a, modrmbyte(mod, reg_field, rm));
|
emitbyte(a, modrmbyte(mod, regfield, rm));
|
||||||
if (needsib) {
|
if (needsib) {
|
||||||
emitbyte(a, 36u8); // 0x24: scale=0 idx=4(none) base=4
|
emitbyte(a, 36u8); // 0x24: scale=0 idx=4(none) base=4
|
||||||
};
|
};
|
||||||
@@ -1291,17 +1291,17 @@ fn encoderr(a: *asm_, opcode: u8, src: i32, dst: i32) void = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// reg→mem(base, disp) (e.g. MOVQ src reg into mem; opcode = 0x89).
|
// reg→mem(base, disp) (e.g. MOVQ src reg into mem; opcode = 0x89).
|
||||||
fn encoderm(a: *asm_, opcode: u8, src_reg: i32, base: i32, disp: i64) void = {
|
fn encoderm(a: *asm_, opcode: u8, srcreg: i32, base: i32, disp: i64) void = {
|
||||||
emitrex(a, rhi(src_reg), rhi(base), 1);
|
emitrex(a, rhi(srcreg), rhi(base), 1);
|
||||||
emitbyte(a, opcode);
|
emitbyte(a, opcode);
|
||||||
emitmodrmmem(a, rcode(src_reg), base, disp);
|
emitmodrmmem(a, rcode(srcreg), base, disp);
|
||||||
};
|
};
|
||||||
|
|
||||||
// mem(base, disp) → reg (e.g. MOVQ mem into reg; opcode = 0x8B).
|
// mem(base, disp) → reg (e.g. MOVQ mem into reg; opcode = 0x8B).
|
||||||
fn encodemr(a: *asm_, opcode: u8, dst_reg: i32, base: i32, disp: i64) void = {
|
fn encodemr(a: *asm_, opcode: u8, dstreg: i32, base: i32, disp: i64) void = {
|
||||||
emitrex(a, rhi(dst_reg), rhi(base), 1);
|
emitrex(a, rhi(dstreg), rhi(base), 1);
|
||||||
emitbyte(a, opcode);
|
emitbyte(a, opcode);
|
||||||
emitmodrmmem(a, rcode(dst_reg), base, disp);
|
emitmodrmmem(a, rcode(dstreg), base, disp);
|
||||||
};
|
};
|
||||||
|
|
||||||
// OPCODE /n imm32 reg form (e.g. ADDQ $imm, reg).
|
// OPCODE /n imm32 reg form (e.g. ADDQ $imm, reg).
|
||||||
@@ -1321,29 +1321,29 @@ fn encodeunary(a: *asm_, opcode: u8, subop: i32, dst: i32) void = {
|
|||||||
|
|
||||||
// SSE2 helpers. Plan 9 syntax: source first, destination second.
|
// SSE2 helpers. Plan 9 syntax: source first, destination second.
|
||||||
// For ADDSD-style ops we put dst in the reg field, src in r/m.
|
// For ADDSD-style ops we put dst in the reg field, src in r/m.
|
||||||
fn sserr(a: *asm_, prefix: u8, op2: u8, reg_op: i32, rm_op: i32) void = {
|
fn sserr(a: *asm_, prefix: u8, op2: u8, regop: i32, rmop: i32) void = {
|
||||||
if (prefix != 0u8) { emitbyte(a, prefix); };
|
if (prefix != 0u8) { emitbyte(a, prefix); };
|
||||||
emitrex(a, rhi(reg_op), rhi(rm_op), 0);
|
emitrex(a, rhi(regop), rhi(rmop), 0);
|
||||||
emitbyte(a, 15u8); // 0x0F
|
emitbyte(a, 15u8); // 0x0F
|
||||||
emitbyte(a, op2);
|
emitbyte(a, op2);
|
||||||
emitbyte(a, modrmbyte(3, rcode(reg_op), rcode(rm_op)));
|
emitbyte(a, modrmbyte(3, rcode(regop), rcode(rmop)));
|
||||||
};
|
};
|
||||||
|
|
||||||
fn ssemrload(a: *asm_, prefix: u8, op2: u8, reg_op: i32, base: i32, disp: i64) void = {
|
fn ssemrload(a: *asm_, prefix: u8, op2: u8, regop: i32, base: i32, disp: i64) void = {
|
||||||
if (prefix != 0u8) { emitbyte(a, prefix); };
|
if (prefix != 0u8) { emitbyte(a, prefix); };
|
||||||
emitrex(a, rhi(reg_op), rhi(base), 0);
|
emitrex(a, rhi(regop), rhi(base), 0);
|
||||||
emitbyte(a, 15u8);
|
emitbyte(a, 15u8);
|
||||||
emitbyte(a, op2);
|
emitbyte(a, op2);
|
||||||
emitmodrmmem(a, rcode(reg_op), base, disp);
|
emitmodrmmem(a, rcode(regop), base, disp);
|
||||||
};
|
};
|
||||||
|
|
||||||
// REX.W variant of sse_rr (CVTTSD2SI / CVTSI2SD).
|
// REX.W variant of sse_rr (CVTTSD2SI / CVTSI2SD).
|
||||||
fn sserrw(a: *asm_, prefix: u8, op2: u8, reg_op: i32, rm_op: i32) void = {
|
fn sserrw(a: *asm_, prefix: u8, op2: u8, regop: i32, rmop: i32) void = {
|
||||||
if (prefix != 0u8) { emitbyte(a, prefix); };
|
if (prefix != 0u8) { emitbyte(a, prefix); };
|
||||||
emitrex(a, rhi(reg_op), rhi(rm_op), 1);
|
emitrex(a, rhi(regop), rhi(rmop), 1);
|
||||||
emitbyte(a, 15u8);
|
emitbyte(a, 15u8);
|
||||||
emitbyte(a, op2);
|
emitbyte(a, op2);
|
||||||
emitbyte(a, modrmbyte(3, rcode(reg_op), rcode(rm_op)));
|
emitbyte(a, modrmbyte(3, rcode(regop), rcode(rmop)));
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---- label resolution / fixups ----------------------------------------
|
// ---- label resolution / fixups ----------------------------------------
|
||||||
@@ -1788,7 +1788,7 @@ export fn encode(a: *asm_) i32 = {
|
|||||||
|
|
||||||
// Conditional jumps. 0x0F + cc + rel32.
|
// Conditional jumps. 0x0F + cc + rel32.
|
||||||
let cc: u8 = 0u8;
|
let cc: u8 = 0u8;
|
||||||
let is_jcc: bool = true;
|
let isjcc: bool = true;
|
||||||
if (op == A_JE) { cc = 132u8; } // 0x84
|
if (op == A_JE) { cc = 132u8; } // 0x84
|
||||||
else { if (op == A_JZ) { cc = 132u8; }
|
else { if (op == A_JZ) { cc = 132u8; }
|
||||||
else { if (op == A_JNE) { cc = 133u8; }
|
else { if (op == A_JNE) { cc = 133u8; }
|
||||||
@@ -1801,8 +1801,8 @@ export fn encode(a: *asm_) i32 = {
|
|||||||
else { if (op == A_JBE) { cc = 134u8; }
|
else { if (op == A_JBE) { cc = 134u8; }
|
||||||
else { if (op == A_JA) { cc = 135u8; }
|
else { if (op == A_JA) { cc = 135u8; }
|
||||||
else { if (op == A_JAE) { cc = 131u8; }
|
else { if (op == A_JAE) { cc = 131u8; }
|
||||||
else { is_jcc = false; };};};};};};};};};};};};
|
else { isjcc = false; };};};};};};};};};};};};
|
||||||
if (is_jcc) {
|
if (isjcc) {
|
||||||
emitbyte(a, 15u8);
|
emitbyte(a, 15u8);
|
||||||
emitbyte(a, cc);
|
emitbyte(a, cc);
|
||||||
addfixup(a, a.textlen, p.to.asym);
|
addfixup(a, a.textlen, p.to.asym);
|
||||||
@@ -1962,11 +1962,11 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
bufputb(&str_, &zero, 1u64);
|
bufputb(&str_, &zero, 1u64);
|
||||||
|
|
||||||
// Section name offsets.
|
// Section name offsets.
|
||||||
let shn_text: u32 = bufputcstr(&shstr, ".text");
|
let shntext: u32 = bufputcstr(&shstr, ".text");
|
||||||
let shn_rela: u32 = bufputcstr(&shstr, ".rela.text");
|
let shnrela: u32 = bufputcstr(&shstr, ".rela.text");
|
||||||
let shn_symtab: u32 = bufputcstr(&shstr, ".symtab");
|
let shnsymtab: u32 = bufputcstr(&shstr, ".symtab");
|
||||||
let shn_strtab: u32 = bufputcstr(&shstr, ".strtab");
|
let shnstrtab: u32 = bufputcstr(&shstr, ".strtab");
|
||||||
let shn_shstrtab: u32 = bufputcstr(&shstr, ".shstrtab");
|
let shnshstrtab: u32 = bufputcstr(&shstr, ".shstrtab");
|
||||||
|
|
||||||
// Symbol 0 — STN_UNDEF (24 zero bytes).
|
// Symbol 0 — STN_UNDEF (24 zero bytes).
|
||||||
let zsym: [24]u8;
|
let zsym: [24]u8;
|
||||||
@@ -1983,8 +1983,8 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
let entry: [24]u8;
|
let entry: [24]u8;
|
||||||
let ei: i32 = 0;
|
let ei: i32 = 0;
|
||||||
for (ei < 24) { entry[ei] = 0u8; ei += 1; };
|
for (ei < 24) { entry[ei] = 0u8; ei += 1; };
|
||||||
let st_name: u32 = bufputcstr(&str_, s.name);
|
let stname: u32 = bufputcstr(&str_, s.name);
|
||||||
wru32(entry.ptr, 0u64, st_name);
|
wru32(entry.ptr, 0u64, stname);
|
||||||
if (s.defined != 0) {
|
if (s.defined != 0) {
|
||||||
wru8(entry.ptr, 4u64, ((STB_GLOBAL << 4u8) | STT_FUNC));
|
wru8(entry.ptr, 4u64, ((STB_GLOBAL << 4u8) | STT_FUNC));
|
||||||
wru16(entry.ptr, 6u64, SH_TEXT);
|
wru16(entry.ptr, 6u64, SH_TEXT);
|
||||||
@@ -2004,8 +2004,8 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
for (r != nil) {
|
for (r != nil) {
|
||||||
let entry: [24]u8;
|
let entry: [24]u8;
|
||||||
wru64(entry.ptr, 0u64, r.off);
|
wru64(entry.ptr, 0u64, r.off);
|
||||||
let r_info: u64 = (r.asy.idx: u64 << 32u64) | (r.kind: u64 & 4294967295u64);
|
let rinfo: u64 = (r.asy.idx: u64 << 32u64) | (r.kind: u64 & 4294967295u64);
|
||||||
wru64(entry.ptr, 8u64, r_info);
|
wru64(entry.ptr, 8u64, rinfo);
|
||||||
wru64(entry.ptr, 16u64, r.addend: u64);
|
wru64(entry.ptr, 16u64, r.addend: u64);
|
||||||
bufputb(&rela, entry.ptr, 24u64);
|
bufputb(&rela, entry.ptr, 24u64);
|
||||||
r = r.rnext;
|
r = r.rnext;
|
||||||
@@ -2013,13 +2013,13 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
|
|
||||||
// File offsets.
|
// File offsets.
|
||||||
let off: u64 = EHDR_SZ;
|
let off: u64 = EHDR_SZ;
|
||||||
let off_text: u64 = off; off = off + a.textlen;
|
let offtext: u64 = off; off = off + a.textlen;
|
||||||
let off_rela: u64 = off; off = off + rela.n;
|
let offrela: u64 = off; off = off + rela.n;
|
||||||
let off_sym: u64 = off; off = off + sym.n;
|
let offsym: u64 = off; off = off + sym.n;
|
||||||
let off_str: u64 = off; off = off + str_.n;
|
let offstr: u64 = off; off = off + str_.n;
|
||||||
let off_shstr: u64 = off; off = off + shstr.n;
|
let offshstr: u64 = off; off = off + shstr.n;
|
||||||
for ((off & 7u64) != 0u64) { off += 1u64; };
|
for ((off & 7u64) != 0u64) { off += 1u64; };
|
||||||
let off_shdr: u64 = off;
|
let offshdr: u64 = off;
|
||||||
let NSECT: u16 = 6u16;
|
let NSECT: u16 = 6u16;
|
||||||
|
|
||||||
// ---- Ehdr ----
|
// ---- Ehdr ----
|
||||||
@@ -2038,7 +2038,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
wru32(eh.ptr, 20u64, EV_CURRENT_W);
|
wru32(eh.ptr, 20u64, EV_CURRENT_W);
|
||||||
wru64(eh.ptr, 24u64, 0u64); // e_entry
|
wru64(eh.ptr, 24u64, 0u64); // e_entry
|
||||||
wru64(eh.ptr, 32u64, 0u64); // e_phoff
|
wru64(eh.ptr, 32u64, 0u64); // e_phoff
|
||||||
wru64(eh.ptr, 40u64, off_shdr); // e_shoff
|
wru64(eh.ptr, 40u64, offshdr); // e_shoff
|
||||||
wru32(eh.ptr, 48u64, 0u32); // e_flags
|
wru32(eh.ptr, 48u64, 0u32); // e_flags
|
||||||
wru16(eh.ptr, 52u64, 64u16); // e_ehsize
|
wru16(eh.ptr, 52u64, 64u16); // e_ehsize
|
||||||
wru16(eh.ptr, 54u64, 0u16); // e_phentsize
|
wru16(eh.ptr, 54u64, 0u16); // e_phentsize
|
||||||
@@ -2080,20 +2080,20 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
// .text
|
// .text
|
||||||
sn = 0;
|
sn = 0;
|
||||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||||
wru32(shbuf.ptr, 0u64, shn_text);
|
wru32(shbuf.ptr, 0u64, shntext);
|
||||||
wru32(shbuf.ptr, 4u64, SHT_PROGBITS_C);
|
wru32(shbuf.ptr, 4u64, SHT_PROGBITS_C);
|
||||||
wru64(shbuf.ptr, 8u64, SHF_ALLOC | SHF_EXECINSTR);
|
wru64(shbuf.ptr, 8u64, SHF_ALLOC | SHF_EXECINSTR);
|
||||||
wru64(shbuf.ptr, 24u64, off_text);
|
wru64(shbuf.ptr, 24u64, offtext);
|
||||||
wru64(shbuf.ptr, 32u64, a.textlen);
|
wru64(shbuf.ptr, 32u64, a.textlen);
|
||||||
wru64(shbuf.ptr, 48u64, 1u64); // sh_addralign
|
wru64(shbuf.ptr, 48u64, 1u64); // sh_addralign
|
||||||
os.writefull(fd, shbuf.ptr, 64u64);
|
os.writefull(fd, shbuf.ptr, 64u64);
|
||||||
// .rela.text
|
// .rela.text
|
||||||
sn = 0;
|
sn = 0;
|
||||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||||
wru32(shbuf.ptr, 0u64, shn_rela);
|
wru32(shbuf.ptr, 0u64, shnrela);
|
||||||
wru32(shbuf.ptr, 4u64, SHT_RELA_C);
|
wru32(shbuf.ptr, 4u64, SHT_RELA_C);
|
||||||
wru64(shbuf.ptr, 8u64, SHF_INFO_LINK);
|
wru64(shbuf.ptr, 8u64, SHF_INFO_LINK);
|
||||||
wru64(shbuf.ptr, 24u64, off_rela);
|
wru64(shbuf.ptr, 24u64, offrela);
|
||||||
wru64(shbuf.ptr, 32u64, rela.n);
|
wru64(shbuf.ptr, 32u64, rela.n);
|
||||||
wru32(shbuf.ptr, 40u64, 3u32); // sh_link = symtab idx
|
wru32(shbuf.ptr, 40u64, 3u32); // sh_link = symtab idx
|
||||||
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = .text idx
|
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = .text idx
|
||||||
@@ -2103,9 +2103,9 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
// .symtab
|
// .symtab
|
||||||
sn = 0;
|
sn = 0;
|
||||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||||
wru32(shbuf.ptr, 0u64, shn_symtab);
|
wru32(shbuf.ptr, 0u64, shnsymtab);
|
||||||
wru32(shbuf.ptr, 4u64, SHT_SYMTAB_C);
|
wru32(shbuf.ptr, 4u64, SHT_SYMTAB_C);
|
||||||
wru64(shbuf.ptr, 24u64, off_sym);
|
wru64(shbuf.ptr, 24u64, offsym);
|
||||||
wru64(shbuf.ptr, 32u64, sym.n);
|
wru64(shbuf.ptr, 32u64, sym.n);
|
||||||
wru32(shbuf.ptr, 40u64, 4u32); // sh_link = strtab
|
wru32(shbuf.ptr, 40u64, 4u32); // sh_link = strtab
|
||||||
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = one local (STN_UNDEF)
|
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = one local (STN_UNDEF)
|
||||||
@@ -2115,18 +2115,18 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
// .strtab
|
// .strtab
|
||||||
sn = 0;
|
sn = 0;
|
||||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||||
wru32(shbuf.ptr, 0u64, shn_strtab);
|
wru32(shbuf.ptr, 0u64, shnstrtab);
|
||||||
wru32(shbuf.ptr, 4u64, SHT_STRTAB_C);
|
wru32(shbuf.ptr, 4u64, SHT_STRTAB_C);
|
||||||
wru64(shbuf.ptr, 24u64, off_str);
|
wru64(shbuf.ptr, 24u64, offstr);
|
||||||
wru64(shbuf.ptr, 32u64, str_.n);
|
wru64(shbuf.ptr, 32u64, str_.n);
|
||||||
wru64(shbuf.ptr, 48u64, 1u64);
|
wru64(shbuf.ptr, 48u64, 1u64);
|
||||||
os.writefull(fd, shbuf.ptr, 64u64);
|
os.writefull(fd, shbuf.ptr, 64u64);
|
||||||
// .shstrtab
|
// .shstrtab
|
||||||
sn = 0;
|
sn = 0;
|
||||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||||
wru32(shbuf.ptr, 0u64, shn_shstrtab);
|
wru32(shbuf.ptr, 0u64, shnshstrtab);
|
||||||
wru32(shbuf.ptr, 4u64, SHT_STRTAB_C);
|
wru32(shbuf.ptr, 4u64, SHT_STRTAB_C);
|
||||||
wru64(shbuf.ptr, 24u64, off_shstr);
|
wru64(shbuf.ptr, 24u64, offshstr);
|
||||||
wru64(shbuf.ptr, 32u64, shstr.n);
|
wru64(shbuf.ptr, 32u64, shstr.n);
|
||||||
wru64(shbuf.ptr, 48u64, 1u64);
|
wru64(shbuf.ptr, 48u64, 1u64);
|
||||||
os.writefull(fd, shbuf.ptr, 64u64);
|
os.writefull(fd, shbuf.ptr, 64u64);
|
||||||
|
|||||||
@@ -120,11 +120,11 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
bufputb(&str_, &zero, 1u64);
|
bufputb(&str_, &zero, 1u64);
|
||||||
|
|
||||||
// Section name offsets.
|
// Section name offsets.
|
||||||
let shn_text: u32 = bufputcstr(&shstr, ".text");
|
let shntext: u32 = bufputcstr(&shstr, ".text");
|
||||||
let shn_rela: u32 = bufputcstr(&shstr, ".rela.text");
|
let shnrela: u32 = bufputcstr(&shstr, ".rela.text");
|
||||||
let shn_symtab: u32 = bufputcstr(&shstr, ".symtab");
|
let shnsymtab: u32 = bufputcstr(&shstr, ".symtab");
|
||||||
let shn_strtab: u32 = bufputcstr(&shstr, ".strtab");
|
let shnstrtab: u32 = bufputcstr(&shstr, ".strtab");
|
||||||
let shn_shstrtab: u32 = bufputcstr(&shstr, ".shstrtab");
|
let shnshstrtab: u32 = bufputcstr(&shstr, ".shstrtab");
|
||||||
|
|
||||||
// Symbol 0 — STN_UNDEF (24 zero bytes).
|
// Symbol 0 — STN_UNDEF (24 zero bytes).
|
||||||
let zsym: [24]u8;
|
let zsym: [24]u8;
|
||||||
@@ -141,8 +141,8 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
let entry: [24]u8;
|
let entry: [24]u8;
|
||||||
let ei: i32 = 0;
|
let ei: i32 = 0;
|
||||||
for (ei < 24) { entry[ei] = 0u8; ei += 1; };
|
for (ei < 24) { entry[ei] = 0u8; ei += 1; };
|
||||||
let st_name: u32 = bufputcstr(&str_, s.name);
|
let stname: u32 = bufputcstr(&str_, s.name);
|
||||||
wru32(entry.ptr, 0u64, st_name);
|
wru32(entry.ptr, 0u64, stname);
|
||||||
if (s.defined != 0) {
|
if (s.defined != 0) {
|
||||||
wru8(entry.ptr, 4u64, ((STB_GLOBAL << 4u8) | STT_FUNC));
|
wru8(entry.ptr, 4u64, ((STB_GLOBAL << 4u8) | STT_FUNC));
|
||||||
wru16(entry.ptr, 6u64, SH_TEXT);
|
wru16(entry.ptr, 6u64, SH_TEXT);
|
||||||
@@ -162,8 +162,8 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
for (r != nil) {
|
for (r != nil) {
|
||||||
let entry: [24]u8;
|
let entry: [24]u8;
|
||||||
wru64(entry.ptr, 0u64, r.off);
|
wru64(entry.ptr, 0u64, r.off);
|
||||||
let r_info: u64 = (r.asy.idx: u64 << 32u64) | (r.kind: u64 & 4294967295u64);
|
let rinfo: u64 = (r.asy.idx: u64 << 32u64) | (r.kind: u64 & 4294967295u64);
|
||||||
wru64(entry.ptr, 8u64, r_info);
|
wru64(entry.ptr, 8u64, rinfo);
|
||||||
wru64(entry.ptr, 16u64, r.addend: u64);
|
wru64(entry.ptr, 16u64, r.addend: u64);
|
||||||
bufputb(&rela, entry.ptr, 24u64);
|
bufputb(&rela, entry.ptr, 24u64);
|
||||||
r = r.rnext;
|
r = r.rnext;
|
||||||
@@ -171,13 +171,13 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
|
|
||||||
// File offsets.
|
// File offsets.
|
||||||
let off: u64 = EHDR_SZ;
|
let off: u64 = EHDR_SZ;
|
||||||
let off_text: u64 = off; off = off + a.textlen;
|
let offtext: u64 = off; off = off + a.textlen;
|
||||||
let off_rela: u64 = off; off = off + rela.n;
|
let offrela: u64 = off; off = off + rela.n;
|
||||||
let off_sym: u64 = off; off = off + sym.n;
|
let offsym: u64 = off; off = off + sym.n;
|
||||||
let off_str: u64 = off; off = off + str_.n;
|
let offstr: u64 = off; off = off + str_.n;
|
||||||
let off_shstr: u64 = off; off = off + shstr.n;
|
let offshstr: u64 = off; off = off + shstr.n;
|
||||||
for ((off & 7u64) != 0u64) { off += 1u64; };
|
for ((off & 7u64) != 0u64) { off += 1u64; };
|
||||||
let off_shdr: u64 = off;
|
let offshdr: u64 = off;
|
||||||
let NSECT: u16 = 6u16;
|
let NSECT: u16 = 6u16;
|
||||||
|
|
||||||
// ---- Ehdr ----
|
// ---- Ehdr ----
|
||||||
@@ -196,7 +196,7 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
wru32(eh.ptr, 20u64, EV_CURRENT_W);
|
wru32(eh.ptr, 20u64, EV_CURRENT_W);
|
||||||
wru64(eh.ptr, 24u64, 0u64); // e_entry
|
wru64(eh.ptr, 24u64, 0u64); // e_entry
|
||||||
wru64(eh.ptr, 32u64, 0u64); // e_phoff
|
wru64(eh.ptr, 32u64, 0u64); // e_phoff
|
||||||
wru64(eh.ptr, 40u64, off_shdr); // e_shoff
|
wru64(eh.ptr, 40u64, offshdr); // e_shoff
|
||||||
wru32(eh.ptr, 48u64, 0u32); // e_flags
|
wru32(eh.ptr, 48u64, 0u32); // e_flags
|
||||||
wru16(eh.ptr, 52u64, 64u16); // e_ehsize
|
wru16(eh.ptr, 52u64, 64u16); // e_ehsize
|
||||||
wru16(eh.ptr, 54u64, 0u16); // e_phentsize
|
wru16(eh.ptr, 54u64, 0u16); // e_phentsize
|
||||||
@@ -238,20 +238,20 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
// .text
|
// .text
|
||||||
sn = 0;
|
sn = 0;
|
||||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||||
wru32(shbuf.ptr, 0u64, shn_text);
|
wru32(shbuf.ptr, 0u64, shntext);
|
||||||
wru32(shbuf.ptr, 4u64, SHT_PROGBITS_C);
|
wru32(shbuf.ptr, 4u64, SHT_PROGBITS_C);
|
||||||
wru64(shbuf.ptr, 8u64, SHF_ALLOC | SHF_EXECINSTR);
|
wru64(shbuf.ptr, 8u64, SHF_ALLOC | SHF_EXECINSTR);
|
||||||
wru64(shbuf.ptr, 24u64, off_text);
|
wru64(shbuf.ptr, 24u64, offtext);
|
||||||
wru64(shbuf.ptr, 32u64, a.textlen);
|
wru64(shbuf.ptr, 32u64, a.textlen);
|
||||||
wru64(shbuf.ptr, 48u64, 1u64); // sh_addralign
|
wru64(shbuf.ptr, 48u64, 1u64); // sh_addralign
|
||||||
os.writefull(fd, shbuf.ptr, 64u64);
|
os.writefull(fd, shbuf.ptr, 64u64);
|
||||||
// .rela.text
|
// .rela.text
|
||||||
sn = 0;
|
sn = 0;
|
||||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||||
wru32(shbuf.ptr, 0u64, shn_rela);
|
wru32(shbuf.ptr, 0u64, shnrela);
|
||||||
wru32(shbuf.ptr, 4u64, SHT_RELA_C);
|
wru32(shbuf.ptr, 4u64, SHT_RELA_C);
|
||||||
wru64(shbuf.ptr, 8u64, SHF_INFO_LINK);
|
wru64(shbuf.ptr, 8u64, SHF_INFO_LINK);
|
||||||
wru64(shbuf.ptr, 24u64, off_rela);
|
wru64(shbuf.ptr, 24u64, offrela);
|
||||||
wru64(shbuf.ptr, 32u64, rela.n);
|
wru64(shbuf.ptr, 32u64, rela.n);
|
||||||
wru32(shbuf.ptr, 40u64, 3u32); // sh_link = symtab idx
|
wru32(shbuf.ptr, 40u64, 3u32); // sh_link = symtab idx
|
||||||
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = .text idx
|
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = .text idx
|
||||||
@@ -261,9 +261,9 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
// .symtab
|
// .symtab
|
||||||
sn = 0;
|
sn = 0;
|
||||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||||
wru32(shbuf.ptr, 0u64, shn_symtab);
|
wru32(shbuf.ptr, 0u64, shnsymtab);
|
||||||
wru32(shbuf.ptr, 4u64, SHT_SYMTAB_C);
|
wru32(shbuf.ptr, 4u64, SHT_SYMTAB_C);
|
||||||
wru64(shbuf.ptr, 24u64, off_sym);
|
wru64(shbuf.ptr, 24u64, offsym);
|
||||||
wru64(shbuf.ptr, 32u64, sym.n);
|
wru64(shbuf.ptr, 32u64, sym.n);
|
||||||
wru32(shbuf.ptr, 40u64, 4u32); // sh_link = strtab
|
wru32(shbuf.ptr, 40u64, 4u32); // sh_link = strtab
|
||||||
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = one local (STN_UNDEF)
|
wru32(shbuf.ptr, 44u64, 1u32); // sh_info = one local (STN_UNDEF)
|
||||||
@@ -273,18 +273,18 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
|
|||||||
// .strtab
|
// .strtab
|
||||||
sn = 0;
|
sn = 0;
|
||||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||||
wru32(shbuf.ptr, 0u64, shn_strtab);
|
wru32(shbuf.ptr, 0u64, shnstrtab);
|
||||||
wru32(shbuf.ptr, 4u64, SHT_STRTAB_C);
|
wru32(shbuf.ptr, 4u64, SHT_STRTAB_C);
|
||||||
wru64(shbuf.ptr, 24u64, off_str);
|
wru64(shbuf.ptr, 24u64, offstr);
|
||||||
wru64(shbuf.ptr, 32u64, str_.n);
|
wru64(shbuf.ptr, 32u64, str_.n);
|
||||||
wru64(shbuf.ptr, 48u64, 1u64);
|
wru64(shbuf.ptr, 48u64, 1u64);
|
||||||
os.writefull(fd, shbuf.ptr, 64u64);
|
os.writefull(fd, shbuf.ptr, 64u64);
|
||||||
// .shstrtab
|
// .shstrtab
|
||||||
sn = 0;
|
sn = 0;
|
||||||
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
|
||||||
wru32(shbuf.ptr, 0u64, shn_shstrtab);
|
wru32(shbuf.ptr, 0u64, shnshstrtab);
|
||||||
wru32(shbuf.ptr, 4u64, SHT_STRTAB_C);
|
wru32(shbuf.ptr, 4u64, SHT_STRTAB_C);
|
||||||
wru64(shbuf.ptr, 24u64, off_shstr);
|
wru64(shbuf.ptr, 24u64, offshstr);
|
||||||
wru64(shbuf.ptr, 32u64, shstr.n);
|
wru64(shbuf.ptr, 32u64, shstr.n);
|
||||||
wru64(shbuf.ptr, 48u64, 1u64);
|
wru64(shbuf.ptr, 48u64, 1u64);
|
||||||
os.writefull(fd, shbuf.ptr, 64u64);
|
os.writefull(fd, shbuf.ptr, 64u64);
|
||||||
|
|||||||
@@ -259,12 +259,12 @@ fn parseoperand(a: *asm_, p: *u8, offin: u64, n: u64, out: *aoperand) u64 = {
|
|||||||
|
|
||||||
// number(REG) — possibly signed — or bare $NUM-less constant
|
// number(REG) — possibly signed — or bare $NUM-less constant
|
||||||
let cur: u64 = off;
|
let cur: u64 = off;
|
||||||
let is_num: bool = false;
|
let isnum: bool = false;
|
||||||
if (cur < n) {
|
if (cur < n) {
|
||||||
if (p[cur] == 45u8) { is_num = true; }
|
if (p[cur] == 45u8) { isnum = true; }
|
||||||
else { if (p[cur] >= 48u8) { if (p[cur] <= 57u8) { is_num = true; }; }; };
|
else { if (p[cur] >= 48u8) { if (p[cur] <= 57u8) { isnum = true; }; }; };
|
||||||
};
|
};
|
||||||
if (is_num) {
|
if (isnum) {
|
||||||
let v: i64;
|
let v: i64;
|
||||||
let used: u64;
|
let used: u64;
|
||||||
v, used = parsenum(p + off, n - off);
|
v, used = parsenum(p + off, n - off);
|
||||||
@@ -392,11 +392,11 @@ export fn parse(a: *asm_) i32 = {
|
|||||||
if (line[0u64] != 9u8) {
|
if (line[0u64] != 9u8) {
|
||||||
if (isidstart(line[i]: i32)) {
|
if (isidstart(line[i]: i32)) {
|
||||||
let q: u64 = i;
|
let q: u64 = i;
|
||||||
let scan_id: bool = true;
|
let scanid: bool = true;
|
||||||
for (scan_id) {
|
for (scanid) {
|
||||||
if (q >= n) { scan_id = false; }
|
if (q >= n) { scanid = false; }
|
||||||
else { if (isidcont(line[q]: i32)) { q += 1u64; }
|
else { if (isidcont(line[q]: i32)) { q += 1u64; }
|
||||||
else { scan_id = false; }; };
|
else { scanid = false; }; };
|
||||||
};
|
};
|
||||||
if (q < n) { if (line[q] == 58u8) { // ':'
|
if (q < n) { if (line[q] == 58u8) { // ':'
|
||||||
let nm: str = dupstr(a.a, line + i, q - i);
|
let nm: str = dupstr(a.a, line + i, q - i);
|
||||||
@@ -441,26 +441,26 @@ export fn parse(a: *asm_) i32 = {
|
|||||||
if (opc == A_TEXT) {
|
if (opc == A_TEXT) {
|
||||||
// TEXT name,$framesize — find first ',' as the end of name.
|
// TEXT name,$framesize — find first ',' as the end of name.
|
||||||
let q: u64 = r0;
|
let q: u64 = r0;
|
||||||
let comma_pos: u64 = n;
|
let commapos: u64 = n;
|
||||||
let scan_t: bool = true;
|
let scant: bool = true;
|
||||||
for (scan_t) {
|
for (scant) {
|
||||||
if (q >= n) { scan_t = false; }
|
if (q >= n) { scant = false; }
|
||||||
else { if (line[q] == 44u8) { comma_pos = q; scan_t = false; }
|
else { if (line[q] == 44u8) { commapos = q; scant = false; }
|
||||||
else { q += 1u64; }; };
|
else { q += 1u64; }; };
|
||||||
};
|
};
|
||||||
let to_op: *aoperand = pr.to;
|
let toop: *aoperand = pr.to;
|
||||||
to_op.atype = D_EXTERN;
|
toop.atype = D_EXTERN;
|
||||||
to_op.asym = dupstr(a.a, line + r0, comma_pos - r0);
|
toop.asym = dupstr(a.a, line + r0, commapos - r0);
|
||||||
if (comma_pos < n) {
|
if (commapos < n) {
|
||||||
let p2: u64 = comma_pos + 1u64;
|
let p2: u64 = commapos + 1u64;
|
||||||
p2 = skipws(line, p2, n);
|
p2 = skipws(line, p2, n);
|
||||||
if (p2 < n) { if (line[p2] == 36u8) { p2 += 1u64; }; };
|
if (p2 < n) { if (line[p2] == 36u8) { p2 += 1u64; }; };
|
||||||
let v: i64;
|
let v: i64;
|
||||||
let used: u64;
|
let used: u64;
|
||||||
v, used = parsenum(line + p2, n - p2);
|
v, used = parsenum(line + p2, n - p2);
|
||||||
let from_op: *aoperand = pr.from;
|
let fromop: *aoperand = pr.from;
|
||||||
from_op.atype = D_CONST;
|
fromop.atype = D_CONST;
|
||||||
from_op.offset = v;
|
fromop.offset = v;
|
||||||
};
|
};
|
||||||
a.line += 1; continue;
|
a.line += 1; continue;
|
||||||
};
|
};
|
||||||
@@ -469,31 +469,31 @@ export fn parse(a: *asm_) i32 = {
|
|||||||
// DATA name(SB),"escaped bytes" — find first '(' as end of name.
|
// DATA name(SB),"escaped bytes" — find first '(' as end of name.
|
||||||
let q: u64 = r0;
|
let q: u64 = r0;
|
||||||
let lparen: u64 = n;
|
let lparen: u64 = n;
|
||||||
let scan_d: bool = true;
|
let scand: bool = true;
|
||||||
for (scan_d) {
|
for (scand) {
|
||||||
if (q >= n) { scan_d = false; }
|
if (q >= n) { scand = false; }
|
||||||
else { if (line[q] == 40u8) { lparen = q; scan_d = false; }
|
else { if (line[q] == 40u8) { lparen = q; scand = false; }
|
||||||
else { q += 1u64; }; };
|
else { q += 1u64; }; };
|
||||||
};
|
};
|
||||||
let to_op: *aoperand = pr.to;
|
let toop: *aoperand = pr.to;
|
||||||
to_op.atype = D_EXTERN;
|
toop.atype = D_EXTERN;
|
||||||
to_op.asym = dupstr(a.a, line + r0, lparen - r0);
|
toop.asym = dupstr(a.a, line + r0, lparen - r0);
|
||||||
// Skip past `(SB)` to land just after ')'.
|
// Skip past `(SB)` to land just after ')'.
|
||||||
let p2: u64 = lparen;
|
let p2: u64 = lparen;
|
||||||
let scan_d2: bool = true;
|
let scand2: bool = true;
|
||||||
for (scan_d2) {
|
for (scand2) {
|
||||||
if (p2 >= n) { scan_d2 = false; }
|
if (p2 >= n) { scand2 = false; }
|
||||||
else { if (line[p2] == 41u8) { p2 += 1u64; scan_d2 = false; }
|
else { if (line[p2] == 41u8) { p2 += 1u64; scand2 = false; }
|
||||||
else { p2 += 1u64; }; };
|
else { p2 += 1u64; }; };
|
||||||
};
|
};
|
||||||
// Skip ws / ',' / tab between `)` and the `"`.
|
// Skip ws / ',' / tab between `)` and the `"`.
|
||||||
let scan_d3: bool = true;
|
let scand3: bool = true;
|
||||||
for (scan_d3) {
|
for (scand3) {
|
||||||
if (p2 >= n) { scan_d3 = false; }
|
if (p2 >= n) { scand3 = false; }
|
||||||
else { if (line[p2] == 32u8) { p2 += 1u64; }
|
else { if (line[p2] == 32u8) { p2 += 1u64; }
|
||||||
else { if (line[p2] == 44u8) { p2 += 1u64; }
|
else { if (line[p2] == 44u8) { p2 += 1u64; }
|
||||||
else { if (line[p2] == 9u8) { p2 += 1u64; }
|
else { if (line[p2] == 9u8) { p2 += 1u64; }
|
||||||
else { scan_d3 = false; }; }; }; };
|
else { scand3 = false; }; }; }; };
|
||||||
};
|
};
|
||||||
if (p2 >= n) { perr(a, "DATA missing payload"); a.line += 1; continue; };
|
if (p2 >= n) { perr(a, "DATA missing payload"); a.line += 1; continue; };
|
||||||
if (line[p2] != 34u8) { perr(a, "DATA expects \"...\""); a.line += 1; continue; };
|
if (line[p2] != 34u8) { perr(a, "DATA expects \"...\""); a.line += 1; continue; };
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -141,21 +141,21 @@ fn readallso(path: *u8) (*u8, u64) = {
|
|||||||
// the .so's verstr buffer) for the entry whose vd_ndx == ndx. The name
|
// the .so's verstr buffer) for the entry whose vd_ndx == ndx. The name
|
||||||
// is the first Verdaux's vda_name (subsequent auxes are predecessor
|
// is the first Verdaux's vda_name (subsequent auxes are predecessor
|
||||||
// names). Returns nil if no entry matches.
|
// names). Returns nil if no entry matches.
|
||||||
fn vdnameat(buf: *u8, verdef_off: u64, verdef_size: u64,
|
fn vdnameat(buf: *u8, verdefoff: u64, verdefsize: u64,
|
||||||
verstr: *u8, ndx: u16) *u8 = {
|
verstr: *u8, ndx: u16) *u8 = {
|
||||||
let off: u64 = 0u64;
|
let off: u64 = 0u64;
|
||||||
for (off < verdef_size) {
|
for (off < verdefsize) {
|
||||||
let vd_p: u64 = verdef_off + off;
|
let vdp: u64 = verdefoff + off;
|
||||||
let vd_ndx: u16 = du16(buf, vd_p + VD_NDX);
|
let vdndx: u16 = du16(buf, vdp + VD_NDX);
|
||||||
let vd_aux: u32 = du32(buf, vd_p + VD_AUX);
|
let vdaux: u32 = du32(buf, vdp + VD_AUX);
|
||||||
let vd_next: u32 = du32(buf, vd_p + VD_NEXT);
|
let vdnext: u32 = du32(buf, vdp + VD_NEXT);
|
||||||
if (vd_ndx == ndx) {
|
if (vdndx == ndx) {
|
||||||
let aux_p: u64 = vd_p + (vd_aux: u64);
|
let auxp: u64 = vdp + (vdaux: u64);
|
||||||
let vda_name: u32 = du32(buf, aux_p + VA_NAME);
|
let vdaname: u32 = du32(buf, auxp + VA_NAME);
|
||||||
return verstr + (vda_name: u64);
|
return verstr + (vdaname: u64);
|
||||||
};
|
};
|
||||||
if (vd_next == 0u32) { return nil; };
|
if (vdnext == 0u32) { return nil; };
|
||||||
off += vd_next: u64;
|
off += vdnext: u64;
|
||||||
};
|
};
|
||||||
return nil;
|
return nil;
|
||||||
};
|
};
|
||||||
@@ -192,56 +192,56 @@ export fn loadso(l: *lnk, path: *u8) i32 = {
|
|||||||
if (shnum == 0u32) { return soerr("stripped .so unsupported"); };
|
if (shnum == 0u32) { return soerr("stripped .so unsupported"); };
|
||||||
|
|
||||||
// Locate the four sections we care about.
|
// Locate the four sections we care about.
|
||||||
let idx_dynsym: i32 = -1;
|
let idxdynsym: i32 = -1;
|
||||||
let idx_dynamic: i32 = -1;
|
let idxdynamic: i32 = -1;
|
||||||
let idx_versym: i32 = -1;
|
let idxversym: i32 = -1;
|
||||||
let idx_verdef: i32 = -1;
|
let idxverdef: i32 = -1;
|
||||||
let i: u32 = 0u32;
|
let i: u32 = 0u32;
|
||||||
for (i < shnum) {
|
for (i < shnum) {
|
||||||
let sh_p: u64 = shoff + (i: u64) * SH_SIZE;
|
let shp: u64 = shoff + (i: u64) * SH_SIZE;
|
||||||
let sh_type: u32 = du32(buf, sh_p + SH_TYPE);
|
let shtype: u32 = du32(buf, shp + SH_TYPE);
|
||||||
if (sh_type == SHT_DYNSYM) { idx_dynsym = i: i32; };
|
if (shtype == SHT_DYNSYM) { idxdynsym = i: i32; };
|
||||||
if (sh_type == SHT_DYNAMIC) { idx_dynamic = i: i32; };
|
if (shtype == SHT_DYNAMIC) { idxdynamic = i: i32; };
|
||||||
if (sh_type == SHT_GNU_VERSYM) { idx_versym = i: i32; };
|
if (shtype == SHT_GNU_VERSYM) { idxversym = i: i32; };
|
||||||
if (sh_type == SHT_GNU_VERDEF) { idx_verdef = i: i32; };
|
if (shtype == SHT_GNU_VERDEF) { idxverdef = i: i32; };
|
||||||
i += 1u32;
|
i += 1u32;
|
||||||
};
|
};
|
||||||
if (idx_dynsym < 0) {
|
if (idxdynsym < 0) {
|
||||||
return soerr("no .dynsym");
|
return soerr("no .dynsym");
|
||||||
};
|
};
|
||||||
|
|
||||||
let dynsym_sh: u64 = shoff + (idx_dynsym: u64) * SH_SIZE;
|
let dynsymsh: u64 = shoff + (idxdynsym: u64) * SH_SIZE;
|
||||||
let dynsym_off: u64 = du64(buf, dynsym_sh + SH_OFFSET);
|
let dynsymoff: u64 = du64(buf, dynsymsh + SH_OFFSET);
|
||||||
let dynsym_size: u64 = du64(buf, dynsym_sh + SH_SIZE_F);
|
let dynsymsize: u64 = du64(buf, dynsymsh + SH_SIZE_F);
|
||||||
let dynsym_link: u32 = du32(buf, dynsym_sh + SH_LINK);
|
let dynsymlink: u32 = du32(buf, dynsymsh + SH_LINK);
|
||||||
let nsyms: u64 = dynsym_size / SY_SIZE;
|
let nsyms: u64 = dynsymsize / SY_SIZE;
|
||||||
|
|
||||||
let dynstr_sh: u64 = shoff + (dynsym_link: u64) * SH_SIZE;
|
let dynstrsh: u64 = shoff + (dynsymlink: u64) * SH_SIZE;
|
||||||
let dynstr_off: u64 = du64(buf, dynstr_sh + SH_OFFSET);
|
let dynstroff: u64 = du64(buf, dynstrsh + SH_OFFSET);
|
||||||
let dynstr: *u8 = buf + dynstr_off;
|
let dynstr: *u8 = buf + dynstroff;
|
||||||
|
|
||||||
// SONAME: .dynamic strings live in the section pointed at by its
|
// SONAME: .dynamic strings live in the section pointed at by its
|
||||||
// sh_link (almost always .dynstr).
|
// sh_link (almost always .dynstr).
|
||||||
let soname_cs: *u8 = nil;
|
let sonamecs: *u8 = nil;
|
||||||
if (idx_dynamic >= 0) {
|
if (idxdynamic >= 0) {
|
||||||
let dyn_sh: u64 = shoff + (idx_dynamic: u64) * SH_SIZE;
|
let dynsh: u64 = shoff + (idxdynamic: u64) * SH_SIZE;
|
||||||
let dyn_off: u64 = du64(buf, dyn_sh + SH_OFFSET);
|
let dynoff: u64 = du64(buf, dynsh + SH_OFFSET);
|
||||||
let dyn_size: u64 = du64(buf, dyn_sh + SH_SIZE_F);
|
let dynsize: u64 = du64(buf, dynsh + SH_SIZE_F);
|
||||||
let dyn_link: u32 = du32(buf, dyn_sh + SH_LINK);
|
let dynlink: u32 = du32(buf, dynsh + SH_LINK);
|
||||||
let dstr_sh: u64 = shoff + (dyn_link: u64) * SH_SIZE;
|
let dstrsh: u64 = shoff + (dynlink: u64) * SH_SIZE;
|
||||||
let dstr_off: u64 = du64(buf, dstr_sh + SH_OFFSET);
|
let dstroff: u64 = du64(buf, dstrsh + SH_OFFSET);
|
||||||
let dstr: *u8 = buf + dstr_off;
|
let dstr: *u8 = buf + dstroff;
|
||||||
let nd: u64 = dyn_size / DY_SIZE;
|
let nd: u64 = dynsize / DY_SIZE;
|
||||||
let di: u64 = 0u64;
|
let di: u64 = 0u64;
|
||||||
for (di < nd) {
|
for (di < nd) {
|
||||||
let d_p: u64 = dyn_off + di * DY_SIZE;
|
let dp: u64 = dynoff + di * DY_SIZE;
|
||||||
let d_tag: i64 = di64(buf, d_p + DY_TAG);
|
let dtag: i64 = di64(buf, dp + DY_TAG);
|
||||||
if (d_tag == DT_NULL_TAG) {
|
if (dtag == DT_NULL_TAG) {
|
||||||
di = nd; // break
|
di = nd; // break
|
||||||
} else {
|
} else {
|
||||||
if (d_tag == DT_SONAME_TAG) {
|
if (dtag == DT_SONAME_TAG) {
|
||||||
let d_val: u64 = du64(buf, d_p + DY_VAL);
|
let dval: u64 = du64(buf, dp + DY_VAL);
|
||||||
soname_cs = dstr + d_val;
|
sonamecs = dstr + dval;
|
||||||
di = nd; // break
|
di = nd; // break
|
||||||
} else {
|
} else {
|
||||||
di += 1u64;
|
di += 1u64;
|
||||||
@@ -249,66 +249,66 @@ export fn loadso(l: *lnk, path: *u8) i32 = {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
if (soname_cs == nil) {
|
if (sonamecs == nil) {
|
||||||
soname_cs = dbasename(path);
|
sonamecs = dbasename(path);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Versym is one u16 per dynsym entry.
|
// Versym is one u16 per dynsym entry.
|
||||||
let versym_off: u64 = 0u64;
|
let versymoff: u64 = 0u64;
|
||||||
let has_versym: i32 = 0;
|
let hasversym: i32 = 0;
|
||||||
if (idx_versym >= 0) {
|
if (idxversym >= 0) {
|
||||||
let vs_sh: u64 = shoff + (idx_versym: u64) * SH_SIZE;
|
let vssh: u64 = shoff + (idxversym: u64) * SH_SIZE;
|
||||||
versym_off = du64(buf, vs_sh + SH_OFFSET);
|
versymoff = du64(buf, vssh + SH_OFFSET);
|
||||||
has_versym = 1;
|
hasversym = 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Verdef section bounds + the .dynstr-like string section it uses.
|
// Verdef section bounds + the .dynstr-like string section it uses.
|
||||||
let verdef_off: u64 = 0u64;
|
let verdefoff: u64 = 0u64;
|
||||||
let verdef_size: u64 = 0u64;
|
let verdefsize: u64 = 0u64;
|
||||||
let verstr: *u8 = nil;
|
let verstr: *u8 = nil;
|
||||||
if (idx_verdef >= 0) {
|
if (idxverdef >= 0) {
|
||||||
let vd_sh: u64 = shoff + (idx_verdef: u64) * SH_SIZE;
|
let vdsh: u64 = shoff + (idxverdef: u64) * SH_SIZE;
|
||||||
verdef_off = du64(buf, vd_sh + SH_OFFSET);
|
verdefoff = du64(buf, vdsh + SH_OFFSET);
|
||||||
verdef_size = du64(buf, vd_sh + SH_SIZE_F);
|
verdefsize = du64(buf, vdsh + SH_SIZE_F);
|
||||||
let vd_link: u32 = du32(buf, vd_sh + SH_LINK);
|
let vdlink: u32 = du32(buf, vdsh + SH_LINK);
|
||||||
let vstr_sh: u64 = shoff + (vd_link: u64) * SH_SIZE;
|
let vstrsh: u64 = shoff + (vdlink: u64) * SH_SIZE;
|
||||||
let vstr_off: u64 = du64(buf, vstr_sh + SH_OFFSET);
|
let vstroff: u64 = du64(buf, vstrsh + SH_OFFSET);
|
||||||
verstr = buf + vstr_off;
|
verstr = buf + vstroff;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Build the lso. Exports are appended in dynsym order so
|
// Build the lso. Exports are appended in dynsym order so
|
||||||
// soprovides_v's first-match semantics match the C version.
|
// soprovides_v's first-match semantics match the C version.
|
||||||
let so: *lso = amalloc(l.a, 64u64): *lso;
|
let so: *lso = amalloc(l.a, 64u64): *lso;
|
||||||
so.path = dcstrtostr(l.a, path);
|
so.path = dcstrtostr(l.a, path);
|
||||||
so.soname = dcstrtostr(l.a, soname_cs);
|
so.soname = dcstrtostr(l.a, sonamecs);
|
||||||
so.exports = nil;
|
so.exports = nil;
|
||||||
let tail: *lexport = nil;
|
let tail: *lexport = nil;
|
||||||
|
|
||||||
let si: u64 = 1u64;
|
let si: u64 = 1u64;
|
||||||
for (si < nsyms) {
|
for (si < nsyms) {
|
||||||
let s_p: u64 = dynsym_off + si * SY_SIZE;
|
let sp: u64 = dynsymoff + si * SY_SIZE;
|
||||||
let st_shndx: u16 = du16(buf, s_p + SY_SHNDX);
|
let stshndx: u16 = du16(buf, sp + SY_SHNDX);
|
||||||
if (st_shndx == 0u16) { si += 1u64; } else {
|
if (stshndx == 0u16) { si += 1u64; } else {
|
||||||
let st_info: u8 = buf[s_p + SY_INFO];
|
let stinfo: u8 = buf[sp + SY_INFO];
|
||||||
let bind: u32 = (st_info: u32) >> 4u32;
|
let bind: u32 = (stinfo: u32) >> 4u32;
|
||||||
if (bind != 1u32) { if (bind != 2u32) {
|
if (bind != 1u32) { if (bind != 2u32) {
|
||||||
// not GLOBAL/WEAK
|
// not GLOBAL/WEAK
|
||||||
si += 1u64;
|
si += 1u64;
|
||||||
continue;
|
continue;
|
||||||
}; };
|
}; };
|
||||||
let st_name: u32 = du32(buf, s_p + SY_NAME);
|
let stname: u32 = du32(buf, sp + SY_NAME);
|
||||||
let nm_p: *u8 = dynstr + (st_name: u64);
|
let nmp: *u8 = dynstr + (stname: u64);
|
||||||
if (nm_p[0u64] == 0u8) {
|
if (nmp[0u64] == 0u8) {
|
||||||
si += 1u64;
|
si += 1u64;
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Determine version. Skip non-default (hidden) and
|
// Determine version. Skip non-default (hidden) and
|
||||||
// local entries.
|
// local entries.
|
||||||
let vername_cs: *u8 = nil;
|
let vernamecs: *u8 = nil;
|
||||||
let keep: i32 = 1;
|
let keep: i32 = 1;
|
||||||
if (has_versym != 0) {
|
if (hasversym != 0) {
|
||||||
let v: u16 = du16(buf, versym_off + si * 2u64);
|
let v: u16 = du16(buf, versymoff + si * 2u64);
|
||||||
if ((v & VERSYM_HIDDEN_C) != 0u16) {
|
if ((v & VERSYM_HIDDEN_C) != 0u16) {
|
||||||
keep = 0; // non-default
|
keep = 0; // non-default
|
||||||
} else {
|
} else {
|
||||||
@@ -316,17 +316,17 @@ export fn loadso(l: *lnk, path: *u8) i32 = {
|
|||||||
if (vidx == VER_NDX_LOCAL_C) {
|
if (vidx == VER_NDX_LOCAL_C) {
|
||||||
keep = 0; // not exported
|
keep = 0; // not exported
|
||||||
} else { if (vidx == VER_NDX_GLOBAL_C) {
|
} else { if (vidx == VER_NDX_GLOBAL_C) {
|
||||||
vername_cs = nil;
|
vernamecs = nil;
|
||||||
} else { if (vidx == 1u16) {
|
} else { if (vidx == 1u16) {
|
||||||
// glibc's BASE entry: treat as
|
// glibc's BASE entry: treat as
|
||||||
// unversioned. (The C version
|
// unversioned. (The C version
|
||||||
// notes that vidx==1 in Verdef
|
// notes that vidx==1 in Verdef
|
||||||
// maps to the SONAME BASE.)
|
// maps to the SONAME BASE.)
|
||||||
vername_cs = nil;
|
vernamecs = nil;
|
||||||
} else {
|
} else {
|
||||||
if (verstr != nil) {
|
if (verstr != nil) {
|
||||||
let nm: *u8 = vdnameat(buf, verdef_off, verdef_size, verstr, vidx);
|
let nm: *u8 = vdnameat(buf, verdefoff, verdefsize, verstr, vidx);
|
||||||
vername_cs = nm;
|
vernamecs = nm;
|
||||||
};
|
};
|
||||||
}; }; };
|
}; }; };
|
||||||
};
|
};
|
||||||
@@ -334,12 +334,12 @@ export fn loadso(l: *lnk, path: *u8) i32 = {
|
|||||||
|
|
||||||
if (keep != 0) {
|
if (keep != 0) {
|
||||||
let e: *lexport = amalloc(l.a, 48u64): *lexport;
|
let e: *lexport = amalloc(l.a, 48u64): *lexport;
|
||||||
e.name = dcstrtostr(l.a, nm_p);
|
e.name = dcstrtostr(l.a, nmp);
|
||||||
if (vername_cs == nil) {
|
if (vernamecs == nil) {
|
||||||
e.version.ptr = nil;
|
e.version.ptr = nil;
|
||||||
e.version.len = 0i32;
|
e.version.len = 0i32;
|
||||||
} else {
|
} else {
|
||||||
e.version = dcstrtostr(l.a, vername_cs);
|
e.version = dcstrtostr(l.a, vernamecs);
|
||||||
};
|
};
|
||||||
e.enext = nil;
|
e.enext = nil;
|
||||||
if (tail == nil) {
|
if (tail == nil) {
|
||||||
|
|||||||
@@ -203,10 +203,10 @@ export fn emitdynelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// ---- collect used .so's (in l.sos order) ----
|
// ---- collect used .so's (in l.sos order) ----
|
||||||
let max_sos: i32 = 0;
|
let maxsos: i32 = 0;
|
||||||
let so: *lso = l.sos;
|
let so: *lso = l.sos;
|
||||||
for (so != nil) { max_sos += 1; so = so.sonext; };
|
for (so != nil) { maxsos += 1; so = so.sonext; };
|
||||||
let sos_used: **lso = amalloc(a, (max_sos: u64) * 8u64): **lso;
|
let sosused: **lso = amalloc(a, (maxsos: u64) * 8u64): **lso;
|
||||||
let nsos: i32 = 0;
|
let nsos: i32 = 0;
|
||||||
so = l.sos;
|
so = l.sos;
|
||||||
for (so != nil) {
|
for (so != nil) {
|
||||||
@@ -219,7 +219,7 @@ export fn emitdynelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
|
|||||||
else { j += 1; };
|
else { j += 1; };
|
||||||
};
|
};
|
||||||
if (used != 0) {
|
if (used != 0) {
|
||||||
sos_used[nsos] = so;
|
sosused[nsos] = so;
|
||||||
nsos += 1;
|
nsos += 1;
|
||||||
};
|
};
|
||||||
so = so.sonext;
|
so = so.sonext;
|
||||||
@@ -235,155 +235,155 @@ export fn emitdynelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
|
|||||||
// ver_dynstr_off[v] = offset within .dynstr (assigned after layout).
|
// ver_dynstr_off[v] = offset within .dynstr (assigned after layout).
|
||||||
// ver_vna_other[v] = versym index (starting at 2).
|
// ver_vna_other[v] = versym index (starting at 2).
|
||||||
|
|
||||||
let vlib_sos_idx_buf: *u8 = amalloc(a, (max_sos: u64) * 4u64): *u8;
|
let vlibsosidxbuf: *u8 = amalloc(a, (maxsos: u64) * 4u64): *u8;
|
||||||
let vlib_first_buf: *u8 = amalloc(a, (max_sos: u64) * 4u64): *u8;
|
let vlibfirstbuf: *u8 = amalloc(a, (maxsos: u64) * 4u64): *u8;
|
||||||
let vlib_count_buf: *u8 = amalloc(a, (max_sos: u64) * 4u64): *u8;
|
let vlibcountbuf: *u8 = amalloc(a, (maxsos: u64) * 4u64): *u8;
|
||||||
let n_vlibs: i32 = 0;
|
let nvlibs: i32 = 0;
|
||||||
|
|
||||||
let ver_lib_idx_buf: *u8 = amalloc(a, nu * 4u64): *u8;
|
let verlibidxbuf: *u8 = amalloc(a, nu * 4u64): *u8;
|
||||||
let ver_name_ptr_arr: **u8 = amalloc(a, nu * 8u64): **u8;
|
let vernameptrarr: **u8 = amalloc(a, nu * 8u64): **u8;
|
||||||
let ver_name_len_buf: *u8 = amalloc(a, nu * 4u64): *u8;
|
let vernamelenbuf: *u8 = amalloc(a, nu * 4u64): *u8;
|
||||||
let ver_dynstr_off: *u8 = amalloc(a, nu * 4u64): *u8;
|
let verdynstroff: *u8 = amalloc(a, nu * 4u64): *u8;
|
||||||
let ver_vna_other: *u8 = amalloc(a, nu * 2u64): *u8;
|
let vervnaother: *u8 = amalloc(a, nu * 2u64): *u8;
|
||||||
let n_vers: i32 = 0;
|
let nvers: i32 = 0;
|
||||||
|
|
||||||
let si: i32 = 0;
|
let si: i32 = 0;
|
||||||
for (si < nsos) {
|
for (si < nsos) {
|
||||||
let cur_so: *lso = sos_used[si];
|
let curso: *lso = sosused[si];
|
||||||
let has: i32 = 0;
|
let has: i32 = 0;
|
||||||
let j: i32 = 0;
|
let j: i32 = 0;
|
||||||
for (j < n) {
|
for (j < n) {
|
||||||
let dsm: *lsym = dynsyms[j];
|
let dsm: *lsym = dynsyms[j];
|
||||||
let dl: *lso = dsm.dynlib;
|
let dl: *lso = dsm.dynlib;
|
||||||
if (dl == cur_so) {
|
if (dl == curso) {
|
||||||
let nm0: str = dsm.name;
|
let nm0: str = dsm.name;
|
||||||
let dv: str = soversion(cur_so, nm0);
|
let dv: str = soversion(curso, nm0);
|
||||||
if (dv.len > 0) {
|
if (dv.len > 0) {
|
||||||
has = 1; j = n;
|
has = 1; j = n;
|
||||||
} else { j += 1; };
|
} else { j += 1; };
|
||||||
} else { j += 1; };
|
} else { j += 1; };
|
||||||
};
|
};
|
||||||
if (has != 0) {
|
if (has != 0) {
|
||||||
dwr32(vlib_sos_idx_buf, (n_vlibs: u64) * 4u64, si: u32);
|
dwr32(vlibsosidxbuf, (nvlibs: u64) * 4u64, si: u32);
|
||||||
dwr32(vlib_first_buf, (n_vlibs: u64) * 4u64, n_vers: u32);
|
dwr32(vlibfirstbuf, (nvlibs: u64) * 4u64, nvers: u32);
|
||||||
let added: i32 = 0;
|
let added: i32 = 0;
|
||||||
let jj: i32 = 0;
|
let jj: i32 = 0;
|
||||||
for (jj < n) {
|
for (jj < n) {
|
||||||
let dsm2: *lsym = dynsyms[jj];
|
let dsm2: *lsym = dynsyms[jj];
|
||||||
let dl2: *lso = dsm2.dynlib;
|
let dl2: *lso = dsm2.dynlib;
|
||||||
if (dl2 == cur_so) {
|
if (dl2 == curso) {
|
||||||
let nm2: str = dsm2.name;
|
let nm2: str = dsm2.name;
|
||||||
let vname: str = soversion(cur_so, nm2);
|
let vname: str = soversion(curso, nm2);
|
||||||
if (vname.len > 0) {
|
if (vname.len > 0) {
|
||||||
let seen: i32 = 0;
|
let seen: i32 = 0;
|
||||||
let k: i32 = 0;
|
let k: i32 = 0;
|
||||||
for (k < added) {
|
for (k < added) {
|
||||||
let kk: i32 = n_vers - added + k;
|
let kk: i32 = nvers - added + k;
|
||||||
let existing: str;
|
let existing: str;
|
||||||
existing.ptr = ver_name_ptr_arr[kk];
|
existing.ptr = vernameptrarr[kk];
|
||||||
existing.len = drdi32(ver_name_len_buf, (kk: u64) * 4u64);
|
existing.len = drdi32(vernamelenbuf, (kk: u64) * 4u64);
|
||||||
if (streqd(existing, vname)) {
|
if (streqd(existing, vname)) {
|
||||||
seen = 1; k = added;
|
seen = 1; k = added;
|
||||||
} else { k += 1; };
|
} else { k += 1; };
|
||||||
};
|
};
|
||||||
if (seen == 0) {
|
if (seen == 0) {
|
||||||
dwr32(ver_lib_idx_buf, (n_vers: u64) * 4u64, n_vlibs: u32);
|
dwr32(verlibidxbuf, (nvers: u64) * 4u64, nvlibs: u32);
|
||||||
ver_name_ptr_arr[n_vers] = vname.ptr;
|
vernameptrarr[nvers] = vname.ptr;
|
||||||
dwri32(ver_name_len_buf, (n_vers: u64) * 4u64, vname.len);
|
dwri32(vernamelenbuf, (nvers: u64) * 4u64, vname.len);
|
||||||
n_vers += 1;
|
nvers += 1;
|
||||||
added += 1;
|
added += 1;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
jj += 1;
|
jj += 1;
|
||||||
};
|
};
|
||||||
dwr32(vlib_count_buf, (n_vlibs: u64) * 4u64, added: u32);
|
dwr32(vlibcountbuf, (nvlibs: u64) * 4u64, added: u32);
|
||||||
n_vlibs += 1;
|
nvlibs += 1;
|
||||||
};
|
};
|
||||||
si += 1;
|
si += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Assign vna_other indices starting at 2, walking vlib then per-version.
|
// Assign vna_other indices starting at 2, walking vlib then per-version.
|
||||||
let next_vna: u16 = 2u16;
|
let nextvna: u16 = 2u16;
|
||||||
let vi: i32 = 0;
|
let vi: i32 = 0;
|
||||||
for (vi < n_vlibs) {
|
for (vi < nvlibs) {
|
||||||
let first: i32 = drdi32(vlib_first_buf, (vi: u64) * 4u64);
|
let first: i32 = drdi32(vlibfirstbuf, (vi: u64) * 4u64);
|
||||||
let cnt: i32 = drdi32(vlib_count_buf, (vi: u64) * 4u64);
|
let cnt: i32 = drdi32(vlibcountbuf, (vi: u64) * 4u64);
|
||||||
let k: i32 = 0;
|
let k: i32 = 0;
|
||||||
for (k < cnt) {
|
for (k < cnt) {
|
||||||
dwr16(ver_vna_other, ((first + k): u64) * 2u64, next_vna);
|
dwr16(vervnaother, ((first + k): u64) * 2u64, nextvna);
|
||||||
next_vna += 1u16;
|
nextvna += 1u16;
|
||||||
k += 1;
|
k += 1;
|
||||||
};
|
};
|
||||||
vi += 1;
|
vi += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---- compute dynstr size ----
|
// ---- compute dynstr size ----
|
||||||
let dynstr_sz: u64 = 1u64; // leading NUL
|
let dynstrsz: u64 = 1u64; // leading NUL
|
||||||
let pi: i32 = 0;
|
let pi: i32 = 0;
|
||||||
for (pi < nsos) {
|
for (pi < nsos) {
|
||||||
let so4: *lso = sos_used[pi];
|
let so4: *lso = sosused[pi];
|
||||||
dynstr_sz += so4.soname.len: u64;
|
dynstrsz += so4.soname.len: u64;
|
||||||
dynstr_sz += 1u64;
|
dynstrsz += 1u64;
|
||||||
pi += 1;
|
pi += 1;
|
||||||
};
|
};
|
||||||
pi = 0;
|
pi = 0;
|
||||||
for (pi < n) {
|
for (pi < n) {
|
||||||
let dsm4: *lsym = dynsyms[pi];
|
let dsm4: *lsym = dynsyms[pi];
|
||||||
dynstr_sz += dsm4.name.len: u64;
|
dynstrsz += dsm4.name.len: u64;
|
||||||
dynstr_sz += 1u64;
|
dynstrsz += 1u64;
|
||||||
pi += 1;
|
pi += 1;
|
||||||
};
|
};
|
||||||
pi = 0;
|
pi = 0;
|
||||||
for (pi < n_vers) {
|
for (pi < nvers) {
|
||||||
let nm_len: i32 = drdi32(ver_name_len_buf, (pi: u64) * 4u64);
|
let nmlen: i32 = drdi32(vernamelenbuf, (pi: u64) * 4u64);
|
||||||
dynstr_sz += nm_len: u64;
|
dynstrsz += nmlen: u64;
|
||||||
dynstr_sz += 1u64;
|
dynstrsz += 1u64;
|
||||||
pi += 1;
|
pi += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---- fill dynstr ----
|
// ---- fill dynstr ----
|
||||||
let dynstr: *u8 = amalloc(a, dynstr_sz): *u8;
|
let dynstr: *u8 = amalloc(a, dynstrsz): *u8;
|
||||||
let dynstr_pos: u64 = 1u64; // past leading NUL
|
let dynstrpos: u64 = 1u64; // past leading NUL
|
||||||
|
|
||||||
let soname_str: *u8 = amalloc(a, (nsos: u64) * 4u64): *u8;
|
let sonamestr: *u8 = amalloc(a, (nsos: u64) * 4u64): *u8;
|
||||||
pi = 0;
|
pi = 0;
|
||||||
for (pi < nsos) {
|
for (pi < nsos) {
|
||||||
dwr32(soname_str, (pi: u64) * 4u64, dynstr_pos: u32);
|
dwr32(sonamestr, (pi: u64) * 4u64, dynstrpos: u32);
|
||||||
let so2: *lso = sos_used[pi];
|
let so2: *lso = sosused[pi];
|
||||||
let snm: str = so2.soname;
|
let snm: str = so2.soname;
|
||||||
dbcopy(dynstr, dynstr_pos, snm.ptr, snm.len: u64);
|
dbcopy(dynstr, dynstrpos, snm.ptr, snm.len: u64);
|
||||||
dynstr_pos += snm.len: u64;
|
dynstrpos += snm.len: u64;
|
||||||
dynstr[dynstr_pos] = 0u8;
|
dynstr[dynstrpos] = 0u8;
|
||||||
dynstr_pos += 1u64;
|
dynstrpos += 1u64;
|
||||||
pi += 1;
|
pi += 1;
|
||||||
};
|
};
|
||||||
let symname_str: *u8 = amalloc(a, nu * 4u64): *u8;
|
let symnamestr: *u8 = amalloc(a, nu * 4u64): *u8;
|
||||||
pi = 0;
|
pi = 0;
|
||||||
for (pi < n) {
|
for (pi < n) {
|
||||||
dwr32(symname_str, (pi: u64) * 4u64, dynstr_pos: u32);
|
dwr32(symnamestr, (pi: u64) * 4u64, dynstrpos: u32);
|
||||||
let dsm: *lsym = dynsyms[pi];
|
let dsm: *lsym = dynsyms[pi];
|
||||||
let snm: str = dsm.name;
|
let snm: str = dsm.name;
|
||||||
dbcopy(dynstr, dynstr_pos, snm.ptr, snm.len: u64);
|
dbcopy(dynstr, dynstrpos, snm.ptr, snm.len: u64);
|
||||||
dynstr_pos += snm.len: u64;
|
dynstrpos += snm.len: u64;
|
||||||
dynstr[dynstr_pos] = 0u8;
|
dynstr[dynstrpos] = 0u8;
|
||||||
dynstr_pos += 1u64;
|
dynstrpos += 1u64;
|
||||||
pi += 1;
|
pi += 1;
|
||||||
};
|
};
|
||||||
pi = 0;
|
pi = 0;
|
||||||
for (pi < n_vers) {
|
for (pi < nvers) {
|
||||||
dwr32(ver_dynstr_off, (pi: u64) * 4u64, dynstr_pos: u32);
|
dwr32(verdynstroff, (pi: u64) * 4u64, dynstrpos: u32);
|
||||||
let nm_p: *u8 = ver_name_ptr_arr[pi];
|
let nmp: *u8 = vernameptrarr[pi];
|
||||||
let nm_len: i32 = drdi32(ver_name_len_buf, (pi: u64) * 4u64);
|
let nmlen: i32 = drdi32(vernamelenbuf, (pi: u64) * 4u64);
|
||||||
dbcopy(dynstr, dynstr_pos, nm_p, nm_len: u64);
|
dbcopy(dynstr, dynstrpos, nmp, nmlen: u64);
|
||||||
dynstr_pos += nm_len: u64;
|
dynstrpos += nmlen: u64;
|
||||||
dynstr[dynstr_pos] = 0u8;
|
dynstr[dynstrpos] = 0u8;
|
||||||
dynstr_pos += 1u64;
|
dynstrpos += 1u64;
|
||||||
pi += 1;
|
pi += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---- per-dyn-sym versym index ----
|
// ---- per-dyn-sym versym index ----
|
||||||
let versym_for: *u8 = amalloc(a, nu * 2u64): *u8;
|
let versymfor: *u8 = amalloc(a, nu * 2u64): *u8;
|
||||||
pi = 0;
|
pi = 0;
|
||||||
for (pi < n) {
|
for (pi < n) {
|
||||||
let dsm3: *lsym = dynsyms[pi];
|
let dsm3: *lsym = dynsyms[pi];
|
||||||
@@ -391,235 +391,235 @@ export fn emitdynelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
|
|||||||
let nm3: str = dsm3.name;
|
let nm3: str = dsm3.name;
|
||||||
let vname: str = soversion(dl3, nm3);
|
let vname: str = soversion(dl3, nm3);
|
||||||
if (vname.len == 0) {
|
if (vname.len == 0) {
|
||||||
dwr16(versym_for, (pi: u64) * 2u64, VER_NDX_GLOBAL_D);
|
dwr16(versymfor, (pi: u64) * 2u64, VER_NDX_GLOBAL_D);
|
||||||
} else {
|
} else {
|
||||||
let matched: i32 = 0;
|
let matched: i32 = 0;
|
||||||
let vk: i32 = 0;
|
let vk: i32 = 0;
|
||||||
for (vk < n_vers) {
|
for (vk < nvers) {
|
||||||
let vlx: i32 = drdi32(ver_lib_idx_buf, (vk: u64) * 4u64);
|
let vlx: i32 = drdi32(verlibidxbuf, (vk: u64) * 4u64);
|
||||||
let sosx: i32 = drdi32(vlib_sos_idx_buf, (vlx: u64) * 4u64);
|
let sosx: i32 = drdi32(vlibsosidxbuf, (vlx: u64) * 4u64);
|
||||||
if (sos_used[sosx] == dl3) {
|
if (sosused[sosx] == dl3) {
|
||||||
let exi: str;
|
let exi: str;
|
||||||
exi.ptr = ver_name_ptr_arr[vk];
|
exi.ptr = vernameptrarr[vk];
|
||||||
exi.len = drdi32(ver_name_len_buf, (vk: u64) * 4u64);
|
exi.len = drdi32(vernamelenbuf, (vk: u64) * 4u64);
|
||||||
if (streqd(exi, vname)) {
|
if (streqd(exi, vname)) {
|
||||||
let other: u16 = drdu16(ver_vna_other, (vk: u64) * 2u64);
|
let other: u16 = drdu16(vervnaother, (vk: u64) * 2u64);
|
||||||
dwr16(versym_for, (pi: u64) * 2u64, other);
|
dwr16(versymfor, (pi: u64) * 2u64, other);
|
||||||
matched = 1;
|
matched = 1;
|
||||||
vk = n_vers;
|
vk = nvers;
|
||||||
} else { vk += 1; };
|
} else { vk += 1; };
|
||||||
} else { vk += 1; };
|
} else { vk += 1; };
|
||||||
};
|
};
|
||||||
if (matched == 0) {
|
if (matched == 0) {
|
||||||
dwr16(versym_for, (pi: u64) * 2u64, VER_NDX_GLOBAL_D);
|
dwr16(versymfor, (pi: u64) * 2u64, VER_NDX_GLOBAL_D);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
pi += 1;
|
pi += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---- compute byte sizes ----
|
// ---- compute byte sizes ----
|
||||||
let ehdr_sz: u64 = 64u64;
|
let ehdrsz: u64 = 64u64;
|
||||||
let n_phdrs: u64 = 4u64;
|
let nphdrs: u64 = 4u64;
|
||||||
let phdr_sz: u64 = n_phdrs * 56u64;
|
let phdrsz: u64 = nphdrs * 56u64;
|
||||||
let is_local: str = interpstrv();
|
let islocal: str = interpstrv();
|
||||||
let interp_sz: u64 = (is_local.len: u64) + 1u64;
|
let interpsz: u64 = (islocal.len: u64) + 1u64;
|
||||||
|
|
||||||
let nsyms_total: u64 = 1u64 + nu;
|
let nsymstotal: u64 = 1u64 + nu;
|
||||||
let dynsym_sz: u64 = nsyms_total * 24u64;
|
let dynsymsz: u64 = nsymstotal * 24u64;
|
||||||
|
|
||||||
let nbuckets: u32 = 1u32;
|
let nbuckets: u32 = 1u32;
|
||||||
let nchain: u32 = nsyms_total: u32;
|
let nchain: u32 = nsymstotal: u32;
|
||||||
let hash_sz: u64 = (2u64 + (nbuckets: u64) + (nchain: u64)) * 4u64;
|
let hashsz: u64 = (2u64 + (nbuckets: u64) + (nchain: u64)) * 4u64;
|
||||||
|
|
||||||
let relaplt_sz: u64 = nu * 24u64;
|
let relapltsz: u64 = nu * 24u64;
|
||||||
let plt_sz: u64 = nu * PLT_STUB_BYTES_D;
|
let pltsz: u64 = nu * PLT_STUB_BYTES_D;
|
||||||
let gotplt_sz: u64 = (3u64 + nu) * 8u64;
|
let gotpltsz: u64 = (3u64 + nu) * 8u64;
|
||||||
let versym_sz: u64 = nsyms_total * 2u64;
|
let versymsz: u64 = nsymstotal * 2u64;
|
||||||
|
|
||||||
let verneed_sz: u64 = 0u64;
|
let verneedsz: u64 = 0u64;
|
||||||
let vli: i32 = 0;
|
let vli: i32 = 0;
|
||||||
for (vli < n_vlibs) {
|
for (vli < nvlibs) {
|
||||||
let cnt: i32 = drdi32(vlib_count_buf, (vli: u64) * 4u64);
|
let cnt: i32 = drdi32(vlibcountbuf, (vli: u64) * 4u64);
|
||||||
verneed_sz += 16u64 + 16u64 * (cnt: u64);
|
verneedsz += 16u64 + 16u64 * (cnt: u64);
|
||||||
vli += 1;
|
vli += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
let with_ver: i32 = 0;
|
let withver: i32 = 0;
|
||||||
if (n_vlibs > 0) { with_ver = 1; };
|
if (nvlibs > 0) { withver = 1; };
|
||||||
let extra: u64 = 0u64;
|
let extra: u64 = 0u64;
|
||||||
if (with_ver != 0) { extra = 3u64; };
|
if (withver != 0) { extra = 3u64; };
|
||||||
let ndyn: u64 = (nsos: u64) + 11u64 + extra;
|
let ndyn: u64 = (nsos: u64) + 11u64 + extra;
|
||||||
let dynamic_sz: u64 = ndyn * 16u64;
|
let dynamicsz: u64 = ndyn * 16u64;
|
||||||
|
|
||||||
// ---- compute file offsets ----
|
// ---- compute file offsets ----
|
||||||
let off: u64 = ehdr_sz + phdr_sz;
|
let off: u64 = ehdrsz + phdrsz;
|
||||||
let interp_off: u64 = off; off += interp_sz;
|
let interpoff: u64 = off; off += interpsz;
|
||||||
off = alignup(off, 8u64);
|
off = alignup(off, 8u64);
|
||||||
let dynstr_off: u64 = off; off += dynstr_sz;
|
let dynstroff: u64 = off; off += dynstrsz;
|
||||||
off = alignup(off, 8u64);
|
off = alignup(off, 8u64);
|
||||||
let dynsym_off: u64 = off; off += dynsym_sz;
|
let dynsymoff: u64 = off; off += dynsymsz;
|
||||||
let hash_off: u64 = off; off += hash_sz;
|
let hashoff: u64 = off; off += hashsz;
|
||||||
off = alignup(off, 2u64);
|
off = alignup(off, 2u64);
|
||||||
let versym_off: u64 = off; off += versym_sz;
|
let versymoff: u64 = off; off += versymsz;
|
||||||
off = alignup(off, 4u64);
|
off = alignup(off, 4u64);
|
||||||
let verneed_off: u64 = off; off += verneed_sz;
|
let verneedoff: u64 = off; off += verneedsz;
|
||||||
off = alignup(off, 8u64);
|
off = alignup(off, 8u64);
|
||||||
let relaplt_off: u64 = off; off += relaplt_sz;
|
let relapltoff: u64 = off; off += relapltsz;
|
||||||
|
|
||||||
let text_off: u64 = alignup(off, PAGE);
|
let textoff: u64 = alignup(off, PAGE);
|
||||||
let plt_off: u64 = text_off + l.textlen;
|
let pltoff: u64 = textoff + l.textlen;
|
||||||
let rx_end: u64 = plt_off + plt_sz;
|
let rxend: u64 = pltoff + pltsz;
|
||||||
|
|
||||||
let gotplt_off: u64 = alignup(rx_end, PAGE);
|
let gotpltoff: u64 = alignup(rxend, PAGE);
|
||||||
let dynamic_off: u64 = gotplt_off + gotplt_sz;
|
let dynamicoff: u64 = gotpltoff + gotpltsz;
|
||||||
let file_end: u64 = dynamic_off + dynamic_sz;
|
let fileend: u64 = dynamicoff + dynamicsz;
|
||||||
|
|
||||||
let interp_va: u64 = base + interp_off;
|
let interpva: u64 = base + interpoff;
|
||||||
let dynstr_va: u64 = base + dynstr_off;
|
let dynstrva: u64 = base + dynstroff;
|
||||||
let dynsym_va: u64 = base + dynsym_off;
|
let dynsymva: u64 = base + dynsymoff;
|
||||||
let hash_va: u64 = base + hash_off;
|
let hashva: u64 = base + hashoff;
|
||||||
let versym_va: u64 = base + versym_off;
|
let versymva: u64 = base + versymoff;
|
||||||
let verneed_va: u64 = base + verneed_off;
|
let verneedva: u64 = base + verneedoff;
|
||||||
let relaplt_va: u64 = base + relaplt_off;
|
let relapltva: u64 = base + relapltoff;
|
||||||
let text_va: u64 = base + text_off;
|
let textva: u64 = base + textoff;
|
||||||
let plt_va: u64 = base + plt_off;
|
let pltva: u64 = base + pltoff;
|
||||||
let gotplt_va: u64 = base + gotplt_off;
|
let gotpltva: u64 = base + gotpltoff;
|
||||||
let dynamic_va: u64 = base + dynamic_off;
|
let dynamicva: u64 = base + dynamicoff;
|
||||||
|
|
||||||
// ---- build .dynsym ----
|
// ---- build .dynsym ----
|
||||||
let dynsym_buf: *u8 = amalloc(a, dynsym_sz): *u8;
|
let dynsymbuf: *u8 = amalloc(a, dynsymsz): *u8;
|
||||||
pi = 0;
|
pi = 0;
|
||||||
for (pi < n) {
|
for (pi < n) {
|
||||||
let eoff: u64 = (1u64 + (pi: u64)) * 24u64;
|
let eoff: u64 = (1u64 + (pi: u64)) * 24u64;
|
||||||
dwr32(dynsym_buf, eoff + 0u64, drdu32(symname_str, (pi: u64) * 4u64));
|
dwr32(dynsymbuf, eoff + 0u64, drdu32(symnamestr, (pi: u64) * 4u64));
|
||||||
dwr8(dynsym_buf, eoff + 4u64, (STB_GLOBAL_D << 4u8) | (STT_FUNC_D & 15u8));
|
dwr8(dynsymbuf, eoff + 4u64, (STB_GLOBAL_D << 4u8) | (STT_FUNC_D & 15u8));
|
||||||
dwr8(dynsym_buf, eoff + 5u64, 0u8);
|
dwr8(dynsymbuf, eoff + 5u64, 0u8);
|
||||||
dwr16(dynsym_buf, eoff + 6u64, 0u16);
|
dwr16(dynsymbuf, eoff + 6u64, 0u16);
|
||||||
dwr64(dynsym_buf, eoff + 8u64, 0u64);
|
dwr64(dynsymbuf, eoff + 8u64, 0u64);
|
||||||
dwr64(dynsym_buf, eoff + 16u64, 0u64);
|
dwr64(dynsymbuf, eoff + 16u64, 0u64);
|
||||||
pi += 1;
|
pi += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---- build .hash (SysV, 1 bucket) ----
|
// ---- build .hash (SysV, 1 bucket) ----
|
||||||
let hash_buf: *u8 = amalloc(a, hash_sz): *u8;
|
let hashbuf: *u8 = amalloc(a, hashsz): *u8;
|
||||||
dwr32(hash_buf, 0u64, nbuckets);
|
dwr32(hashbuf, 0u64, nbuckets);
|
||||||
dwr32(hash_buf, 4u64, nchain);
|
dwr32(hashbuf, 4u64, nchain);
|
||||||
let bucket0: u32 = 0u32;
|
let bucket0: u32 = 0u32;
|
||||||
if (nsyms_total > 1u64) { bucket0 = 1u32; };
|
if (nsymstotal > 1u64) { bucket0 = 1u32; };
|
||||||
dwr32(hash_buf, 8u64, bucket0);
|
dwr32(hashbuf, 8u64, bucket0);
|
||||||
let ci: u64 = 1u64;
|
let ci: u64 = 1u64;
|
||||||
for (ci < nsyms_total) {
|
for (ci < nsymstotal) {
|
||||||
let nxt: u32 = 0u32;
|
let nxt: u32 = 0u32;
|
||||||
if (ci + 1u64 < nsyms_total) { nxt = (ci + 1u64): u32; };
|
if (ci + 1u64 < nsymstotal) { nxt = (ci + 1u64): u32; };
|
||||||
dwr32(hash_buf, 8u64 + (nbuckets: u64) * 4u64 + ci * 4u64, nxt);
|
dwr32(hashbuf, 8u64 + (nbuckets: u64) * 4u64 + ci * 4u64, nxt);
|
||||||
ci += 1u64;
|
ci += 1u64;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---- build .rela.plt ----
|
// ---- build .rela.plt ----
|
||||||
let relaplt_buf: *u8 = amalloc(a, relaplt_sz): *u8;
|
let relapltbuf: *u8 = amalloc(a, relapltsz): *u8;
|
||||||
pi = 0;
|
pi = 0;
|
||||||
for (pi < n) {
|
for (pi < n) {
|
||||||
let roff: u64 = (pi: u64) * 24u64;
|
let roff: u64 = (pi: u64) * 24u64;
|
||||||
dwr64(relaplt_buf, roff + 0u64, gotplt_va + (3u64 + (pi: u64)) * 8u64);
|
dwr64(relapltbuf, roff + 0u64, gotpltva + (3u64 + (pi: u64)) * 8u64);
|
||||||
let info: u64 = ((1u64 + (pi: u64)) << 32u64) | (R_X86_64_JUMP_SLOT_D: u64);
|
let info: u64 = ((1u64 + (pi: u64)) << 32u64) | (R_X86_64_JUMP_SLOT_D: u64);
|
||||||
dwr64(relaplt_buf, roff + 8u64, info);
|
dwr64(relapltbuf, roff + 8u64, info);
|
||||||
dwri64(relaplt_buf, roff + 16u64, 0i64);
|
dwri64(relapltbuf, roff + 16u64, 0i64);
|
||||||
pi += 1;
|
pi += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---- build .gnu.version (u16 per dynsym entry) ----
|
// ---- build .gnu.version (u16 per dynsym entry) ----
|
||||||
let versym_buf: *u8 = amalloc(a, versym_sz): *u8;
|
let versymbuf: *u8 = amalloc(a, versymsz): *u8;
|
||||||
dwr16(versym_buf, 0u64, VER_NDX_LOCAL_D);
|
dwr16(versymbuf, 0u64, VER_NDX_LOCAL_D);
|
||||||
pi = 0;
|
pi = 0;
|
||||||
for (pi < n) {
|
for (pi < n) {
|
||||||
dwr16(versym_buf, 2u64 + (pi: u64) * 2u64, drdu16(versym_for, (pi: u64) * 2u64));
|
dwr16(versymbuf, 2u64 + (pi: u64) * 2u64, drdu16(versymfor, (pi: u64) * 2u64));
|
||||||
pi += 1;
|
pi += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---- build .gnu.version_r ----
|
// ---- build .gnu.version_r ----
|
||||||
let verneed_buf: *u8 = amalloc(a, verneed_sz): *u8;
|
let verneedbuf: *u8 = amalloc(a, verneedsz): *u8;
|
||||||
if (verneed_sz > 0u64) {
|
if (verneedsz > 0u64) {
|
||||||
let vnoff: u64 = 0u64;
|
let vnoff: u64 = 0u64;
|
||||||
vli = 0;
|
vli = 0;
|
||||||
for (vli < n_vlibs) {
|
for (vli < nvlibs) {
|
||||||
let sos_idx: i32 = drdi32(vlib_sos_idx_buf, (vli: u64) * 4u64);
|
let sosidx: i32 = drdi32(vlibsosidxbuf, (vli: u64) * 4u64);
|
||||||
let first: i32 = drdi32(vlib_first_buf, (vli: u64) * 4u64);
|
let first: i32 = drdi32(vlibfirstbuf, (vli: u64) * 4u64);
|
||||||
let cnt: i32 = drdi32(vlib_count_buf, (vli: u64) * 4u64);
|
let cnt: i32 = drdi32(vlibcountbuf, (vli: u64) * 4u64);
|
||||||
let vn_start: u64 = vnoff;
|
let vnstart: u64 = vnoff;
|
||||||
dwr16(verneed_buf, vnoff + 0u64, 1u16);
|
dwr16(verneedbuf, vnoff + 0u64, 1u16);
|
||||||
dwr16(verneed_buf, vnoff + 2u64, cnt: u16);
|
dwr16(verneedbuf, vnoff + 2u64, cnt: u16);
|
||||||
dwr32(verneed_buf, vnoff + 4u64, drdu32(soname_str, (sos_idx: u64) * 4u64));
|
dwr32(verneedbuf, vnoff + 4u64, drdu32(sonamestr, (sosidx: u64) * 4u64));
|
||||||
dwr32(verneed_buf, vnoff + 8u64, 16u32);
|
dwr32(verneedbuf, vnoff + 8u64, 16u32);
|
||||||
vnoff += 16u64;
|
vnoff += 16u64;
|
||||||
let k: i32 = 0;
|
let k: i32 = 0;
|
||||||
for (k < cnt) {
|
for (k < cnt) {
|
||||||
let vk: i32 = first + k;
|
let vk: i32 = first + k;
|
||||||
let nm: str;
|
let nm: str;
|
||||||
nm.ptr = ver_name_ptr_arr[vk];
|
nm.ptr = vernameptrarr[vk];
|
||||||
nm.len = drdi32(ver_name_len_buf, (vk: u64) * 4u64);
|
nm.len = drdi32(vernamelenbuf, (vk: u64) * 4u64);
|
||||||
let h: u32 = elfhash(nm);
|
let h: u32 = elfhash(nm);
|
||||||
dwr32(verneed_buf, vnoff + 0u64, h);
|
dwr32(verneedbuf, vnoff + 0u64, h);
|
||||||
dwr16(verneed_buf, vnoff + 4u64, 0u16);
|
dwr16(verneedbuf, vnoff + 4u64, 0u16);
|
||||||
dwr16(verneed_buf, vnoff + 6u64, drdu16(ver_vna_other, (vk: u64) * 2u64));
|
dwr16(verneedbuf, vnoff + 6u64, drdu16(vervnaother, (vk: u64) * 2u64));
|
||||||
dwr32(verneed_buf, vnoff + 8u64, drdu32(ver_dynstr_off, (vk: u64) * 4u64));
|
dwr32(verneedbuf, vnoff + 8u64, drdu32(verdynstroff, (vk: u64) * 4u64));
|
||||||
let nxt: u32 = 0u32;
|
let nxt: u32 = 0u32;
|
||||||
if (k + 1 < cnt) { nxt = 16u32; };
|
if (k + 1 < cnt) { nxt = 16u32; };
|
||||||
dwr32(verneed_buf, vnoff + 12u64, nxt);
|
dwr32(verneedbuf, vnoff + 12u64, nxt);
|
||||||
vnoff += 16u64;
|
vnoff += 16u64;
|
||||||
k += 1;
|
k += 1;
|
||||||
};
|
};
|
||||||
let vn_nxt: u32 = 0u32;
|
let vnnxt: u32 = 0u32;
|
||||||
if (vli + 1 < n_vlibs) { vn_nxt = (vnoff - vn_start): u32; };
|
if (vli + 1 < nvlibs) { vnnxt = (vnoff - vnstart): u32; };
|
||||||
dwr32(verneed_buf, vn_start + 12u64, vn_nxt);
|
dwr32(verneedbuf, vnstart + 12u64, vnnxt);
|
||||||
vli += 1;
|
vli += 1;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---- build .plt ----
|
// ---- build .plt ----
|
||||||
let plt_buf: *u8 = amalloc(a, plt_sz): *u8;
|
let pltbuf: *u8 = amalloc(a, pltsz): *u8;
|
||||||
pi = 0;
|
pi = 0;
|
||||||
for (pi < n) {
|
for (pi < n) {
|
||||||
let p_off: u64 = (pi: u64) * PLT_STUB_BYTES_D;
|
let poff: u64 = (pi: u64) * PLT_STUB_BYTES_D;
|
||||||
let stub_va: u64 = plt_va + p_off;
|
let stubva: u64 = pltva + poff;
|
||||||
let next_ip: u64 = stub_va + 6u64;
|
let nextip: u64 = stubva + 6u64;
|
||||||
let slot_va: u64 = gotplt_va + (3u64 + (pi: u64)) * 8u64;
|
let slotva: u64 = gotpltva + (3u64 + (pi: u64)) * 8u64;
|
||||||
let disp: i64 = (slot_va: i64) - (next_ip: i64);
|
let disp: i64 = (slotva: i64) - (nextip: i64);
|
||||||
dwr8(plt_buf, p_off + 0u64, 255u8);
|
dwr8(pltbuf, poff + 0u64, 255u8);
|
||||||
dwr8(plt_buf, p_off + 1u64, 37u8);
|
dwr8(pltbuf, poff + 1u64, 37u8);
|
||||||
dwr32(plt_buf, p_off + 2u64, (disp: i32): u32);
|
dwr32(pltbuf, poff + 2u64, (disp: i32): u32);
|
||||||
pi += 1;
|
pi += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---- build .got.plt ----
|
// ---- build .got.plt ----
|
||||||
let gotplt_buf: *u8 = amalloc(a, gotplt_sz): *u8;
|
let gotpltbuf: *u8 = amalloc(a, gotpltsz): *u8;
|
||||||
dwr64(gotplt_buf, 0u64, dynamic_va);
|
dwr64(gotpltbuf, 0u64, dynamicva);
|
||||||
|
|
||||||
// ---- build .dynamic ----
|
// ---- build .dynamic ----
|
||||||
let dynamic_buf: *u8 = amalloc(a, dynamic_sz): *u8;
|
let dynamicbuf: *u8 = amalloc(a, dynamicsz): *u8;
|
||||||
let dk: u64 = 0u64;
|
let dk: u64 = 0u64;
|
||||||
pi = 0;
|
pi = 0;
|
||||||
for (pi < nsos) {
|
for (pi < nsos) {
|
||||||
dwri64(dynamic_buf, dk * 16u64 + 0u64, DT_NEEDED);
|
dwri64(dynamicbuf, dk * 16u64 + 0u64, DT_NEEDED);
|
||||||
dwr64(dynamic_buf, dk * 16u64 + 8u64, drdu32(soname_str, (pi: u64) * 4u64): u64);
|
dwr64(dynamicbuf, dk * 16u64 + 8u64, drdu32(sonamestr, (pi: u64) * 4u64): u64);
|
||||||
dk += 1u64;
|
dk += 1u64;
|
||||||
pi += 1;
|
pi += 1;
|
||||||
};
|
};
|
||||||
dwri64(dynamic_buf, dk * 16u64, DT_HASH); dwr64(dynamic_buf, dk * 16u64 + 8u64, hash_va); dk += 1u64;
|
dwri64(dynamicbuf, dk * 16u64, DT_HASH); dwr64(dynamicbuf, dk * 16u64 + 8u64, hashva); dk += 1u64;
|
||||||
dwri64(dynamic_buf, dk * 16u64, DT_STRTAB); dwr64(dynamic_buf, dk * 16u64 + 8u64, dynstr_va); dk += 1u64;
|
dwri64(dynamicbuf, dk * 16u64, DT_STRTAB); dwr64(dynamicbuf, dk * 16u64 + 8u64, dynstrva); dk += 1u64;
|
||||||
dwri64(dynamic_buf, dk * 16u64, DT_SYMTAB); dwr64(dynamic_buf, dk * 16u64 + 8u64, dynsym_va); dk += 1u64;
|
dwri64(dynamicbuf, dk * 16u64, DT_SYMTAB); dwr64(dynamicbuf, dk * 16u64 + 8u64, dynsymva); dk += 1u64;
|
||||||
dwri64(dynamic_buf, dk * 16u64, DT_STRSZ); dwr64(dynamic_buf, dk * 16u64 + 8u64, dynstr_sz); dk += 1u64;
|
dwri64(dynamicbuf, dk * 16u64, DT_STRSZ); dwr64(dynamicbuf, dk * 16u64 + 8u64, dynstrsz); dk += 1u64;
|
||||||
dwri64(dynamic_buf, dk * 16u64, DT_SYMENT); dwr64(dynamic_buf, dk * 16u64 + 8u64, 24u64); dk += 1u64;
|
dwri64(dynamicbuf, dk * 16u64, DT_SYMENT); dwr64(dynamicbuf, dk * 16u64 + 8u64, 24u64); dk += 1u64;
|
||||||
dwri64(dynamic_buf, dk * 16u64, DT_PLTGOT); dwr64(dynamic_buf, dk * 16u64 + 8u64, gotplt_va); dk += 1u64;
|
dwri64(dynamicbuf, dk * 16u64, DT_PLTGOT); dwr64(dynamicbuf, dk * 16u64 + 8u64, gotpltva); dk += 1u64;
|
||||||
dwri64(dynamic_buf, dk * 16u64, DT_PLTRELSZ); dwr64(dynamic_buf, dk * 16u64 + 8u64, relaplt_sz); dk += 1u64;
|
dwri64(dynamicbuf, dk * 16u64, DT_PLTRELSZ); dwr64(dynamicbuf, dk * 16u64 + 8u64, relapltsz); dk += 1u64;
|
||||||
dwri64(dynamic_buf, dk * 16u64, DT_PLTREL); dwr64(dynamic_buf, dk * 16u64 + 8u64, DT_RELA: u64);dk += 1u64;
|
dwri64(dynamicbuf, dk * 16u64, DT_PLTREL); dwr64(dynamicbuf, dk * 16u64 + 8u64, DT_RELA: u64);dk += 1u64;
|
||||||
dwri64(dynamic_buf, dk * 16u64, DT_JMPREL); dwr64(dynamic_buf, dk * 16u64 + 8u64, relaplt_va); dk += 1u64;
|
dwri64(dynamicbuf, dk * 16u64, DT_JMPREL); dwr64(dynamicbuf, dk * 16u64 + 8u64, relapltva); dk += 1u64;
|
||||||
dwri64(dynamic_buf, dk * 16u64, DT_BIND_NOW); dwr64(dynamic_buf, dk * 16u64 + 8u64, 0u64); dk += 1u64;
|
dwri64(dynamicbuf, dk * 16u64, DT_BIND_NOW); dwr64(dynamicbuf, dk * 16u64 + 8u64, 0u64); dk += 1u64;
|
||||||
if (with_ver != 0) {
|
if (withver != 0) {
|
||||||
dwri64(dynamic_buf, dk * 16u64, DT_VERSYM); dwr64(dynamic_buf, dk * 16u64 + 8u64, versym_va); dk += 1u64;
|
dwri64(dynamicbuf, dk * 16u64, DT_VERSYM); dwr64(dynamicbuf, dk * 16u64 + 8u64, versymva); dk += 1u64;
|
||||||
dwri64(dynamic_buf, dk * 16u64, DT_VERNEED); dwr64(dynamic_buf, dk * 16u64 + 8u64, verneed_va); dk += 1u64;
|
dwri64(dynamicbuf, dk * 16u64, DT_VERNEED); dwr64(dynamicbuf, dk * 16u64 + 8u64, verneedva); dk += 1u64;
|
||||||
dwri64(dynamic_buf, dk * 16u64, DT_VERNEEDNUM); dwr64(dynamic_buf, dk * 16u64 + 8u64, n_vlibs: u64);dk += 1u64;
|
dwri64(dynamicbuf, dk * 16u64, DT_VERNEEDNUM); dwr64(dynamicbuf, dk * 16u64 + 8u64, nvlibs: u64);dk += 1u64;
|
||||||
};
|
};
|
||||||
dwri64(dynamic_buf, dk * 16u64, DT_NULL); dwr64(dynamic_buf, dk * 16u64 + 8u64, 0u64); dk += 1u64;
|
dwri64(dynamicbuf, dk * 16u64, DT_NULL); dwr64(dynamicbuf, dk * 16u64 + 8u64, 0u64); dk += 1u64;
|
||||||
if (dk != ndyn) {
|
if (dk != ndyn) {
|
||||||
os.write(2, "w6l: dynamic entry count mismatch\n".ptr, 33u64);
|
os.write(2, "w6l: dynamic entry count mismatch\n".ptr, 33u64);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -637,8 +637,8 @@ export fn emitdynelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
|
|||||||
return 1;
|
return 1;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
let site: u64 = text_va + r.off;
|
let site: u64 = textva + r.off;
|
||||||
let stub: u64 = plt_va + (rsym.pltidx: u64) * PLT_STUB_BYTES_D;
|
let stub: u64 = pltva + (rsym.pltidx: u64) * PLT_STUB_BYTES_D;
|
||||||
let disp: i64 = (stub: i64) - (site: i64) + r.addend;
|
let disp: i64 = (stub: i64) - (site: i64) + r.addend;
|
||||||
dwr32(l.text, r.off, (disp: i32): u32);
|
dwr32(l.text, r.off, (disp: i32): u32);
|
||||||
};
|
};
|
||||||
@@ -647,96 +647,96 @@ export fn emitdynelf(l: *lnk, fd: i32, base: u64, entry: u64) i32 = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// ---- assemble file buffer ----
|
// ---- assemble file buffer ----
|
||||||
let file_buf: *u8 = os.alloc(file_end): *u8;
|
let filebuf: *u8 = os.alloc(fileend): *u8;
|
||||||
if (file_buf == nil) {
|
if (filebuf == nil) {
|
||||||
os.write(2, "w6l: out of memory\n".ptr, 18u64);
|
os.write(2, "w6l: out of memory\n".ptr, 18u64);
|
||||||
return 1;
|
return 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Ehdr
|
// Ehdr
|
||||||
dwr8(file_buf, 0u64, 127u8);
|
dwr8(filebuf, 0u64, 127u8);
|
||||||
dwr8(file_buf, 1u64, 69u8);
|
dwr8(filebuf, 1u64, 69u8);
|
||||||
dwr8(file_buf, 2u64, 76u8);
|
dwr8(filebuf, 2u64, 76u8);
|
||||||
dwr8(file_buf, 3u64, 70u8);
|
dwr8(filebuf, 3u64, 70u8);
|
||||||
dwr8(file_buf, 4u64, ELFCLASS64_D);
|
dwr8(filebuf, 4u64, ELFCLASS64_D);
|
||||||
dwr8(file_buf, 5u64, ELFDATA2LSB_D);
|
dwr8(filebuf, 5u64, ELFDATA2LSB_D);
|
||||||
dwr8(file_buf, 6u64, EV_CURRENT_D: u8);
|
dwr8(filebuf, 6u64, EV_CURRENT_D: u8);
|
||||||
dwr16(file_buf, 16u64, ET_EXEC_D);
|
dwr16(filebuf, 16u64, ET_EXEC_D);
|
||||||
dwr16(file_buf, 18u64, EM_X86_64_D);
|
dwr16(filebuf, 18u64, EM_X86_64_D);
|
||||||
dwr32(file_buf, 20u64, EV_CURRENT_D);
|
dwr32(filebuf, 20u64, EV_CURRENT_D);
|
||||||
dwr64(file_buf, 24u64, entry);
|
dwr64(filebuf, 24u64, entry);
|
||||||
dwr64(file_buf, 32u64, ehdr_sz);
|
dwr64(filebuf, 32u64, ehdrsz);
|
||||||
dwr64(file_buf, 40u64, 0u64);
|
dwr64(filebuf, 40u64, 0u64);
|
||||||
dwr32(file_buf, 48u64, 0u32);
|
dwr32(filebuf, 48u64, 0u32);
|
||||||
dwr16(file_buf, 52u64, ehdr_sz: u16);
|
dwr16(filebuf, 52u64, ehdrsz: u16);
|
||||||
dwr16(file_buf, 54u64, 56u16);
|
dwr16(filebuf, 54u64, 56u16);
|
||||||
dwr16(file_buf, 56u64, n_phdrs: u16);
|
dwr16(filebuf, 56u64, nphdrs: u16);
|
||||||
dwr16(file_buf, 58u64, 0u16);
|
dwr16(filebuf, 58u64, 0u16);
|
||||||
dwr16(file_buf, 60u64, 0u16);
|
dwr16(filebuf, 60u64, 0u16);
|
||||||
dwr16(file_buf, 62u64, 0u16);
|
dwr16(filebuf, 62u64, 0u16);
|
||||||
|
|
||||||
// Phdrs at offset 64.
|
// Phdrs at offset 64.
|
||||||
let p0: u64 = 64u64;
|
let p0: u64 = 64u64;
|
||||||
dwr32(file_buf, p0 + 0u64, PT_LOAD_D);
|
dwr32(filebuf, p0 + 0u64, PT_LOAD_D);
|
||||||
dwr32(file_buf, p0 + 4u64, PF_R_D | PF_X_D);
|
dwr32(filebuf, p0 + 4u64, PF_R_D | PF_X_D);
|
||||||
dwr64(file_buf, p0 + 8u64, 0u64);
|
dwr64(filebuf, p0 + 8u64, 0u64);
|
||||||
dwr64(file_buf, p0 + 16u64, base);
|
dwr64(filebuf, p0 + 16u64, base);
|
||||||
dwr64(file_buf, p0 + 24u64, base);
|
dwr64(filebuf, p0 + 24u64, base);
|
||||||
dwr64(file_buf, p0 + 32u64, rx_end);
|
dwr64(filebuf, p0 + 32u64, rxend);
|
||||||
dwr64(file_buf, p0 + 40u64, rx_end);
|
dwr64(filebuf, p0 + 40u64, rxend);
|
||||||
dwr64(file_buf, p0 + 48u64, PAGE);
|
dwr64(filebuf, p0 + 48u64, PAGE);
|
||||||
|
|
||||||
let p1: u64 = 64u64 + 56u64;
|
let p1: u64 = 64u64 + 56u64;
|
||||||
dwr32(file_buf, p1 + 0u64, PT_LOAD_D);
|
dwr32(filebuf, p1 + 0u64, PT_LOAD_D);
|
||||||
dwr32(file_buf, p1 + 4u64, PF_R_D | PF_W_D);
|
dwr32(filebuf, p1 + 4u64, PF_R_D | PF_W_D);
|
||||||
dwr64(file_buf, p1 + 8u64, gotplt_off);
|
dwr64(filebuf, p1 + 8u64, gotpltoff);
|
||||||
dwr64(file_buf, p1 + 16u64, gotplt_va);
|
dwr64(filebuf, p1 + 16u64, gotpltva);
|
||||||
dwr64(file_buf, p1 + 24u64, gotplt_va);
|
dwr64(filebuf, p1 + 24u64, gotpltva);
|
||||||
dwr64(file_buf, p1 + 32u64, file_end - gotplt_off);
|
dwr64(filebuf, p1 + 32u64, fileend - gotpltoff);
|
||||||
dwr64(file_buf, p1 + 40u64, file_end - gotplt_off);
|
dwr64(filebuf, p1 + 40u64, fileend - gotpltoff);
|
||||||
dwr64(file_buf, p1 + 48u64, PAGE);
|
dwr64(filebuf, p1 + 48u64, PAGE);
|
||||||
|
|
||||||
let p2: u64 = 64u64 + 112u64;
|
let p2: u64 = 64u64 + 112u64;
|
||||||
dwr32(file_buf, p2 + 0u64, PT_INTERP_D);
|
dwr32(filebuf, p2 + 0u64, PT_INTERP_D);
|
||||||
dwr32(file_buf, p2 + 4u64, PF_R_D);
|
dwr32(filebuf, p2 + 4u64, PF_R_D);
|
||||||
dwr64(file_buf, p2 + 8u64, interp_off);
|
dwr64(filebuf, p2 + 8u64, interpoff);
|
||||||
dwr64(file_buf, p2 + 16u64, interp_va);
|
dwr64(filebuf, p2 + 16u64, interpva);
|
||||||
dwr64(file_buf, p2 + 24u64, interp_va);
|
dwr64(filebuf, p2 + 24u64, interpva);
|
||||||
dwr64(file_buf, p2 + 32u64, interp_sz);
|
dwr64(filebuf, p2 + 32u64, interpsz);
|
||||||
dwr64(file_buf, p2 + 40u64, interp_sz);
|
dwr64(filebuf, p2 + 40u64, interpsz);
|
||||||
dwr64(file_buf, p2 + 48u64, 1u64);
|
dwr64(filebuf, p2 + 48u64, 1u64);
|
||||||
|
|
||||||
let p3: u64 = 64u64 + 168u64;
|
let p3: u64 = 64u64 + 168u64;
|
||||||
dwr32(file_buf, p3 + 0u64, PT_DYNAMIC_D);
|
dwr32(filebuf, p3 + 0u64, PT_DYNAMIC_D);
|
||||||
dwr32(file_buf, p3 + 4u64, PF_R_D | PF_W_D);
|
dwr32(filebuf, p3 + 4u64, PF_R_D | PF_W_D);
|
||||||
dwr64(file_buf, p3 + 8u64, dynamic_off);
|
dwr64(filebuf, p3 + 8u64, dynamicoff);
|
||||||
dwr64(file_buf, p3 + 16u64, dynamic_va);
|
dwr64(filebuf, p3 + 16u64, dynamicva);
|
||||||
dwr64(file_buf, p3 + 24u64, dynamic_va);
|
dwr64(filebuf, p3 + 24u64, dynamicva);
|
||||||
dwr64(file_buf, p3 + 32u64, dynamic_sz);
|
dwr64(filebuf, p3 + 32u64, dynamicsz);
|
||||||
dwr64(file_buf, p3 + 40u64, dynamic_sz);
|
dwr64(filebuf, p3 + 40u64, dynamicsz);
|
||||||
dwr64(file_buf, p3 + 48u64, 8u64);
|
dwr64(filebuf, p3 + 48u64, 8u64);
|
||||||
|
|
||||||
// Sections.
|
// Sections.
|
||||||
let interp_local: str = interpstrv();
|
let interplocal: str = interpstrv();
|
||||||
dbcopy(file_buf, interp_off, interp_local.ptr, interp_local.len: u64);
|
dbcopy(filebuf, interpoff, interplocal.ptr, interplocal.len: u64);
|
||||||
dwr8(file_buf, interp_off + (interp_local.len: u64), 0u8);
|
dwr8(filebuf, interpoff + (interplocal.len: u64), 0u8);
|
||||||
dbcopy(file_buf, dynstr_off, dynstr, dynstr_sz);
|
dbcopy(filebuf, dynstroff, dynstr, dynstrsz);
|
||||||
dbcopy(file_buf, dynsym_off, dynsym_buf, dynsym_sz);
|
dbcopy(filebuf, dynsymoff, dynsymbuf, dynsymsz);
|
||||||
dbcopy(file_buf, hash_off, hash_buf, hash_sz);
|
dbcopy(filebuf, hashoff, hashbuf, hashsz);
|
||||||
dbcopy(file_buf, versym_off, versym_buf, versym_sz);
|
dbcopy(filebuf, versymoff, versymbuf, versymsz);
|
||||||
if (verneed_sz > 0u64) {
|
if (verneedsz > 0u64) {
|
||||||
dbcopy(file_buf, verneed_off, verneed_buf, verneed_sz);
|
dbcopy(filebuf, verneedoff, verneedbuf, verneedsz);
|
||||||
};
|
};
|
||||||
dbcopy(file_buf, relaplt_off, relaplt_buf, relaplt_sz);
|
dbcopy(filebuf, relapltoff, relapltbuf, relapltsz);
|
||||||
if (l.textlen > 0u64) {
|
if (l.textlen > 0u64) {
|
||||||
dbcopy(file_buf, text_off, l.text, l.textlen);
|
dbcopy(filebuf, textoff, l.text, l.textlen);
|
||||||
};
|
};
|
||||||
dbcopy(file_buf, plt_off, plt_buf, plt_sz);
|
dbcopy(filebuf, pltoff, pltbuf, pltsz);
|
||||||
dbcopy(file_buf, gotplt_off, gotplt_buf, gotplt_sz);
|
dbcopy(filebuf, gotpltoff, gotpltbuf, gotpltsz);
|
||||||
dbcopy(file_buf, dynamic_off, dynamic_buf, dynamic_sz);
|
dbcopy(filebuf, dynamicoff, dynamicbuf, dynamicsz);
|
||||||
|
|
||||||
let wrote: i64 = os.writefull(fd, file_buf, file_end);
|
let wrote: i64 = os.writefull(fd, filebuf, fileend);
|
||||||
if (wrote != file_end: i64) {
|
if (wrote != fileend: i64) {
|
||||||
return 1;
|
return 1;
|
||||||
};
|
};
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -200,51 +200,51 @@ fn elfglobals(a: *arena, buf: *u8, len: u64) *defent = {
|
|||||||
let shnum: u32 = rdu16(buf, EHDR_SHNUM): u32;
|
let shnum: u32 = rdu16(buf, EHDR_SHNUM): u32;
|
||||||
let shstrndx: u32 = rdu16(buf, EHDR_SHSTRNDX): u32;
|
let shstrndx: u32 = rdu16(buf, EHDR_SHSTRNDX): u32;
|
||||||
|
|
||||||
let shstr_sh_off: u64 = rdu64(buf, shoff + (shstrndx: u64) * SHDR_SIZE + SHDR_OFFSET);
|
let shstrshoff: u64 = rdu64(buf, shoff + (shstrndx: u64) * SHDR_SIZE + SHDR_OFFSET);
|
||||||
let shstr: *u8 = buf + shstr_sh_off;
|
let shstr: *u8 = buf + shstrshoff;
|
||||||
|
|
||||||
let idx_text: i32 = -1;
|
let idxtext: i32 = -1;
|
||||||
let idx_symtab: i32 = -1;
|
let idxsymtab: i32 = -1;
|
||||||
let i: u32 = 0u32;
|
let i: u32 = 0u32;
|
||||||
for (i < shnum) {
|
for (i < shnum) {
|
||||||
let sh_off: u64 = shoff + (i: u64) * SHDR_SIZE;
|
let secoff: u64 = shoff + (i: u64) * SHDR_SIZE;
|
||||||
let sh_type: u32 = rdu32(buf, sh_off + SHDR_TYPE);
|
let shtype: u32 = rdu32(buf, secoff + SHDR_TYPE);
|
||||||
let sh_name: u32 = rdu32(buf, sh_off + SHDR_NAME);
|
let shname: u32 = rdu32(buf, secoff + SHDR_NAME);
|
||||||
let nm: *u8 = shstr + (sh_name: u64);
|
let nm: *u8 = shstr + (shname: u64);
|
||||||
if (sh_type == SHT_PROGBITS: u32) {
|
if (shtype == SHT_PROGBITS: u32) {
|
||||||
if (cstreq(nm, ".text")) { idx_text = i: i32; };
|
if (cstreq(nm, ".text")) { idxtext = i: i32; };
|
||||||
};
|
};
|
||||||
if (sh_type == SHT_SYMTAB: u32) { idx_symtab = i: i32; };
|
if (shtype == SHT_SYMTAB: u32) { idxsymtab = i: i32; };
|
||||||
i += 1u32;
|
i += 1u32;
|
||||||
};
|
};
|
||||||
if (idx_text < 0) { return nil; };
|
if (idxtext < 0) { return nil; };
|
||||||
if (idx_symtab < 0) { return nil; };
|
if (idxsymtab < 0) { return nil; };
|
||||||
|
|
||||||
let sym_sh: u64 = shoff + (idx_symtab: u64) * SHDR_SIZE;
|
let symsh: u64 = shoff + (idxsymtab: u64) * SHDR_SIZE;
|
||||||
let sym_off: u64 = rdu64(buf, sym_sh + SHDR_OFFSET);
|
let symoff: u64 = rdu64(buf, symsh + SHDR_OFFSET);
|
||||||
let sym_size: u64 = rdu64(buf, sym_sh + SHDR_SIZE_F);
|
let symsize: u64 = rdu64(buf, symsh + SHDR_SIZE_F);
|
||||||
let sym_link: u32 = rdu32(buf, sym_sh + SHDR_LINK);
|
let symlink: u32 = rdu32(buf, symsh + SHDR_LINK);
|
||||||
let nsyms: u64 = sym_size / SYM_SIZE;
|
let nsyms: u64 = symsize / SYM_SIZE;
|
||||||
|
|
||||||
let str_sh: u64 = shoff + (sym_link: u64) * SHDR_SIZE;
|
let strsh: u64 = shoff + (symlink: u64) * SHDR_SIZE;
|
||||||
let str_off: u64 = rdu64(buf, str_sh + SHDR_OFFSET);
|
let stroff: u64 = rdu64(buf, strsh + SHDR_OFFSET);
|
||||||
let strtab: *u8 = buf + str_off;
|
let strtab: *u8 = buf + stroff;
|
||||||
|
|
||||||
let head: *defent = nil;
|
let head: *defent = nil;
|
||||||
let si: u64 = 1u64;
|
let si: u64 = 1u64;
|
||||||
for (si < nsyms) {
|
for (si < nsyms) {
|
||||||
let sym_p: u64 = sym_off + si * SYM_SIZE;
|
let symp: u64 = symoff + si * SYM_SIZE;
|
||||||
let st_name: u32 = rdu32(buf, sym_p + SYM_NAME);
|
let stname: u32 = rdu32(buf, symp + SYM_NAME);
|
||||||
let st_info: u8 = buf[sym_p + SYM_INFO];
|
let stinfo: u8 = buf[symp + SYM_INFO];
|
||||||
let st_shndx: u16 = rdu16(buf, sym_p + SYM_SHNDX);
|
let stshndx: u16 = rdu16(buf, symp + SYM_SHNDX);
|
||||||
let bind: u32 = (st_info: u32) >> 4u32;
|
let bind: u32 = (stinfo: u32) >> 4u32;
|
||||||
// STB_GLOBAL = 1; defined in .text.
|
// STB_GLOBAL = 1; defined in .text.
|
||||||
if (bind == 1u32) {
|
if (bind == 1u32) {
|
||||||
if (st_shndx != 0u16) {
|
if (stshndx != 0u16) {
|
||||||
if ((st_shndx: i32) == idx_text) {
|
if ((stshndx: i32) == idxtext) {
|
||||||
let nm_p: *u8 = strtab + (st_name: u64);
|
let nmp: *u8 = strtab + (stname: u64);
|
||||||
if (nm_p[0u64] != 0u8) {
|
if (nmp[0u64] != 0u8) {
|
||||||
let nm: str = cstrtostr(a, nm_p);
|
let nm: str = cstrtostr(a, nmp);
|
||||||
let de: *defent = amalloc(a, 32u64): *defent;
|
let de: *defent = amalloc(a, 32u64): *defent;
|
||||||
de.name = nm;
|
de.name = nm;
|
||||||
de.dnext = head;
|
de.dnext = head;
|
||||||
@@ -283,31 +283,31 @@ fn loadarchive(l: *lnk, path: *u8, buf: *u8, len: u64) i32 = {
|
|||||||
let tail: *armember = nil;
|
let tail: *armember = nil;
|
||||||
let pos: u64 = 8u64; // past "!<arch>\n"
|
let pos: u64 = 8u64; // past "!<arch>\n"
|
||||||
for (pos + 60u64 <= len) {
|
for (pos + 60u64 <= len) {
|
||||||
let hdr_size: u64 = arfield(buf + pos + 48u64, 10u64);
|
let hdrsize: u64 = arfield(buf + pos + 48u64, 10u64);
|
||||||
let hdr_end: u64 = pos + 60u64;
|
let hdrend: u64 = pos + 60u64;
|
||||||
if (hdr_end + hdr_size > len) { break; };
|
if (hdrend + hdrsize > len) { break; };
|
||||||
let first: u8 = buf[pos];
|
let first: u8 = buf[pos];
|
||||||
// Skip the symbol table ('/'), long-name table ('//'), and
|
// Skip the symbol table ('/'), long-name table ('//'), and
|
||||||
// any padding entries (NUL or space leading byte).
|
// any padding entries (NUL or space leading byte).
|
||||||
if (first != 47u8) { if (first != 0u8) { if (first != 32u8) {
|
if (first != 47u8) { if (first != 0u8) { if (first != 32u8) {
|
||||||
let m: *armember = amalloc(l.a, 48u64): *armember;
|
let m: *armember = amalloc(l.a, 48u64): *armember;
|
||||||
m.size = hdr_size;
|
m.size = hdrsize;
|
||||||
let mb: *u8 = amalloc(l.a, hdr_size): *u8;
|
let mb: *u8 = amalloc(l.a, hdrsize): *u8;
|
||||||
let i: u64 = 0u64;
|
let i: u64 = 0u64;
|
||||||
for (i < hdr_size) {
|
for (i < hdrsize) {
|
||||||
mb[i] = buf[hdr_end + i];
|
mb[i] = buf[hdrend + i];
|
||||||
i += 1u64;
|
i += 1u64;
|
||||||
};
|
};
|
||||||
m.data = mb;
|
m.data = mb;
|
||||||
m.defs = elfglobals(l.a, mb, hdr_size);
|
m.defs = elfglobals(l.a, mb, hdrsize);
|
||||||
m.loaded = 0;
|
m.loaded = 0;
|
||||||
m.mnext = nil;
|
m.mnext = nil;
|
||||||
if (head == nil) { head = m; }
|
if (head == nil) { head = m; }
|
||||||
else { tail.mnext = m; };
|
else { tail.mnext = m; };
|
||||||
tail = m;
|
tail = m;
|
||||||
}; }; };
|
}; }; };
|
||||||
pos = hdr_end + hdr_size;
|
pos = hdrend + hdrsize;
|
||||||
if ((hdr_size & 1u64) != 0u64) { pos = pos + 1u64; };
|
if ((hdrsize & 1u64) != 0u64) { pos = pos + 1u64; };
|
||||||
};
|
};
|
||||||
|
|
||||||
let changed: i32 = 1;
|
let changed: i32 = 1;
|
||||||
@@ -360,50 +360,50 @@ fn loadimage(l: *lnk, path: *u8, buf: *u8, len: u64) i32 = {
|
|||||||
let shnum: u32 = rdu16(buf, EHDR_SHNUM): u32;
|
let shnum: u32 = rdu16(buf, EHDR_SHNUM): u32;
|
||||||
let shstrndx: u32 = rdu16(buf, EHDR_SHSTRNDX): u32;
|
let shstrndx: u32 = rdu16(buf, EHDR_SHSTRNDX): u32;
|
||||||
|
|
||||||
let shstr_sh_off: u64 = rdu64(buf, shoff + (shstrndx: u64) * SHDR_SIZE + SHDR_OFFSET);
|
let shstrshoff: u64 = rdu64(buf, shoff + (shstrndx: u64) * SHDR_SIZE + SHDR_OFFSET);
|
||||||
let shstr: *u8 = buf + shstr_sh_off;
|
let shstr: *u8 = buf + shstrshoff;
|
||||||
|
|
||||||
// find .text, .symtab, .rela.text
|
// find .text, .symtab, .rela.text
|
||||||
let idx_text: i32 = -1;
|
let idxtext: i32 = -1;
|
||||||
let idx_symtab: i32 = -1;
|
let idxsymtab: i32 = -1;
|
||||||
let idx_rela: i32 = -1;
|
let idxrela: i32 = -1;
|
||||||
let i: u32 = 0u32;
|
let i: u32 = 0u32;
|
||||||
for (i < shnum) {
|
for (i < shnum) {
|
||||||
let sh_off: u64 = shoff + (i: u64) * SHDR_SIZE;
|
let secoff: u64 = shoff + (i: u64) * SHDR_SIZE;
|
||||||
let sh_type: u32 = rdu32(buf, sh_off + SHDR_TYPE);
|
let shtype: u32 = rdu32(buf, secoff + SHDR_TYPE);
|
||||||
let sh_name: u32 = rdu32(buf, sh_off + SHDR_NAME);
|
let shname: u32 = rdu32(buf, secoff + SHDR_NAME);
|
||||||
let nm: *u8 = shstr + (sh_name: u64);
|
let nm: *u8 = shstr + (shname: u64);
|
||||||
if (sh_type == SHT_PROGBITS: u32) {
|
if (shtype == SHT_PROGBITS: u32) {
|
||||||
if (cstreq(nm, ".text")) { idx_text = i: i32; };
|
if (cstreq(nm, ".text")) { idxtext = i: i32; };
|
||||||
};
|
};
|
||||||
if (sh_type == SHT_SYMTAB: u32) { idx_symtab = i: i32; };
|
if (shtype == SHT_SYMTAB: u32) { idxsymtab = i: i32; };
|
||||||
if (sh_type == SHT_RELA: u32) {
|
if (shtype == SHT_RELA: u32) {
|
||||||
if (cstreq(nm, ".rela.text")) { idx_rela = i: i32; };
|
if (cstreq(nm, ".rela.text")) { idxrela = i: i32; };
|
||||||
};
|
};
|
||||||
i += 1u32;
|
i += 1u32;
|
||||||
};
|
};
|
||||||
if (idx_text < 0) {
|
if (idxtext < 0) {
|
||||||
os.write(2, "w6l: missing .text\n".ptr, 18u64);
|
os.write(2, "w6l: missing .text\n".ptr, 18u64);
|
||||||
return -1;
|
return -1;
|
||||||
};
|
};
|
||||||
if (idx_symtab < 0) {
|
if (idxsymtab < 0) {
|
||||||
os.write(2, "w6l: missing .symtab\n".ptr, 20u64);
|
os.write(2, "w6l: missing .symtab\n".ptr, 20u64);
|
||||||
return -1;
|
return -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
let text_sh: u64 = shoff + (idx_text: u64) * SHDR_SIZE;
|
let textsh: u64 = shoff + (idxtext: u64) * SHDR_SIZE;
|
||||||
let text_off: u64 = rdu64(buf, text_sh + SHDR_OFFSET);
|
let textoff: u64 = rdu64(buf, textsh + SHDR_OFFSET);
|
||||||
let text_size: u64 = rdu64(buf, text_sh + SHDR_SIZE_F);
|
let textsize: u64 = rdu64(buf, textsh + SHDR_SIZE_F);
|
||||||
|
|
||||||
let sym_sh: u64 = shoff + (idx_symtab: u64) * SHDR_SIZE;
|
let symsh: u64 = shoff + (idxsymtab: u64) * SHDR_SIZE;
|
||||||
let sym_off: u64 = rdu64(buf, sym_sh + SHDR_OFFSET);
|
let symoff: u64 = rdu64(buf, symsh + SHDR_OFFSET);
|
||||||
let sym_size: u64 = rdu64(buf, sym_sh + SHDR_SIZE_F);
|
let symsize: u64 = rdu64(buf, symsh + SHDR_SIZE_F);
|
||||||
let sym_link: u32 = rdu32(buf, sym_sh + SHDR_LINK);
|
let symlink: u32 = rdu32(buf, symsh + SHDR_LINK);
|
||||||
let nsyms: u64 = sym_size / SYM_SIZE;
|
let nsyms: u64 = symsize / SYM_SIZE;
|
||||||
|
|
||||||
let str_sh: u64 = shoff + (sym_link: u64) * SHDR_SIZE;
|
let strsh: u64 = shoff + (symlink: u64) * SHDR_SIZE;
|
||||||
let str_off: u64 = rdu64(buf, str_sh + SHDR_OFFSET);
|
let stroff: u64 = rdu64(buf, strsh + SHDR_OFFSET);
|
||||||
let strtab: *u8 = buf + str_off;
|
let strtab: *u8 = buf + stroff;
|
||||||
|
|
||||||
// Track this object.
|
// Track this object.
|
||||||
let ob: *lobj = amalloc(l.a, 64u64): *lobj;
|
let ob: *lobj = amalloc(l.a, 64u64): *lobj;
|
||||||
@@ -411,28 +411,28 @@ fn loadimage(l: *lnk, path: *u8, buf: *u8, len: u64) i32 = {
|
|||||||
ob.buf = buf;
|
ob.buf = buf;
|
||||||
ob.len = len;
|
ob.len = len;
|
||||||
ob.textoff = l.textlen;
|
ob.textoff = l.textlen;
|
||||||
ob.textsize = text_size;
|
ob.textsize = textsize;
|
||||||
ob.onext = l.objs;
|
ob.onext = l.objs;
|
||||||
l.objs = ob;
|
l.objs = ob;
|
||||||
|
|
||||||
// Append .text bytes to the combined image.
|
// Append .text bytes to the combined image.
|
||||||
emittext(l, buf + text_off, text_size);
|
emittext(l, buf + textoff, textsize);
|
||||||
|
|
||||||
// Walk symbols. We don't keep a per-object map[] of *lsym. Instead
|
// Walk symbols. We don't keep a per-object map[] of *lsym. Instead
|
||||||
// the reloc loop re-walks symtab and re-interns by name. Simpler
|
// the reloc loop re-walks symtab and re-interns by name. Simpler
|
||||||
// than dancing around the cgen's u64-shift gaps.
|
// than dancing around the cgen's u64-shift gaps.
|
||||||
let si: u64 = 1u64; // skip index 0 (always undef sentinel)
|
let si: u64 = 1u64; // skip index 0 (always undef sentinel)
|
||||||
for (si < nsyms) {
|
for (si < nsyms) {
|
||||||
let sym_p: u64 = sym_off + si * SYM_SIZE;
|
let symp: u64 = symoff + si * SYM_SIZE;
|
||||||
let st_name: u32 = rdu32(buf, sym_p + SYM_NAME);
|
let stname: u32 = rdu32(buf, symp + SYM_NAME);
|
||||||
let st_shndx: u16 = rdu16(buf, sym_p + SYM_SHNDX);
|
let stshndx: u16 = rdu16(buf, symp + SYM_SHNDX);
|
||||||
let st_value: u64 = rdu64(buf, sym_p + SYM_VALUE);
|
let stvalue: u64 = rdu64(buf, symp + SYM_VALUE);
|
||||||
let nm_p: *u8 = strtab + (st_name: u64);
|
let nmp: *u8 = strtab + (stname: u64);
|
||||||
if (nm_p[0u64] != 0u8) {
|
if (nmp[0u64] != 0u8) {
|
||||||
let nm: str = cstrtostr(l.a, nm_p);
|
let nm: str = cstrtostr(l.a, nmp);
|
||||||
let gs: *lsym = intern(l, nm);
|
let gs: *lsym = intern(l, nm);
|
||||||
if (st_shndx != 0u16) {
|
if (stshndx != 0u16) {
|
||||||
if ((st_shndx: i32) == idx_text) {
|
if ((stshndx: i32) == idxtext) {
|
||||||
if (gs.defined != 0) {
|
if (gs.defined != 0) {
|
||||||
os.write(2, "w6l: duplicate symbol\n".ptr, 21u64);
|
os.write(2, "w6l: duplicate symbol\n".ptr, 21u64);
|
||||||
l.errs += 1;
|
l.errs += 1;
|
||||||
@@ -440,7 +440,7 @@ fn loadimage(l: *lnk, path: *u8, buf: *u8, len: u64) i32 = {
|
|||||||
gs.defined = 1;
|
gs.defined = 1;
|
||||||
gs.owner = ob;
|
gs.owner = ob;
|
||||||
gs.idxinowner = si: i32;
|
gs.idxinowner = si: i32;
|
||||||
gs.val = ob.textoff + st_value;
|
gs.val = ob.textoff + stvalue;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -449,30 +449,30 @@ fn loadimage(l: *lnk, path: *u8, buf: *u8, len: u64) i32 = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Per-object relocation collection.
|
// Per-object relocation collection.
|
||||||
if (idx_rela >= 0) {
|
if (idxrela >= 0) {
|
||||||
let rela_sh: u64 = shoff + (idx_rela: u64) * SHDR_SIZE;
|
let relash: u64 = shoff + (idxrela: u64) * SHDR_SIZE;
|
||||||
let rela_off: u64 = rdu64(buf, rela_sh + SHDR_OFFSET);
|
let relaoff: u64 = rdu64(buf, relash + SHDR_OFFSET);
|
||||||
let rela_size: u64 = rdu64(buf, rela_sh + SHDR_SIZE_F);
|
let relasize: u64 = rdu64(buf, relash + SHDR_SIZE_F);
|
||||||
let nrel: u64 = rela_size / RELA_SIZE;
|
let nrel: u64 = relasize / RELA_SIZE;
|
||||||
let ri: u64 = 0u64;
|
let ri: u64 = 0u64;
|
||||||
for (ri < nrel) {
|
for (ri < nrel) {
|
||||||
let rp: u64 = rela_off + ri * RELA_SIZE;
|
let rp: u64 = relaoff + ri * RELA_SIZE;
|
||||||
let r_off: u64 = rdu64(buf, rp + RELA_OFFSET);
|
let roff: u64 = rdu64(buf, rp + RELA_OFFSET);
|
||||||
let r_info: u64 = rdu64(buf, rp + RELA_INFO);
|
let rinfo: u64 = rdu64(buf, rp + RELA_INFO);
|
||||||
let r_addend: u64 = rdu64(buf, rp + RELA_ADDEND);
|
let raddend: u64 = rdu64(buf, rp + RELA_ADDEND);
|
||||||
let r_sym_idx: u32 = (r_info >> 32u64): u32;
|
let rsymidx: u32 = (rinfo >> 32u64): u32;
|
||||||
let r_kind: i32 = ((r_info & 4294967295u64): u32): i32;
|
let rkind: i32 = ((rinfo & 4294967295u64): u32): i32;
|
||||||
let nr: *lrel = amalloc(l.a, 48u64): *lrel;
|
let nr: *lrel = amalloc(l.a, 48u64): *lrel;
|
||||||
nr.off = ob.textoff + r_off;
|
nr.off = ob.textoff + roff;
|
||||||
nr.kind = r_kind;
|
nr.kind = rkind;
|
||||||
nr.addend = r_addend: i64;
|
nr.addend = raddend: i64;
|
||||||
// Look up the referenced sym by name (re-walk symtab).
|
// Look up the referenced sym by name (re-walk symtab).
|
||||||
if ((r_sym_idx: u64) < nsyms) {
|
if ((rsymidx: u64) < nsyms) {
|
||||||
let s_p: u64 = sym_off + (r_sym_idx: u64) * SYM_SIZE;
|
let sp: u64 = symoff + (rsymidx: u64) * SYM_SIZE;
|
||||||
let s_name: u32 = rdu32(buf, s_p + SYM_NAME);
|
let sname: u32 = rdu32(buf, sp + SYM_NAME);
|
||||||
let s_nm: *u8 = strtab + (s_name: u64);
|
let snm: *u8 = strtab + (sname: u64);
|
||||||
if (s_nm[0u64] != 0u8) {
|
if (snm[0u64] != 0u8) {
|
||||||
let nm: str = cstrtostr(l.a, s_nm);
|
let nm: str = cstrtostr(l.a, snm);
|
||||||
nr.sym = intern(l, nm);
|
nr.sym = intern(l, nm);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ type fieldinfo = struct {
|
|||||||
type structinfo = struct {
|
type structinfo = struct {
|
||||||
sname: str,
|
sname: str,
|
||||||
fields: *fieldinfo,
|
fields: *fieldinfo,
|
||||||
tot_size: i32,
|
totsize: i32,
|
||||||
sinext: *structinfo,
|
sinext: *structinfo,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -150,9 +150,9 @@ type cgen = struct {
|
|||||||
a: *arena,
|
a: *arena,
|
||||||
locals: *local,
|
locals: *local,
|
||||||
frame: i32,
|
frame: i32,
|
||||||
last_was_return: i32,
|
lastwasreturn: i32,
|
||||||
labelseq: i32,
|
labelseq: i32,
|
||||||
strlit_seq: i32,
|
strlitseq: i32,
|
||||||
strlits: *strlit,
|
strlits: *strlit,
|
||||||
ffis: *ffi,
|
ffis: *ffi,
|
||||||
defs: *defent,
|
defs: *defent,
|
||||||
@@ -160,25 +160,25 @@ type cgen = struct {
|
|||||||
aliases: *aliasent,
|
aliases: *aliasent,
|
||||||
structs: *structinfo,
|
structs: *structinfo,
|
||||||
mods: *modent, // non-exported decls → originating module
|
mods: *modent, // non-exported decls → originating module
|
||||||
fn_name: str,
|
fnname: str,
|
||||||
fn_ret: *node, // declared return type of current fn (or nil)
|
fnret: *node, // declared return type of current fn (or nil)
|
||||||
loop_top: i32,
|
looptop: i32,
|
||||||
loop_end_buf: *str, // stack of end labels for break
|
loopendbuf: *str, // stack of end labels for break
|
||||||
loop_cont_buf: *str, // stack of cont labels for continue
|
loopcontbuf: *str, // stack of cont labels for continue
|
||||||
};
|
};
|
||||||
|
|
||||||
fn cgeninit(c: *cgen, a: *arena) void = {
|
fn cgeninit(c: *cgen, a: *arena) void = {
|
||||||
c.a = a;
|
c.a = a;
|
||||||
c.locals = nil;
|
c.locals = nil;
|
||||||
c.frame = 0;
|
c.frame = 0;
|
||||||
c.last_was_return = 0;
|
c.lastwasreturn = 0;
|
||||||
c.labelseq = 0;
|
c.labelseq = 0;
|
||||||
// Note: strlit_seq, strlits, ffis are *not* reset here; they
|
// Note: strlit_seq, strlits, ffis are *not* reset here; they
|
||||||
// persist across cgfn calls within one file. cgfile resets them
|
// persist across cgfn calls within one file. cgfile resets them
|
||||||
// at the start of each compilation unit.
|
// at the start of each compilation unit.
|
||||||
c.loop_top = 0;
|
c.looptop = 0;
|
||||||
c.loop_end_buf = amalloc(a, (LOOP_MAX: u64) * 16u64): *str;
|
c.loopendbuf = amalloc(a, (LOOP_MAX: u64) * 16u64): *str;
|
||||||
c.loop_cont_buf = amalloc(a, (LOOP_MAX: u64) * 16u64): *str;
|
c.loopcontbuf = amalloc(a, (LOOP_MAX: u64) * 16u64): *str;
|
||||||
};
|
};
|
||||||
|
|
||||||
// localalloc — append a slot for `name` without dedup. Used for
|
// localalloc — append a slot for `name` without dedup. Used for
|
||||||
@@ -310,7 +310,7 @@ fn emitoff(v: i64) void = {
|
|||||||
fn mklabel(c: *cgen, base: str) str = {
|
fn mklabel(c: *cgen, base: str) str = {
|
||||||
let buf: [128]u8;
|
let buf: [128]u8;
|
||||||
let i: i32 = 0;
|
let i: i32 = 0;
|
||||||
let fname: str = c.fn_name;
|
let fname: str = c.fnname;
|
||||||
let j: i32 = 0;
|
let j: i32 = 0;
|
||||||
for (j < fname.len) {
|
for (j < fname.len) {
|
||||||
buf[i] = fname[j];
|
buf[i] = fname[j];
|
||||||
@@ -362,8 +362,8 @@ fn internstrlit(c: *cgen, bytes: str) str = {
|
|||||||
// New label "_S_<seq>".
|
// New label "_S_<seq>".
|
||||||
let buf: [32]u8;
|
let buf: [32]u8;
|
||||||
buf[0] = 95u8; buf[1] = 83u8; buf[2] = 95u8; // "_S_"
|
buf[0] = 95u8; buf[1] = 83u8; buf[2] = 95u8; // "_S_"
|
||||||
let n: i32 = strconv.i64toa(buf[3:32], c.strlit_seq: i64);
|
let n: i32 = strconv.i64toa(buf[3:32], c.strlitseq: i64);
|
||||||
c.strlit_seq += 1;
|
c.strlitseq += 1;
|
||||||
let total: i32 = 3 + n;
|
let total: i32 = 3 + n;
|
||||||
let p: *u8 = amalloc(c.a, (total: u64) + 1u64): *u8;
|
let p: *u8 = amalloc(c.a, (total: u64) + 1u64): *u8;
|
||||||
let i: i32 = 0;
|
let i: i32 = 0;
|
||||||
|
|||||||
@@ -154,8 +154,8 @@ fn cgfnparams(c: *cgen, params: *node) void = {
|
|||||||
|
|
||||||
fn cgfn(c: *cgen, fn_: *node) void = {
|
fn cgfn(c: *cgen, fn_: *node) void = {
|
||||||
cgeninit(c, c.a);
|
cgeninit(c, c.a);
|
||||||
c.fn_name = fn_.str;
|
c.fnname = fn_.str;
|
||||||
c.fn_ret = fn_.lhs;
|
c.fnret = fn_.lhs;
|
||||||
|
|
||||||
emitline("TEXT ");
|
emitline("TEXT ");
|
||||||
if (fn_.exported == 0) {
|
if (fn_.exported == 0) {
|
||||||
@@ -212,10 +212,10 @@ fn cgfn(c: *cgen, fn_: *node) void = {
|
|||||||
emitline(", SP\n");
|
emitline(", SP\n");
|
||||||
|
|
||||||
cgfnparams(c, fn_.list);
|
cgfnparams(c, fn_.list);
|
||||||
c.last_was_return = 0;
|
c.lastwasreturn = 0;
|
||||||
if (fn_.body != nil) { cgstmt(c, fn_.body); };
|
if (fn_.body != nil) { cgstmt(c, fn_.body); };
|
||||||
|
|
||||||
if (c.last_was_return == 0) {
|
if (c.lastwasreturn == 0) {
|
||||||
// Zero AX before the fall-through return — matches C cgen,
|
// Zero AX before the fall-through return — matches C cgen,
|
||||||
// which always emits this so void-returning fns don't leak
|
// which always emits this so void-returning fns don't leak
|
||||||
// a stale callee value to their caller.
|
// a stale callee value to their caller.
|
||||||
@@ -231,7 +231,7 @@ fn cgfn(c: *cgen, fn_: *node) void = {
|
|||||||
export fn cgfile(c: *cgen, file: *node) void = {
|
export fn cgfile(c: *cgen, file: *node) void = {
|
||||||
if (file == nil) { return; };
|
if (file == nil) { return; };
|
||||||
c.strlits = nil;
|
c.strlits = nil;
|
||||||
c.strlit_seq = 0;
|
c.strlitseq = 0;
|
||||||
collectaliases(c, file);
|
collectaliases(c, file);
|
||||||
collectstructs(c, file);
|
collectstructs(c, file);
|
||||||
collectdefs(c, file);
|
collectdefs(c, file);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
// (N_INTLIT, N_RUNELIT, N_TRUE/FALSE/NIL, N_CAST) stay inline.
|
// (N_INTLIT, N_RUNELIT, N_TRUE/FALSE/NIL, N_CAST) stay inline.
|
||||||
//
|
//
|
||||||
// The remainder of cgen lives in cgen.ww (foundation: types, emit
|
// The remainder of cgen lives in cgen.ww (foundation: types, emit
|
||||||
// primitives, the collect* tables, FFI/module maps) and cgen_stmt.ww
|
// primitives, the collect* tables, FFI/module maps) and cgenstmt.ww
|
||||||
// (cgstmt).
|
// (cgstmt).
|
||||||
//
|
//
|
||||||
// `use cgenexpr;` is unnecessary at consumer sites — cgen.ww imports
|
// `use cgenexpr;` is unnecessary at consumer sites — cgen.ww imports
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
// per-kind helper: cgblock, cgreturn, cgexprstmt, cglet, cgif, cgfor,
|
// per-kind helper: cgblock, cgreturn, cgexprstmt, cglet, cgif, cgfor,
|
||||||
// cgmassign, cgbreak, cgcontinue.
|
// cgmassign, cgbreak, cgcontinue.
|
||||||
//
|
//
|
||||||
// The expression generator (cgexpr) lives in cgen_expr.ww; the
|
// The expression generator (cgexpr) lives in cgenexpr.ww; the
|
||||||
// foundation (types, emit primitives, collect* tables, FFI/module
|
// foundation (types, emit primitives, collect* tables, FFI/module
|
||||||
// maps) lives in cgen.ww.
|
// maps) lives in cgen.ww.
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ fn cgstmt(c: *cgen, n: *node) void = {
|
|||||||
if (k == N_BREAK) { cgbreak(c, n); return; };
|
if (k == N_BREAK) { cgbreak(c, n); return; };
|
||||||
if (k == N_CONTINUE) { cgcontinue(c, n); return; };
|
if (k == N_CONTINUE) { cgcontinue(c, n); return; };
|
||||||
|
|
||||||
c.last_was_return = 0;
|
c.lastwasreturn = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
fn cgblock(c: *cgen, n: *node) void = {
|
fn cgblock(c: *cgen, n: *node) void = {
|
||||||
@@ -73,16 +73,16 @@ fn cgreturn(c: *cgen, n: *node) void = {
|
|||||||
emitline("\tMOVQ\tBP, SP\n");
|
emitline("\tMOVQ\tBP, SP\n");
|
||||||
emitline("\tPOPQ\tBP\n");
|
emitline("\tPOPQ\tBP\n");
|
||||||
emitline("\tRET\n");
|
emitline("\tRET\n");
|
||||||
c.last_was_return = 1;
|
c.lastwasreturn = 1;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
// Tagged-union return: pack as (AX=tag, DX=value0, CX=value1).
|
// Tagged-union return: pack as (AX=tag, DX=value0, CX=value1).
|
||||||
// For str variant, cgexpr leaves (AX=ptr, BX=len), so we
|
// For str variant, cgexpr leaves (AX=ptr, BX=len), so we
|
||||||
// shuffle DX←AX (ptr) and CX←BX (len), then load tag.
|
// shuffle DX←AX (ptr) and CX←BX (len), then load tag.
|
||||||
// For other variants, cgexpr leaves AX, shuffle DX←AX.
|
// For other variants, cgexpr leaves AX, shuffle DX←AX.
|
||||||
if (istaggedtype(c.fn_ret)) {
|
if (istaggedtype(c.fnret)) {
|
||||||
cgexpr(c, rhs);
|
cgexpr(c, rhs);
|
||||||
let idx: i32 = taggedvariantindex(c, c.fn_ret, rhs);
|
let idx: i32 = taggedvariantindex(c, c.fnret, rhs);
|
||||||
if (nodeisstr(c, rhs)) {
|
if (nodeisstr(c, rhs)) {
|
||||||
emitline("\tMOVQ\tBX, CX\n");
|
emitline("\tMOVQ\tBX, CX\n");
|
||||||
emitline("\tMOVQ\tAX, DX\n");
|
emitline("\tMOVQ\tAX, DX\n");
|
||||||
@@ -96,7 +96,7 @@ fn cgreturn(c: *cgen, n: *node) void = {
|
|||||||
emitline("\tMOVQ\tBP, SP\n");
|
emitline("\tMOVQ\tBP, SP\n");
|
||||||
emitline("\tPOPQ\tBP\n");
|
emitline("\tPOPQ\tBP\n");
|
||||||
emitline("\tRET\n");
|
emitline("\tRET\n");
|
||||||
c.last_was_return = 1;
|
c.lastwasreturn = 1;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
cgexpr(c, rhs);
|
cgexpr(c, rhs);
|
||||||
@@ -108,19 +108,19 @@ fn cgreturn(c: *cgen, n: *node) void = {
|
|||||||
};
|
};
|
||||||
// SysV: 16-byte aggregates (str, 2-tuple) return in (AX, DX).
|
// SysV: 16-byte aggregates (str, 2-tuple) return in (AX, DX).
|
||||||
// cgexpr leaves str in (AX, BX); shuffle BX→DX.
|
// cgexpr leaves str in (AX, BX); shuffle BX→DX.
|
||||||
if (isstrtype(c, c.fn_ret)) {
|
if (isstrtype(c, c.fnret)) {
|
||||||
emitline("\tMOVQ\tBX, DX\n");
|
emitline("\tMOVQ\tBX, DX\n");
|
||||||
};
|
};
|
||||||
emitline("\tMOVQ\tBP, SP\n");
|
emitline("\tMOVQ\tBP, SP\n");
|
||||||
emitline("\tPOPQ\tBP\n");
|
emitline("\tPOPQ\tBP\n");
|
||||||
emitline("\tRET\n");
|
emitline("\tRET\n");
|
||||||
c.last_was_return = 1;
|
c.lastwasreturn = 1;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
fn cgexprstmt(c: *cgen, n: *node) void = {
|
fn cgexprstmt(c: *cgen, n: *node) void = {
|
||||||
if (n.lhs != nil) { cgexpr(c, n.lhs); };
|
if (n.lhs != nil) { cgexpr(c, n.lhs); };
|
||||||
c.last_was_return = 0;
|
c.lastwasreturn = 0;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -162,7 +162,7 @@ fn cglet(c: *cgen, n: *node) void = {
|
|||||||
emitline("\tMOVQ\tCX, ");
|
emitline("\tMOVQ\tCX, ");
|
||||||
emitoff((off + 16): i64);
|
emitoff((off + 16): i64);
|
||||||
emitline("(BP)\n");
|
emitline("(BP)\n");
|
||||||
c.last_was_return = 0;
|
c.lastwasreturn = 0;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let tagidx: i32 = taggedvariantindex(c, n.lhs, rhs);
|
let tagidx: i32 = taggedvariantindex(c, n.lhs, rhs);
|
||||||
@@ -184,7 +184,7 @@ fn cglet(c: *cgen, n: *node) void = {
|
|||||||
emitline(", ");
|
emitline(", ");
|
||||||
emitoff(off: i64);
|
emitoff(off: i64);
|
||||||
emitline("(BP)\n");
|
emitline("(BP)\n");
|
||||||
c.last_was_return = 0;
|
c.lastwasreturn = 0;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
// Struct literal init: `let p: point = point{x=..., y=...};`.
|
// Struct literal init: `let p: point = point{x=..., y=...};`.
|
||||||
@@ -224,7 +224,7 @@ fn cglet(c: *cgen, n: *node) void = {
|
|||||||
};
|
};
|
||||||
fieldnode = fieldnode.next;
|
fieldnode = fieldnode.next;
|
||||||
};
|
};
|
||||||
c.last_was_return = 0;
|
c.lastwasreturn = 0;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -262,7 +262,7 @@ fn cglet(c: *cgen, n: *node) void = {
|
|||||||
emitline("(BP)\n");
|
emitline("(BP)\n");
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
c.last_was_return = 0;
|
c.lastwasreturn = 0;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -282,7 +282,7 @@ fn cgif(c: *cgen, n: *node) void = {
|
|||||||
cgstmt(c, n.els);
|
cgstmt(c, n.els);
|
||||||
};
|
};
|
||||||
emitlabel(endl);
|
emitlabel(endl);
|
||||||
c.last_was_return = 0;
|
c.lastwasreturn = 0;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -302,18 +302,18 @@ fn cgfor(c: *cgen, n: *node) void = {
|
|||||||
emitline("\tJE\t"); emitline(endl); emitline("\n");
|
emitline("\tJE\t"); emitline(endl); emitline("\n");
|
||||||
};
|
};
|
||||||
|
|
||||||
c.loop_end_buf[c.loop_top] = endl;
|
c.loopendbuf[c.looptop] = endl;
|
||||||
c.loop_cont_buf[c.loop_top] = topl;
|
c.loopcontbuf[c.looptop] = topl;
|
||||||
c.loop_top += 1;
|
c.looptop += 1;
|
||||||
|
|
||||||
if (n.body != nil) { cgstmt(c, n.body); };
|
if (n.body != nil) { cgstmt(c, n.body); };
|
||||||
|
|
||||||
c.loop_top -= 1;
|
c.looptop -= 1;
|
||||||
|
|
||||||
if (n.rhs != nil) { cgexpr(c, n.rhs); };
|
if (n.rhs != nil) { cgexpr(c, n.rhs); };
|
||||||
emitline("\tJMP\t"); emitline(topl); emitline("\n");
|
emitline("\tJMP\t"); emitline(topl); emitline("\n");
|
||||||
emitlabel(endl);
|
emitlabel(endl);
|
||||||
c.last_was_return = 0;
|
c.lastwasreturn = 0;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -349,25 +349,25 @@ fn cgmassign(c: *cgen, n: *node) void = {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
c.last_was_return = 0;
|
c.lastwasreturn = 0;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
fn cgbreak(c: *cgen, n: *node) void = {
|
fn cgbreak(c: *cgen, n: *node) void = {
|
||||||
if (c.loop_top > 0) {
|
if (c.looptop > 0) {
|
||||||
let lbl: str = c.loop_end_buf[c.loop_top - 1];
|
let lbl: str = c.loopendbuf[c.looptop - 1];
|
||||||
emitline("\tJMP\t"); emitline(lbl); emitline("\n");
|
emitline("\tJMP\t"); emitline(lbl); emitline("\n");
|
||||||
};
|
};
|
||||||
c.last_was_return = 0;
|
c.lastwasreturn = 0;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
fn cgcontinue(c: *cgen, n: *node) void = {
|
fn cgcontinue(c: *cgen, n: *node) void = {
|
||||||
if (c.loop_top > 0) {
|
if (c.looptop > 0) {
|
||||||
let lbl: str = c.loop_cont_buf[c.loop_top - 1];
|
let lbl: str = c.loopcontbuf[c.looptop - 1];
|
||||||
emitline("\tJMP\t"); emitline(lbl); emitline("\n");
|
emitline("\tJMP\t"); emitline(lbl); emitline("\n");
|
||||||
};
|
};
|
||||||
c.last_was_return = 0;
|
c.lastwasreturn = 0;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// selfhost/cmd/wcc/cgenutil.ww — split out of cgen.ww.
|
// selfhost/cmd/wcc/cgenutil.ww — split out of cgen.ww.
|
||||||
//
|
//
|
||||||
// General helpers used across cgen_expr / cgen_stmt / cgen_decl:
|
// General helpers used across cgenexpr / cgenstmt / cgendecl:
|
||||||
// - pushargsrev: per-call arg pushing
|
// - pushargsrev: per-call arg pushing
|
||||||
// - type predicates: isstr*/isslice*/istagged*/nodeis* families
|
// - type predicates: isstr*/isslice*/istagged*/nodeis* families
|
||||||
// - field ops: fieldloadop, fieldstoreop
|
// - field ops: fieldloadop, fieldstoreop
|
||||||
@@ -619,9 +619,9 @@ fn primsize(name: str) i32 = {
|
|||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
fn slotsize(c: *cgen, typ_n: *node) i32 = {
|
fn slotsize(c: *cgen, typn: *node) i32 = {
|
||||||
if (typ_n == nil) { return 8; };
|
if (typn == nil) { return 8; };
|
||||||
let k: i32 = typ_n.kind;
|
let k: i32 = typn.kind;
|
||||||
if (k == N_TPTR) { return 8; };
|
if (k == N_TPTR) { return 8; };
|
||||||
if (k == N_TFN) { return 8; };
|
if (k == N_TFN) { return 8; };
|
||||||
if (k == N_TCHAN) { return 8; };
|
if (k == N_TCHAN) { return 8; };
|
||||||
@@ -629,7 +629,7 @@ fn slotsize(c: *cgen, typ_n: *node) i32 = {
|
|||||||
if (k == N_TTUPLE) { return 16; };
|
if (k == N_TTUPLE) { return 16; };
|
||||||
if (k == N_TTAGGED){ return 24; };
|
if (k == N_TTAGGED){ return 24; };
|
||||||
if (k == N_TNAME) {
|
if (k == N_TNAME) {
|
||||||
let nm: str = typ_n.str;
|
let nm: str = typn.str;
|
||||||
if (streq(nm, "str")) { return 16; };
|
if (streq(nm, "str")) { return 16; };
|
||||||
let ps: i32 = primsize(nm);
|
let ps: i32 = primsize(nm);
|
||||||
if (ps > 0) {
|
if (ps > 0) {
|
||||||
@@ -639,12 +639,12 @@ fn slotsize(c: *cgen, typ_n: *node) i32 = {
|
|||||||
};
|
};
|
||||||
// Named struct lookup.
|
// Named struct lookup.
|
||||||
let si: *structinfo = structlookup(c, nm);
|
let si: *structinfo = structlookup(c, nm);
|
||||||
if (si != nil) { return si.tot_size; };
|
if (si != nil) { return si.totsize; };
|
||||||
return 8;
|
return 8;
|
||||||
};
|
};
|
||||||
if (k == N_TARRAY) {
|
if (k == N_TARRAY) {
|
||||||
let lenn: *node = typ_n.rhs;
|
let lenn: *node = typn.rhs;
|
||||||
let elemn: *node = typ_n.lhs;
|
let elemn: *node = typn.lhs;
|
||||||
let elen: i64 = 1i64;
|
let elen: i64 = 1i64;
|
||||||
if (lenn != nil) {
|
if (lenn != nil) {
|
||||||
if (lenn.kind == N_INTLIT) { elen = lenn.uval: i64; };
|
if (lenn.kind == N_INTLIT) { elen = lenn.uval: i64; };
|
||||||
@@ -661,7 +661,7 @@ fn slotsize(c: *cgen, typ_n: *node) i32 = {
|
|||||||
};
|
};
|
||||||
if (k == N_TSTRUCT) {
|
if (k == N_TSTRUCT) {
|
||||||
// Inline anonymous struct — sum of field sizes.
|
// Inline anonymous struct — sum of field sizes.
|
||||||
let f: *node = typ_n.list;
|
let f: *node = typn.list;
|
||||||
let total: i32 = 0;
|
let total: i32 = 0;
|
||||||
for (f != nil) {
|
for (f != nil) {
|
||||||
if (f.kind == N_TFIELD) {
|
if (f.kind == N_TFIELD) {
|
||||||
@@ -687,7 +687,7 @@ fn fieldsize(c: *cgen, tnode: *node) i32 = {
|
|||||||
let ps: i32 = primsize(nm);
|
let ps: i32 = primsize(nm);
|
||||||
if (ps > 0) { return ps; };
|
if (ps > 0) { return ps; };
|
||||||
let si: *structinfo = structlookup(c, nm);
|
let si: *structinfo = structlookup(c, nm);
|
||||||
if (si != nil) { return si.tot_size; };
|
if (si != nil) { return si.totsize; };
|
||||||
return 8;
|
return 8;
|
||||||
};
|
};
|
||||||
if (k == N_TPTR) { return 8; };
|
if (k == N_TPTR) { return 8; };
|
||||||
@@ -710,7 +710,7 @@ fn registerstruct(c: *cgen, name: str, tstruct: *node) void = {
|
|||||||
let si: *structinfo = amalloc(c.a, 64u64): *structinfo;
|
let si: *structinfo = amalloc(c.a, 64u64): *structinfo;
|
||||||
si.sname = name;
|
si.sname = name;
|
||||||
si.fields = nil;
|
si.fields = nil;
|
||||||
si.tot_size = 0;
|
si.totsize = 0;
|
||||||
let head: *fieldinfo = nil;
|
let head: *fieldinfo = nil;
|
||||||
let tail: *fieldinfo = nil;
|
let tail: *fieldinfo = nil;
|
||||||
let off: i32 = 0;
|
let off: i32 = 0;
|
||||||
@@ -742,7 +742,7 @@ fn registerstruct(c: *cgen, name: str, tstruct: *node) void = {
|
|||||||
// Round total to 8 for stack-slot use.
|
// Round total to 8 for stack-slot use.
|
||||||
if ((off & 7) != 0) { off = (off + 7) & ~7; };
|
if ((off & 7) != 0) { off = (off + 7) & ~7; };
|
||||||
si.fields = head;
|
si.fields = head;
|
||||||
si.tot_size = off;
|
si.totsize = off;
|
||||||
si.sinext = c.structs;
|
si.sinext = c.structs;
|
||||||
c.structs = si;
|
c.structs = si;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -538,17 +538,17 @@ fn visitadd(c: *expctx, path: str) void = {
|
|||||||
|
|
||||||
// Try <dir>/<name>.ww then <dir>/<name>/<name>.ww. Returns NUL-terminated
|
// Try <dir>/<name>.ww then <dir>/<name>/<name>.ww. Returns NUL-terminated
|
||||||
// arena-resident path if found, else nil.
|
// arena-resident path if found, else nil.
|
||||||
fn locatein(a: *arena, dir: *u8, dir_len: u64, name: *u8, name_len: u64) *u8 = {
|
fn locatein(a: *arena, dir: *u8, dirlen: u64, name: *u8, namelen: u64) *u8 = {
|
||||||
// candidate 1: <dir>/<name>.ww
|
// candidate 1: <dir>/<name>.ww
|
||||||
let buf: *u8 = amalloc(a, PATH_MAX): *u8;
|
let buf: *u8 = amalloc(a, PATH_MAX): *u8;
|
||||||
let off: u64 = 0u64;
|
let off: u64 = 0u64;
|
||||||
let i: u64 = 0u64;
|
let i: u64 = 0u64;
|
||||||
for (i < dir_len) { buf[off + i] = dir[i]; i += 1u64; };
|
for (i < dirlen) { buf[off + i] = dir[i]; i += 1u64; };
|
||||||
off += dir_len;
|
off += dirlen;
|
||||||
buf[off] = 47u8; off += 1u64; // '/'
|
buf[off] = 47u8; off += 1u64; // '/'
|
||||||
i = 0u64;
|
i = 0u64;
|
||||||
for (i < name_len) { buf[off + i] = name[i]; i += 1u64; };
|
for (i < namelen) { buf[off + i] = name[i]; i += 1u64; };
|
||||||
off += name_len;
|
off += namelen;
|
||||||
buf[off] = 46u8; off += 1u64; // '.'
|
buf[off] = 46u8; off += 1u64; // '.'
|
||||||
buf[off] = 119u8; off += 1u64; // 'w'
|
buf[off] = 119u8; off += 1u64; // 'w'
|
||||||
buf[off] = 119u8; off += 1u64; // 'w'
|
buf[off] = 119u8; off += 1u64; // 'w'
|
||||||
@@ -559,16 +559,16 @@ fn locatein(a: *arena, dir: *u8, dir_len: u64, name: *u8, name_len: u64) *u8 = {
|
|||||||
let buf2: *u8 = amalloc(a, PATH_MAX): *u8;
|
let buf2: *u8 = amalloc(a, PATH_MAX): *u8;
|
||||||
off = 0u64;
|
off = 0u64;
|
||||||
i = 0u64;
|
i = 0u64;
|
||||||
for (i < dir_len) { buf2[off + i] = dir[i]; i += 1u64; };
|
for (i < dirlen) { buf2[off + i] = dir[i]; i += 1u64; };
|
||||||
off += dir_len;
|
off += dirlen;
|
||||||
buf2[off] = 47u8; off += 1u64;
|
buf2[off] = 47u8; off += 1u64;
|
||||||
i = 0u64;
|
i = 0u64;
|
||||||
for (i < name_len) { buf2[off + i] = name[i]; i += 1u64; };
|
for (i < namelen) { buf2[off + i] = name[i]; i += 1u64; };
|
||||||
off += name_len;
|
off += namelen;
|
||||||
buf2[off] = 47u8; off += 1u64;
|
buf2[off] = 47u8; off += 1u64;
|
||||||
i = 0u64;
|
i = 0u64;
|
||||||
for (i < name_len) { buf2[off + i] = name[i]; i += 1u64; };
|
for (i < namelen) { buf2[off + i] = name[i]; i += 1u64; };
|
||||||
off += name_len;
|
off += namelen;
|
||||||
buf2[off] = 46u8; off += 1u64;
|
buf2[off] = 46u8; off += 1u64;
|
||||||
buf2[off] = 119u8; off += 1u64;
|
buf2[off] = 119u8; off += 1u64;
|
||||||
buf2[off] = 119u8; off += 1u64;
|
buf2[off] = 119u8; off += 1u64;
|
||||||
@@ -578,7 +578,7 @@ fn locatein(a: *arena, dir: *u8, dir_len: u64, name: *u8, name_len: u64) *u8 = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Walk a colon-separated dirlist, return first hit or nil.
|
// Walk a colon-separated dirlist, return first hit or nil.
|
||||||
fn locateimport(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
|
fn locateimport(a: *arena, dirs: *u8, name: *u8, namelen: u64) *u8 = {
|
||||||
let total: u64 = cstrlen(dirs);
|
let total: u64 = cstrlen(dirs);
|
||||||
let p: u64 = 0u64;
|
let p: u64 = 0u64;
|
||||||
for (p < total) {
|
for (p < total) {
|
||||||
@@ -589,7 +589,7 @@ fn locateimport(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
|
|||||||
};
|
};
|
||||||
let seglen: u64 = q - p;
|
let seglen: u64 = q - p;
|
||||||
if (seglen > 0u64) {
|
if (seglen > 0u64) {
|
||||||
let hit: *u8 = locatein(a, dirs + p, seglen, name, name_len);
|
let hit: *u8 = locatein(a, dirs + p, seglen, name, namelen);
|
||||||
if (hit != nil) { return hit; };
|
if (hit != nil) { return hit; };
|
||||||
};
|
};
|
||||||
p = q + 1u64;
|
p = q + 1u64;
|
||||||
@@ -599,8 +599,8 @@ fn locateimport(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
|
|||||||
|
|
||||||
// ---- file slurp -------------------------------------------------------
|
// ---- file slurp -------------------------------------------------------
|
||||||
|
|
||||||
fn readall(path_cs: *u8) (*u8, u64) = {
|
fn readall(pathcs: *u8) (*u8, u64) = {
|
||||||
let fd: i32 = os.open(path_cs, os.O_RDONLY, 0i32);
|
let fd: i32 = os.open(pathcs, os.O_RDONLY, 0i32);
|
||||||
if (fd < 0) { return nil, 0u64; };
|
if (fd < 0) { return nil, 0u64; };
|
||||||
let n: i64 = os.filesize(fd);
|
let n: i64 = os.filesize(fd);
|
||||||
if (n < 0i64) { os.close(fd); return nil, 0u64; };
|
if (n < 0i64) { os.close(fd); return nil, 0u64; };
|
||||||
@@ -699,15 +699,15 @@ fn modulename(path: *u8, plen: u64) (*u8, u64) = {
|
|||||||
return path + prev, last - prev;
|
return path + prev, last - prev;
|
||||||
};
|
};
|
||||||
|
|
||||||
fn expand(c: *expctx, path_cs: *u8) void = {
|
fn expand(c: *expctx, pathcs: *u8) void = {
|
||||||
let plen: u64 = cstrlen(path_cs);
|
let plen: u64 = cstrlen(pathcs);
|
||||||
let pathstr: str = astrndup(c.a, path_cs, plen);
|
let pathstr: str = astrndup(c.a, pathcs, plen);
|
||||||
if (visitseen(c, pathstr)) { return; };
|
if (visitseen(c, pathstr)) { return; };
|
||||||
visitadd(c, pathstr);
|
visitadd(c, pathstr);
|
||||||
|
|
||||||
let bufp: *u8;
|
let bufp: *u8;
|
||||||
let blen: u64;
|
let blen: u64;
|
||||||
bufp, blen = readall(path_cs);
|
bufp, blen = readall(pathcs);
|
||||||
if (bufp == nil) {
|
if (bufp == nil) {
|
||||||
os.write(2, "ww: cannot read source\n".ptr, 23u64);
|
os.write(2, "ww: cannot read source\n".ptr, 23u64);
|
||||||
return;
|
return;
|
||||||
@@ -741,7 +741,7 @@ fn expand(c: *expctx, path_cs: *u8) void = {
|
|||||||
// non-exported names by module.
|
// non-exported names by module.
|
||||||
let mp: *u8;
|
let mp: *u8;
|
||||||
let mn: u64;
|
let mn: u64;
|
||||||
mp, mn = modulename(path_cs, plen);
|
mp, mn = modulename(pathcs, plen);
|
||||||
if (mn > 0u64) {
|
if (mn > 0u64) {
|
||||||
os.writefull(c.out, "// MODULE: ".ptr, 11u64);
|
os.writefull(c.out, "// MODULE: ".ptr, 11u64);
|
||||||
os.writefull(c.out, mp, mn);
|
os.writefull(c.out, mp, mn);
|
||||||
|
|||||||
@@ -219,17 +219,17 @@ fn visitadd(c: *expctx, path: str) void = {
|
|||||||
|
|
||||||
// Try <dir>/<name>.ww then <dir>/<name>/<name>.ww. Returns NUL-terminated
|
// Try <dir>/<name>.ww then <dir>/<name>/<name>.ww. Returns NUL-terminated
|
||||||
// arena-resident path if found, else nil.
|
// arena-resident path if found, else nil.
|
||||||
fn locatein(a: *arena, dir: *u8, dir_len: u64, name: *u8, name_len: u64) *u8 = {
|
fn locatein(a: *arena, dir: *u8, dirlen: u64, name: *u8, namelen: u64) *u8 = {
|
||||||
// candidate 1: <dir>/<name>.ww
|
// candidate 1: <dir>/<name>.ww
|
||||||
let buf: *u8 = amalloc(a, PATH_MAX): *u8;
|
let buf: *u8 = amalloc(a, PATH_MAX): *u8;
|
||||||
let off: u64 = 0u64;
|
let off: u64 = 0u64;
|
||||||
let i: u64 = 0u64;
|
let i: u64 = 0u64;
|
||||||
for (i < dir_len) { buf[off + i] = dir[i]; i += 1u64; };
|
for (i < dirlen) { buf[off + i] = dir[i]; i += 1u64; };
|
||||||
off += dir_len;
|
off += dirlen;
|
||||||
buf[off] = 47u8; off += 1u64; // '/'
|
buf[off] = 47u8; off += 1u64; // '/'
|
||||||
i = 0u64;
|
i = 0u64;
|
||||||
for (i < name_len) { buf[off + i] = name[i]; i += 1u64; };
|
for (i < namelen) { buf[off + i] = name[i]; i += 1u64; };
|
||||||
off += name_len;
|
off += namelen;
|
||||||
buf[off] = 46u8; off += 1u64; // '.'
|
buf[off] = 46u8; off += 1u64; // '.'
|
||||||
buf[off] = 119u8; off += 1u64; // 'w'
|
buf[off] = 119u8; off += 1u64; // 'w'
|
||||||
buf[off] = 119u8; off += 1u64; // 'w'
|
buf[off] = 119u8; off += 1u64; // 'w'
|
||||||
@@ -240,16 +240,16 @@ fn locatein(a: *arena, dir: *u8, dir_len: u64, name: *u8, name_len: u64) *u8 = {
|
|||||||
let buf2: *u8 = amalloc(a, PATH_MAX): *u8;
|
let buf2: *u8 = amalloc(a, PATH_MAX): *u8;
|
||||||
off = 0u64;
|
off = 0u64;
|
||||||
i = 0u64;
|
i = 0u64;
|
||||||
for (i < dir_len) { buf2[off + i] = dir[i]; i += 1u64; };
|
for (i < dirlen) { buf2[off + i] = dir[i]; i += 1u64; };
|
||||||
off += dir_len;
|
off += dirlen;
|
||||||
buf2[off] = 47u8; off += 1u64;
|
buf2[off] = 47u8; off += 1u64;
|
||||||
i = 0u64;
|
i = 0u64;
|
||||||
for (i < name_len) { buf2[off + i] = name[i]; i += 1u64; };
|
for (i < namelen) { buf2[off + i] = name[i]; i += 1u64; };
|
||||||
off += name_len;
|
off += namelen;
|
||||||
buf2[off] = 47u8; off += 1u64;
|
buf2[off] = 47u8; off += 1u64;
|
||||||
i = 0u64;
|
i = 0u64;
|
||||||
for (i < name_len) { buf2[off + i] = name[i]; i += 1u64; };
|
for (i < namelen) { buf2[off + i] = name[i]; i += 1u64; };
|
||||||
off += name_len;
|
off += namelen;
|
||||||
buf2[off] = 46u8; off += 1u64;
|
buf2[off] = 46u8; off += 1u64;
|
||||||
buf2[off] = 119u8; off += 1u64;
|
buf2[off] = 119u8; off += 1u64;
|
||||||
buf2[off] = 119u8; off += 1u64;
|
buf2[off] = 119u8; off += 1u64;
|
||||||
@@ -259,7 +259,7 @@ fn locatein(a: *arena, dir: *u8, dir_len: u64, name: *u8, name_len: u64) *u8 = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Walk a colon-separated dirlist, return first hit or nil.
|
// Walk a colon-separated dirlist, return first hit or nil.
|
||||||
fn locateimport(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
|
fn locateimport(a: *arena, dirs: *u8, name: *u8, namelen: u64) *u8 = {
|
||||||
let total: u64 = cstrlen(dirs);
|
let total: u64 = cstrlen(dirs);
|
||||||
let p: u64 = 0u64;
|
let p: u64 = 0u64;
|
||||||
for (p < total) {
|
for (p < total) {
|
||||||
@@ -270,7 +270,7 @@ fn locateimport(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
|
|||||||
};
|
};
|
||||||
let seglen: u64 = q - p;
|
let seglen: u64 = q - p;
|
||||||
if (seglen > 0u64) {
|
if (seglen > 0u64) {
|
||||||
let hit: *u8 = locatein(a, dirs + p, seglen, name, name_len);
|
let hit: *u8 = locatein(a, dirs + p, seglen, name, namelen);
|
||||||
if (hit != nil) { return hit; };
|
if (hit != nil) { return hit; };
|
||||||
};
|
};
|
||||||
p = q + 1u64;
|
p = q + 1u64;
|
||||||
@@ -280,8 +280,8 @@ fn locateimport(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
|
|||||||
|
|
||||||
// ---- file slurp -------------------------------------------------------
|
// ---- file slurp -------------------------------------------------------
|
||||||
|
|
||||||
fn readall(path_cs: *u8) (*u8, u64) = {
|
fn readall(pathcs: *u8) (*u8, u64) = {
|
||||||
let fd: i32 = os.open(path_cs, os.O_RDONLY, 0i32);
|
let fd: i32 = os.open(pathcs, os.O_RDONLY, 0i32);
|
||||||
if (fd < 0) { return nil, 0u64; };
|
if (fd < 0) { return nil, 0u64; };
|
||||||
let n: i64 = os.filesize(fd);
|
let n: i64 = os.filesize(fd);
|
||||||
if (n < 0i64) { os.close(fd); return nil, 0u64; };
|
if (n < 0i64) { os.close(fd); return nil, 0u64; };
|
||||||
@@ -380,15 +380,15 @@ fn modulename(path: *u8, plen: u64) (*u8, u64) = {
|
|||||||
return path + prev, last - prev;
|
return path + prev, last - prev;
|
||||||
};
|
};
|
||||||
|
|
||||||
fn expand(c: *expctx, path_cs: *u8) void = {
|
fn expand(c: *expctx, pathcs: *u8) void = {
|
||||||
let plen: u64 = cstrlen(path_cs);
|
let plen: u64 = cstrlen(pathcs);
|
||||||
let pathstr: str = astrndup(c.a, path_cs, plen);
|
let pathstr: str = astrndup(c.a, pathcs, plen);
|
||||||
if (visitseen(c, pathstr)) { return; };
|
if (visitseen(c, pathstr)) { return; };
|
||||||
visitadd(c, pathstr);
|
visitadd(c, pathstr);
|
||||||
|
|
||||||
let bufp: *u8;
|
let bufp: *u8;
|
||||||
let blen: u64;
|
let blen: u64;
|
||||||
bufp, blen = readall(path_cs);
|
bufp, blen = readall(pathcs);
|
||||||
if (bufp == nil) {
|
if (bufp == nil) {
|
||||||
os.write(2, "ww: cannot read source\n".ptr, 23u64);
|
os.write(2, "ww: cannot read source\n".ptr, 23u64);
|
||||||
return;
|
return;
|
||||||
@@ -422,7 +422,7 @@ fn expand(c: *expctx, path_cs: *u8) void = {
|
|||||||
// non-exported names by module.
|
// non-exported names by module.
|
||||||
let mp: *u8;
|
let mp: *u8;
|
||||||
let mn: u64;
|
let mn: u64;
|
||||||
mp, mn = modulename(path_cs, plen);
|
mp, mn = modulename(pathcs, plen);
|
||||||
if (mn > 0u64) {
|
if (mn > 0u64) {
|
||||||
os.writefull(c.out, "// MODULE: ".ptr, 11u64);
|
os.writefull(c.out, "// MODULE: ".ptr, 11u64);
|
||||||
os.writefull(c.out, mp, mn);
|
os.writefull(c.out, mp, mn);
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user