This generates tidier code and is pic friendly because it lets the linker trampoline calls to dynlinked libs.
Unified · Split
| @@ -306,10 +306,11 @@ fixarg(Ref *pr, int sz, E *e) · 305 unmodified lines | ||
| 306 | 306 | static void |
| 307 | 307 | emitins(Ins *i, E *e) |
| 308 | 308 | { |
| 309 | − char *rn; | |
| 309 | + char *l, *p, *rn; | |
| 310 | 310 | uint64_t s; |
| 311 | 311 | int o; |
| 312 | 312 | Ref r; |
| 313 | + Con *c; | |
| 313 | 314 | |
| 314 | 315 | switch (i->op) { |
| 315 | 316 | default: |
| @@ -355,7 +356,8 @@ emitins(Ins *i, E *e) · 39 unmodified lines | ||
| 355 | 356 | assert(isreg(i->to)); |
| 356 | 357 | switch (rtype(i->arg[0])) { |
| 357 | 358 | case RCon: |
| 358 | − loadcon(&e->fn->con[i->arg[0].val], i->to.val, i->cls, e->f); | |
| 359 | + c = &e->fn->con[i->arg[0].val]; | |
| 360 | + loadcon(c, i->to.val, i->cls, e->f); | |
| 359 | 361 | break; |
| 360 | 362 | case RSlot: |
| 361 | 363 | i->op = Oload; |
| @@ -386,6 +388,16 @@ emitins(Ins *i, E *e) · 24 unmodified lines | ||
| 386 | 388 | rn, s & 0xFFFF, rn, s >> 16, rn, rn |
| 387 | 389 | ); |
| 388 | 390 | break; |
| 391 | + case Ocall: | |
| 392 | + if (rtype(i->arg[0]) != RCon) | |
| 393 | + goto Table; | |
| 394 | + c = &e->fn->con[i->arg[0].val]; | |
| 395 | + if (c->type != CAddr || c->bits.i) | |
| 396 | + die("invalid call argument"); | |
| 397 | + l = str(c->label); | |
| 398 | + p = l[0] == '"' ? "" : T.assym; | |
| 399 | + fprintf(e->f, "\tbl\t%s%s\n", p, l); | |
| 400 | + break; | |
| 389 | 401 | case Osalloc: |
| 390 | 402 | emitf("sub sp, sp, %0", i, e); |
| 391 | 403 | if (!req(i->to, R)) |
| @@ -156,6 +156,22 @@ selcmp(Ref arg[2], int k, Fn *fn) · 155 unmodified lines | ||
| 156 | 156 | return swap; |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | +static int | |
| 160 | +callable(Ref r, Fn *fn) | |
| 161 | +{ | |
| 162 | + Con *c; | |
| 163 | + | |
| 164 | + if (rtype(r) == RTmp) | |
| 165 | + return 1; | |
| 166 | + if (rtype(r) == RCon) { | |
| 167 | + c = &fn->con[r.val]; | |
| 168 | + if (c->type == CAddr) | |
| 169 | + if (c->bits.i == 0) | |
| 170 | + return 1; | |
| 171 | + } | |
| 172 | + return 0; | |
| 173 | +} | |
| 174 | + | |
| 159 | 175 | static void |
| 160 | 176 | sel(Ins i, Fn *fn) |
| 161 | 177 | { |
| @@ -178,6 +194,11 @@ sel(Ins i, Fn *fn) · 16 unmodified lines | ||
| 178 | 194 | i0->op += cc; |
| 179 | 195 | return; |
| 180 | 196 | } |
| 197 | + if (i.op == Ocall) | |
| 198 | + if (callable(i.arg[0], fn)) { | |
| 199 | + emiti(i); | |
| 200 | + return; | |
| 201 | + } | |
| 181 | 202 | if (i.op != Onop) { |
| 182 | 203 | emiti(i); |
| 183 | 204 | iarg = curi->arg; /* fixarg() can change curi */ |