1
// Inferno utils/5l/noop.c
2
// http://code.google.com/p/inferno-os/source/browse/utils/5l/noop.c
4
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
5
// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
6
// Portions Copyright © 1997-1999 Vita Nuova Limited
7
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
8
// Portions Copyright © 2004,2006 Bruce Ellis
9
// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
10
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
11
// Portions Copyright © 2009 The Go Authors. All rights reserved.
13
// Permission is hereby granted, free of charge, to any person obtaining a copy
14
// of this software and associated documentation files (the "Software"), to deal
15
// in the Software without restriction, including without limitation the rights
16
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
// copies of the Software, and to permit persons to whom the Software is
18
// furnished to do so, subject to the following conditions:
20
// The above copyright notice and this permission notice shall be included in
21
// all copies or substantial portions of the Software.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31
// Code transformations.
34
#include "../ld/lib.h"
36
// see ../../runtime/proc.c:/StackGuard
48
static void setdiv(int);
51
movrr(Prog *q, int rs, int rd, Prog *p)
66
fnret(Prog *q, int rs, int foreign, Prog *p)
68
q = movrr(q, rs, REGPC, p);
71
q->from.type = D_NONE;
79
aword(int32 w, Prog *p)
86
q->from.type = D_NONE;
96
adword(int32 w1, int32 w2, Prog *p)
103
q->from.type = D_CONST;
106
q->to.type = D_CONST;
116
Prog *p, *q, *q1, *q2;
122
* find leaf subroutines
125
* expand BECOME pseudo
129
Bprint(&bso, "%5.2f noops\n", cputime());
132
symmorestack = lookup("runtime.morestack", 0);
133
if(symmorestack->type != STEXT) {
134
diag("runtime·morestack not defined");
137
pmorestack = symmorestack->text;
138
pmorestack->reg |= NOSPLIT;
141
for(cursym = textp; cursym != nil; cursym = cursym->next) {
142
for(p = cursym->text; p != P; p = p->link) {
160
cursym->text->mark &= ~LEAF;
166
q->link = q1; /* q is non-nop */
173
cursym->text->mark &= ~LEAF;
196
while(q1->as == ANOP) {
207
for(cursym = textp; cursym != nil; cursym = cursym->next) {
208
for(p = cursym->text; p != P; p = p->link) {
213
autosize = p->to.offset + 4;
215
if(cursym->text->mark & LEAF) {
220
if(!autosize && !(cursym->text->mark & LEAF)) {
222
Bprint(&bso, "save suppressed in: %s\n",
225
cursym->text->mark |= LEAF;
228
if(p->from.sym->foreign){
230
// don't allow literal pool to separate these
231
p = adword(0xe28f7001, 0xe12fff17, p); // arm add 1, pc, r7 and bx r7
232
// p = aword(0xe12fff17, aword(0xe28f7001, p)); // arm add 1, pc, r7 and bx r7
234
p = aword(0x4778, p); // thumb bx pc and 2 bytes padding
237
if(cursym->text->mark & LEAF) {
244
if(!(p->reg & NOSPLIT))
245
diag("stack splitting not supported in thumb");
246
if(!(cursym->text->mark & LEAF)){
247
q = movrr(nil, REGLINK, REGTMPT-1, p);
252
q1->from.type = D_REG;
253
q1->from.reg = REGTMPT-1;
254
q1->to.type = D_OREG;
255
q1->to.name = D_NONE;
265
q2->from.type = D_CONST;
266
q2->from.offset = autosize;
275
if(p->reg & NOSPLIT) {
280
q1->from.type = D_REG;
281
q1->from.reg = REGLINK;
282
q1->to.type = D_OREG;
283
q1->to.offset = -autosize;
285
q1->spadj = autosize;
288
} else if (autosize < StackBig) {
289
// split stack check for small functions
290
// MOVW g_stackguard(g), R1
291
// CMP R1, $-autosize(SP)
292
// MOVW.LO $autosize, R1
295
// BL.LO runtime.morestack(SB) // modifies LR
296
// MOVW.W R14,$-autosize(SP)
298
// TODO(kaib): add more trampolines
299
// TODO(kaib): put stackguard in register
300
// TODO(kaib): add support for -K and underflow detection
302
// MOVW g_stackguard(g), R1
305
p->from.type = D_OREG;
310
if(autosize < StackSmall) {
314
p->from.type = D_REG;
318
// MOVW $-autosize(SP), R2
322
p->from.type = D_CONST;
324
p->from.offset = -autosize;
330
p->from.type = D_REG;
335
// MOVW.LO $autosize, R1
338
p->scond = C_SCOND_LO;
339
p->from.type = D_CONST;
340
/* 160 comes from 3 calls (3*8) 4 safes (4*8) and 104 guard */
341
p->from.offset = autosize+160;
348
p->scond = C_SCOND_LO;
349
p->from.type = D_CONST;
350
p->from.offset = (cursym->text->to.offset2 + 3) & ~3;
357
p->scond = C_SCOND_LO;
358
p->from.type = D_REG;
359
p->from.reg = REGLINK;
363
// BL.LO runtime.morestack(SB) // modifies LR
366
p->scond = C_SCOND_LO;
367
p->to.type = D_BRANCH;
368
p->to.sym = symmorestack;
369
p->cond = pmorestack;
371
// MOVW.W R14,$-autosize(SP)
375
p->from.type = D_REG;
376
p->from.reg = REGLINK;
378
p->to.offset = -autosize;
381
} else { // > StackBig
382
// MOVW $autosize, R1
385
// BL runtime.morestack(SB) // modifies LR
386
// MOVW.W R14,$-autosize(SP)
388
// MOVW $autosize, R1
391
p->from.type = D_CONST;
392
p->from.offset = autosize;
397
// also need to store the extra 4 bytes.
400
p->from.type = D_CONST;
401
p->from.offset = (cursym->text->to.offset2 + 3) & ~3;
408
p->from.type = D_REG;
409
p->from.reg = REGLINK;
413
// BL runtime.morestack(SB) // modifies LR
416
p->to.type = D_BRANCH;
417
p->to.sym = symmorestack;
418
p->cond = pmorestack;
420
// MOVW.W R14,$-autosize(SP)
424
p->from.type = D_REG;
425
p->from.reg = REGLINK;
427
p->to.offset = -autosize;
435
foreign = seenthumb && (cursym->foreign || cursym->fnptr);
436
// print("%s %d %d\n", cursym->name, cursym->foreign, cursym->fnptr);
437
if(cursym->text->mark & LEAF) {
440
p = fnret(p, REGLINK, foreign, p);
443
// if(foreign) print("ABXRET 1 %s\n", cursym->name);
444
p->as = foreign ? ABXRET : AB;
453
if(cursym->text->mark & LEAF){
456
p->from.type = D_CONST;
457
p->from.offset = autosize;
464
q = fnret(q, REGLINK, foreign, p);
470
p->from.type = D_OREG;
471
p->from.name = D_NONE;
475
p->to.reg = REGTMPT-1;
479
q->from.type = D_CONST;
480
q->from.offset = autosize;
488
q1 = fnret(nil, REGTMPT-1, foreign, p);
495
// if(foreign) print("ABXRET 3 %s\n", cursym->name);
498
p->from.type = D_OREG;
499
p->from.name = D_NONE;
508
q->from.type = D_CONST;
509
q->from.offset = autosize;
516
q1->scond = p->scond;
518
q1->to.type = D_OREG;
528
p->from.type = D_OREG;
529
p->from.offset = autosize;
533
// no spadj because it doesn't fall through
538
if(p->from.type == D_CONST && p->from.reg == NREG && p->to.type == D_REG && p->to.reg == REGSP)
539
p->spadj = -p->from.offset;
543
if(p->from.type == D_CONST && p->from.reg == NREG && p->to.type == D_REG && p->to.reg == REGSP)
544
p->spadj = p->from.offset;
553
if(p->from.type != D_REG)
555
if(p->to.type != D_REG)
567
p->from.type = D_REG;
568
p->from.reg = q1->from.reg;
581
p->from.type = D_REG;
582
p->from.reg = q1->reg;
584
p->from.reg = q1->to.reg;
586
p->to.reg = prog_div->from.sym->thumb ? REGTMPT : REGTMP;
589
/* CALL appropriate */
598
if(prog_div->from.sym->thumb)
599
p->as = thumb ? ABL : ABX;
601
p->as = thumb ? ABX : ABL;
604
p->to.type = D_BRANCH;
613
p->to.sym = sym_divu;
621
p->to.sym = sym_modu;
633
p->from.type = D_REG;
634
p->from.reg = prog_div->from.sym->thumb ? REGTMPT : REGTMP;
637
p->to.reg = q1->to.reg;
646
p->from.type = D_CONST;
656
q1->from.type = D_CONST;
669
if(a->type == D_CONST && ((a->name == D_NONE && a->reg == REGSP) || a->name == D_AUTO || a->name == D_PARAM) && (a->offset & 3))
670
diag("SP offset not multiple of 4");
672
if((p->scond & C_WBIT) && p->to.type == D_OREG && p->to.reg == REGSP)
673
p->spadj = -p->to.offset;
674
if((p->scond & C_PBIT) && p->from.type == D_OREG && p->from.reg == REGSP && p->to.reg != REGPC)
675
p->spadj = -p->from.offset;
676
if(p->from.type == D_CONST && p->from.reg == REGSP && p->to.type == D_REG && p->to.reg == REGSP)
677
p->spadj = -p->from.offset;
684
if(p->from.type == D_OREG && (p->from.name == D_AUTO || p->from.name == D_PARAM || (p->from.name == D_CONST && p->from.reg == REGSP))){
687
if(p->from.name == D_AUTO)
688
q->from.offset += autosize;
689
else if(p->from.name == D_PARAM)
690
q->from.offset += autosize+4;
691
q->from.name = D_NONE;
692
q->from.reg = REGTMPT;
693
p = movrr(p, REGSP, REGTMPT, p);
697
if(p->to.type == D_OREG && (p->to.name == D_AUTO || p->to.name == D_PARAM || (p->to.name == D_CONST && p->to.reg == REGSP))){
700
if(p->to.name == D_AUTO)
701
q->to.offset += autosize;
702
else if(p->to.name == D_PARAM)
703
q->to.offset += autosize+4;
706
p = movrr(p, REGSP, REGTMPT, p);
709
if(q->to.offset < 0 || q->to.offset > 255){ // complicated
710
p->to.reg = REGTMPT+1; // mov sp, r8
714
q1->from.type = D_CONST;
715
q1->from.offset = q->to.offset;
717
q1->to.reg = REGTMPT; // mov $o, r7
723
q1->from.type = D_REG;
724
q1->from.reg = REGTMPT+1;
726
q1->to.reg = REGTMPT; // add r8, r7
729
q->to.offset = 0; // mov* r, 0(r7)
737
if(p->from.type == D_OREG){
738
if(p->from.offset == 0)
739
p->from.type = D_REG;
741
diag("non-zero AMOVM offset");
743
else if(p->to.type == D_OREG){
744
if(p->to.offset == 0)
747
diag("non-zero AMOVM offset");
752
if(thumb && p->to.type == D_OREG){
753
if(p->to.offset == 0){
755
p->from.type = D_REG;
756
p->from.reg = p->to.reg;
762
p->from.type = D_CONST;
763
p->from.offset = p->to.offset;
766
p->to.reg = REGTMPT-1;
770
q->from.type = D_REG;
771
q->from.reg = REGTMPT-1;
778
if(seenthumb && !thumb && p->to.type == D_OREG && p->to.reg == REGLINK){
779
// print("warn %s: b (R%d) assuming a return\n", cursym->name, p->to.reg);
785
if(thumb && p->to.type == D_OREG){
786
if(p->to.offset == 0){
788
p->from.type = D_NONE;
793
p->from.type = D_CONST;
794
p->from.offset = p->to.offset;
797
p->to.reg = REGTMPT-1;
801
q->from.type = D_NONE;
803
q->to.reg = REGTMPT-1;
837
Sym *s2, *s3, *s4, *s5;
841
sym_div = s2 = lookup("_div", 0);
842
sym_divu = s3 = lookup("_divu", 0);
843
sym_mod = s4 = lookup("_mod", 0);
844
sym_modu = s5 = lookup("_modu", 0);
846
prog_divu = s3->text;
848
prog_modu = s5->text;
850
diag("undefined: %s", s2->name);
851
prog_div = cursym->text;
854
diag("undefined: %s", s3->name);
855
prog_divu = cursym->text;
858
diag("undefined: %s", s4->name);
859
prog_mod = cursym->text;
862
diag("undefined: %s", s5->name);
863
prog_modu = cursym->text;
873
case ADIV: p = prog_div; break;
874
case ADIVU: p = prog_divu; break;
875
case AMOD: p = prog_mod; break;
876
case AMODU: p = prog_modu; break;
878
if(thumb != p->from.sym->thumb)
879
p->from.sym->foreign = 1;