From 01675e8091f869a4c8fc2902714e8846a1c9150d Mon Sep 17 00:00:00 2001 From: David du Colombier <0intro@gmail.com> Date: Tue, 20 Oct 2015 21:40:00 +0200 Subject: [PATCH 03/10] cmd/4acl: import from plan9.bell-labs.com/sources/contrib/tim/4acl.tgz --- sys/src/cmd/4a/a.h | 184 ++++++ sys/src/cmd/4a/a.y | 613 ++++++++++++++++++++ sys/src/cmd/4a/l.s | 703 ++++++++++++++++++++++ sys/src/cmd/4a/lex.c | 704 ++++++++++++++++++++++ sys/src/cmd/4a/mkfile | 26 + sys/src/cmd/4a/note | 51 ++ sys/src/cmd/4c/bits.c | 127 ++++ sys/src/cmd/4c/cgen.c | 1157 +++++++++++++++++++++++++++++++++++++ sys/src/cmd/4c/gc.h | 332 +++++++++++ sys/src/cmd/4c/list.c | 249 ++++++++ sys/src/cmd/4c/mkenam | 15 + sys/src/cmd/4c/mkfile | 42 ++ sys/src/cmd/4c/mul.c | 608 +++++++++++++++++++ sys/src/cmd/4c/peep.c | 693 ++++++++++++++++++++++ sys/src/cmd/4c/reg.c | 1149 ++++++++++++++++++++++++++++++++++++ sys/src/cmd/4c/sgen.c | 569 ++++++++++++++++++ sys/src/cmd/4c/swt.c | 702 ++++++++++++++++++++++ sys/src/cmd/4c/txt.c | 1459 ++++++++++++++++++++++++++++++++++++++++++++++ sys/src/cmd/4l/asm.c | 1477 +++++++++++++++++++++++++++++++++++++++++++++++ sys/src/cmd/4l/compat.c | 51 ++ sys/src/cmd/4l/enam.c | 121 ++++ sys/src/cmd/4l/l.h | 331 +++++++++++ sys/src/cmd/4l/list.c | 281 +++++++++ sys/src/cmd/4l/mkfile | 38 ++ sys/src/cmd/4l/noop.c | 420 ++++++++++++++ sys/src/cmd/4l/obj.c | 1395 ++++++++++++++++++++++++++++++++++++++++++++ sys/src/cmd/4l/optab.c | 221 +++++++ sys/src/cmd/4l/pass.c | 538 +++++++++++++++++ sys/src/cmd/4l/sched.c | 709 +++++++++++++++++++++++ sys/src/cmd/4l/span.c | 628 ++++++++++++++++++++ 30 files changed, 15593 insertions(+) create mode 100644 sys/src/cmd/4a/a.h create mode 100644 sys/src/cmd/4a/a.y create mode 100644 sys/src/cmd/4a/l.s create mode 100644 sys/src/cmd/4a/lex.c create mode 100644 sys/src/cmd/4a/mkfile create mode 100644 sys/src/cmd/4a/note create mode 100644 sys/src/cmd/4c/bits.c create mode 100644 sys/src/cmd/4c/cgen.c create mode 100644 sys/src/cmd/4c/gc.h create mode 100644 sys/src/cmd/4c/list.c create mode 100644 sys/src/cmd/4c/mkenam create mode 100644 sys/src/cmd/4c/mkfile create mode 100644 sys/src/cmd/4c/mul.c create mode 100644 sys/src/cmd/4c/peep.c create mode 100644 sys/src/cmd/4c/reg.c create mode 100644 sys/src/cmd/4c/sgen.c create mode 100644 sys/src/cmd/4c/swt.c create mode 100644 sys/src/cmd/4c/txt.c create mode 100644 sys/src/cmd/4l/asm.c create mode 100644 sys/src/cmd/4l/compat.c create mode 100644 sys/src/cmd/4l/enam.c create mode 100644 sys/src/cmd/4l/l.h create mode 100644 sys/src/cmd/4l/list.c create mode 100644 sys/src/cmd/4l/mkfile create mode 100644 sys/src/cmd/4l/noop.c create mode 100644 sys/src/cmd/4l/obj.c create mode 100644 sys/src/cmd/4l/optab.c create mode 100644 sys/src/cmd/4l/pass.c create mode 100644 sys/src/cmd/4l/sched.c create mode 100644 sys/src/cmd/4l/span.c diff --git a/sys/src/cmd/4a/a.h b/sys/src/cmd/4a/a.h new file mode 100644 index 0000000..439b513 --- /dev/null +++ b/sys/src/cmd/4a/a.h @@ -0,0 +1,184 @@ +#include +#include +#include +#include "../vc/v.out.h" + +#ifndef EXTERN +#define EXTERN extern +#endif + +#define MAXALIGN 7 + +typedef struct Sym Sym; +typedef struct Gen Gen; +typedef struct Io Io; +typedef struct Hist Hist; + +#define FPCHIP 1 +#define NSYMB 8192 +#define BUFSIZ 8192 +#define HISTSZ 20 +#define NINCLUDE 10 +#define NHUNK 10000 +#define EOF (-1) +#define IGN (-2) +#define GETC() ((--fi.c < 0)? filbuf(): *fi.p++ & 0xff) +#define NHASH 503 +#define STRINGSZ 200 +#define NMACRO 10 + +struct Sym +{ + Sym* link; + char* macro; + vlong value; + ushort type; + char *name; + char sym; +}; +#define S ((Sym*)0) + +EXTERN struct +{ + char* p; + int c; +} fi; + +struct Io +{ + Io* link; + char b[BUFSIZ]; + char* p; + short c; + short f; +}; +#define I ((Io*)0) + +EXTERN struct +{ + Sym* sym; + short type; +} h[NSYM]; + +struct Gen +{ + Sym* sym; + long offset; + short type; + short reg; + short name; + double dval; + char sval[8]; + vlong vval; +}; + +struct Hist +{ + Hist* link; + char* name; + long line; + vlong offset; +}; +#define H ((Hist*)0) + +enum +{ + CLAST, + CMACARG, + CMACRO, + CPREPROC, +}; + +EXTERN char debug[256]; +EXTERN Sym* hash[NHASH]; +EXTERN char* Dlist[30]; +EXTERN int nDlist; +EXTERN Hist* ehist; +EXTERN int newflag; +EXTERN Hist* hist; +EXTERN char* hunk; +EXTERN char* include[NINCLUDE]; +EXTERN Io* iofree; +EXTERN Io* ionext; +EXTERN Io* iostack; +EXTERN long lineno; +EXTERN int nerrors; +EXTERN long nhunk; +EXTERN int nosched; +EXTERN int ninclude; +EXTERN Gen nullgen; +EXTERN char* outfile; +EXTERN int pass; +EXTERN char* pathname; +EXTERN vlong pc; +EXTERN int peekc; +EXTERN int sym; +EXTERN char symb[NSYMB]; +EXTERN int thechar; +EXTERN char* thestring; +EXTERN long thunk; +EXTERN Biobuf obuf; + +void* alloc(long); +void* allocn(void*, long, long); +void errorexit(void); +void pushio(void); +void newio(void); +void newfile(char*, int); +Sym* slookup(char*); +Sym* lookup(void); +void syminit(Sym*); +long yylex(void); +int getc(void); +int getnsc(void); +void unget(int); +int escchar(int); +void cinit(void); +void pinit(char*); +void cclean(void); +int isreg(Gen*); +void outcode(int, Gen*, int, Gen*); +void zname(char*, int, int); +void zaddr(Gen*, int); +void ieeedtod(Ieee*, double); +int filbuf(void); +Sym* getsym(void); +void domacro(void); +void macund(void); +void macdef(void); +void macexpand(Sym*, char*); +void macinc(void); +void maclin(void); +void macprag(void); +void macif(int); +void macend(void); +void outhist(void); +void dodefine(char*); +void prfile(long); +void linehist(char*, int); +void gethunk(void); +void yyerror(char*, ...); +int yyparse(void); +void setinclude(char*); +int assemble(char*); + +/* + * system-dependent stuff from ../cc/compat.c + */ + +enum /* keep in synch with ../cc/cc.h */ +{ + Plan9 = 1<<0, + Unix = 1<<1, + Windows = 1<<2, +}; +int mywait(int*); +int mycreat(char*, int); +int systemtype(int); +int pathchar(void); +char* mygetwd(char*, int); +int myexec(char*, char*[]); +int mydup(int, int); +int myfork(void); +int mypipe(int*); +void* mysbrk(ulong); diff --git a/sys/src/cmd/4a/a.y b/sys/src/cmd/4a/a.y new file mode 100644 index 0000000..c37dac7 --- /dev/null +++ b/sys/src/cmd/4a/a.y @@ -0,0 +1,613 @@ +%{ +#include "a.h" +%} +%union +{ + Sym *sym; + vlong lval; + double dval; + char sval[8]; + Gen gen; +} +%left '|' +%left '^' +%left '&' +%left '<' '>' +%left '+' '-' +%left '*' '/' '%' +%token LTYPE1 LTYPE2 LTYPE3 LTYPE4 LTYPE5 +%token LTYPE6 LTYPE7 LTYPE8 LTYPE9 LTYPEA +%token LTYPEB LTYPEC LTYPED LTYPEE LTYPEF +%token LTYPEG LTYPEH LTYPEI LTYPEJ LTYPEK +%token LCONST LSP LSB LFP LPC LHI LLO LMREG +%token LTYPEX LREG LFREG LFCREG LR LM LF +%token LFCR LSCHED +%token LFCONST +%token LSCONST +%token LVCONST +%token LNAME LLAB LVAR +%type con expr pointer offset sreg +%type gen vgen lgen vlgen rel reg freg mreg fcreg +%type imm ximm ireg name oreg imr nireg fgen +%% +prog: +| prog line + +line: + LLAB ':' + { + if($1->value != pc) + yyerror("redeclaration of %s", $1->name); + $1->value = pc; + } + line +| LNAME ':' + { + $1->type = LLAB; + $1->value = pc; + } + line +| LNAME '=' expr ';' + { + $1->type = LVAR; + $1->value = $3; + } +| LVAR '=' expr ';' + { + if($1->value != $3) + yyerror("redeclaration of %s", $1->name); + $1->value = $3; + } +| LSCHED ';' + { + nosched = $1; + } +| ';' +| inst ';' +| error ';' + +inst: +/* + * Immed-type + */ + LTYPE1 imr ',' sreg ',' reg + { + outcode($1, &$2, $4, &$6); + } +| LTYPE1 imr ',' reg + { + outcode($1, &$2, NREG, &$4); + } +/* + * NOR + */ +| LTYPE2 imr ',' sreg ',' imr + { + outcode($1, &$2, $4, &$6); + } +| LTYPE2 imr ',' imr + { + outcode($1, &$2, NREG, &$4); + } +/* + * LOAD/STORE, but not MOVW + */ +| LTYPE3 lgen ',' gen + { + if(!isreg(&$2) && !isreg(&$4)) + print("one side must be register\n"); + outcode($1, &$2, NREG, &$4); + } +/* + * SPECIAL + */ +| LTYPE4 comma + { + outcode($1, &nullgen, NREG, &nullgen); + } +/* + * MOVW + */ +| LTYPE5 vlgen ',' vgen + { + if(!isreg(&$2) && !isreg(&$4)) + print("one side must be register\n"); + outcode($1, &$2, NREG, &$4); + } +/* + * MUL/DIV + */ +| LTYPE6 reg ',' sreg comma + { + outcode($1, &$2, $4, &nullgen); + } +| LTYPE6 reg ',' sreg ',' reg + { + outcode($1, &$2, $4, &$6); + } +/* + * JMP/JAL + */ +| LTYPE7 comma rel + { + outcode($1, &nullgen, NREG, &$3); + } +| LTYPE7 comma nireg + { + outcode($1, &nullgen, NREG, &$3); + } +| LTYPE8 comma rel + { + outcode($1, &nullgen, NREG, &$3); + } +| LTYPE8 comma nireg + { + outcode($1, &nullgen, NREG, &$3); + } +| LTYPE8 sreg ',' nireg + { + outcode($1, &nullgen, $2, &$4); + } +/* + * BEQ/BNE + */ +| LTYPE9 gen ',' rel + { + if(!isreg(&$2)) + print("left side must be register\n"); + outcode($1, &$2, NREG, &$4); + } +| LTYPE9 gen ',' sreg ',' rel + { + if(!isreg(&$2)) + print("left side must be register\n"); + outcode($1, &$2, $4, &$6); + } +/* + * B-other + */ +| LTYPEA gen ',' rel + { + if(!isreg(&$2)) + print("left side must be register\n"); + outcode($1, &$2, NREG, &$4); + } +/* + * TEXT/GLOBL + */ +| LTYPEB name ',' imm + { + outcode($1, &$2, NREG, &$4); + } +| LTYPEB name ',' con ',' imm + { + outcode($1, &$2, $4, &$6); + } +/* + * DATA + */ +| LTYPEC name '/' con ',' ximm + { + outcode($1, &$2, $4, &$6); + } +/* + * floating-type + */ +| LTYPED freg ',' freg + { + outcode($1, &$2, NREG, &$4); + } +| LTYPEE freg ',' freg + { + outcode($1, &$2, NREG, &$4); + } +| LTYPEE freg ',' LFREG ',' freg + { + outcode($1, &$2, $4, &$6); + } +| LTYPEF freg ',' LFREG comma + { + outcode($1, &$2, $4, &nullgen); + } +/* + * coprocessor branch + */ +| LTYPEG comma rel + { + outcode($1, &nullgen, NREG, &$3); + } +/* + * word + */ +| LTYPEH comma ximm + { + outcode($1, &nullgen, NREG, &$3); + } +/* + * NOP + */ +| LTYPEI comma + { + outcode($1, &nullgen, NREG, &nullgen); + } +| LTYPEI ',' vgen + { + outcode($1, &nullgen, NREG, &$3); + } +| LTYPEI vgen comma + { + outcode($1, &$2, NREG, &nullgen); + } +/* + * BREAK -- overloaded with CACHE opcode + */ +| LTYPEJ comma + { + outcode($1, &nullgen, NREG, &nullgen); + } +| LTYPEJ vgen ',' vgen + { + outcode($1, &$2, NREG, &$4); + } + +comma: +| ',' + +rel: + con '(' LPC ')' + { + $$ = nullgen; + $$.type = D_BRANCH; + $$.offset = $1 + pc; + } +| LNAME offset + { + $$ = nullgen; + if(pass == 2) + yyerror("undefined label: %s", $1->name); + $$.type = D_BRANCH; + $$.sym = $1; + $$.offset = $2; + } +| LLAB offset + { + $$ = nullgen; + $$.type = D_BRANCH; + $$.sym = $1; + $$.offset = $1->value + $2; + } + +vlgen: + lgen +| fgen +| mreg +| fcreg +| LHI + { + $$ = nullgen; + $$.type = D_HI; + } +| LLO + { + $$ = nullgen; + $$.type = D_LO; + } + +vgen: + gen +| fgen +| mreg +| fcreg +| LHI + { + $$ = nullgen; + $$.type = D_HI; + } +| LLO + { + $$ = nullgen; + $$.type = D_LO; + } + +lgen: + gen +| ximm + +fgen: + freg + +mreg: + LMREG + { + $$ = nullgen; + $$.type = D_MREG; + $$.reg = $1; + } +| LM '(' con ')' + { + $$ = nullgen; + $$.type = D_MREG; + $$.reg = $3; + } + +fcreg: + LFCREG + { + $$ = nullgen; + $$.type = D_FCREG; + $$.reg = $1; + } +| LFCR '(' con ')' + { + $$ = nullgen; + $$.type = D_FCREG; + $$.reg = $3; + } + +freg: + LFREG + { + $$ = nullgen; + $$.type = D_FREG; + $$.reg = $1; + } +| LF '(' con ')' + { + $$ = nullgen; + $$.type = D_FREG; + $$.reg = $3; + } + +ximm: '$' con + { + $$ = nullgen; + if(isvconst($2)) { + $$.type = D_VCONST; + $$.vval = $2; + } else { + $$.type = D_CONST; + $$.offset = $2; + } + } +| '$' oreg + { + $$ = $2; + $$.type = D_CONST; + } +| '$' '*' '$' oreg + { + $$ = $4; + $$.type = D_OCONST; + } +| '$' LSCONST + { + $$ = nullgen; + $$.type = D_SCONST; + memcpy($$.sval, $2, sizeof($$.sval)); + } +| '$' LFCONST + { + $$ = nullgen; + $$.type = D_FCONST; + $$.dval = $2; + } +| '$' '-' LFCONST + { + $$ = nullgen; + $$.type = D_FCONST; + $$.dval = -$3; + } + +nireg: + ireg +| con ireg + { + if($1 != 0) + yyerror("offset must be zero"); + $$ = $2; + } +| name + { + $$ = $1; + if($1.name != D_EXTERN && $1.name != D_STATIC) { + } + } + +ireg: + '(' sreg ')' + { + $$ = nullgen; + $$.type = D_OREG; + $$.reg = $2; + $$.offset = 0; + } + +gen: + reg +| con + { + $$ = nullgen; + $$.type = D_OREG; + $$.offset = $1; + } +| oreg + +oreg: + name +| name '(' sreg ')' + { + $$ = $1; + $$.type = D_OREG; + $$.reg = $3; + } +| '(' sreg ')' + { + $$ = nullgen; + $$.type = D_OREG; + $$.reg = $2; + $$.offset = 0; + } +| con '(' sreg ')' + { + $$ = nullgen; + $$.type = D_OREG; + $$.reg = $3; + $$.offset = $1; + } + +imr: + reg +| imm + +imm: '$' con + { + $$ = nullgen; + if(isvconst($2)) { + fprint(2, "imm: vconst: 0x%llx\n", $2); + $$.type = D_VCONST; + $$.vval = $2; + } else { + fprint(2, "imm: const: 0x%lx\n", (long)$2); + $$.type = D_CONST; + $$.offset = $2; + } + } + +reg: + sreg + { + $$ = nullgen; + $$.type = D_REG; + $$.reg = $1; + } + +sreg: + LREG +| LR '(' con ')' + { + if($$ < 0 || $$ >= NREG) + print("register value out of range\n"); + $$ = $3; + } + +name: + con '(' pointer ')' + { + $$ = nullgen; + $$.type = D_OREG; + $$.name = $3; + $$.sym = S; + $$.offset = $1; + } +| LNAME offset '(' pointer ')' + { + $$ = nullgen; + $$.type = D_OREG; + $$.name = $4; + $$.sym = $1; + $$.offset = $2; + } +| LNAME '<' '>' offset '(' LSB ')' + { + $$ = nullgen; + $$.type = D_OREG; + $$.name = D_STATIC; + $$.sym = $1; + $$.offset = $4; + } + +offset: + { + $$ = 0; + } +| '+' con + { + $$ = $2; + } +| '-' con + { + $$ = -$2; + } + +pointer: + LSB +| LSP +| LFP + +con: + LCONST +| LVAR + { + $$ = $1->value; + } +| '-' con + { + $$ = -$2; + } +| '+' con + { + $$ = $2; + } +| '~' con + { + $$ = ~$2; + } +| '(' expr ')' + { + $$ = $2; + } + +expr: + con +| expr '+' expr + { + $$ = $1 + $3; + } +| expr '-' expr + { + $$ = $1 - $3; + } +| expr '*' expr + { + $$ = $1 * $3; + } +| expr '/' expr + { + $$ = $1 / $3; + } +| expr '%' expr + { + $$ = $1 % $3; + } +| expr '<' '<' expr + { + $$ = $1 << $4; + } +| expr '>' '>' expr + { + $$ = $1 >> $4; + } +| expr '&' expr + { + $$ = $1 & $3; + } +| expr '^' expr + { + $$ = $1 ^ $3; + } +| expr '|' expr + { + $$ = $1 | $3; + } +%% + +int +isvconst(vlong con) +{ + vlong v; + + v = con >> 32; + if(v != 0 && v != -1) + return 1; + return 0; +} diff --git a/sys/src/cmd/4a/l.s b/sys/src/cmd/4a/l.s new file mode 100644 index 0000000..1fb4e40 --- /dev/null +++ b/sys/src/cmd/4a/l.s @@ -0,0 +1,703 @@ +/* + * Memory and machine-specific definitions. Used in C and assembler. + */ + +/* + * Sizes + */ + +#define BI2BY 8 /* bits per byte */ +#define BI2WD 32 /* bits per word */ +#define BY2WD 4 /* bytes per word */ +#define BY2PG 4096 /* bytes per page */ +#define WD2PG (BY2PG/BY2WD) /* words per page */ +#define PGSHIFT 12 /* log(BY2PG) */ + +#define MAXMACH 4 /* max # cpus system can run */ + +/* + * Time + */ +#define MS2HZ 50 /* millisec per clock tick */ +#define TK2SEC(t) ((t)/20) /* ticks to seconds */ +#define TK2MS(t) ((t)*MS2HZ) /* ticks to milliseconds */ +#define MS2TK(t) ((t)/MS2HZ) /* milliseconds to ticks */ + +/* + * CP0 registers + */ + +#define INDEX 0 +#define RANDOM 1 +#define TLBPHYS 2 +#define CONTEXT 4 +#define BADVADDR 8 +#define TLBVIRT 10 +#define STATUS 12 +#define CAUSE 13 +#define EPC 14 +#define PRID 15 + +/* + * M(STATUS) bits + */ +#define IEC 0x00000001 +#define KUC 0x00000002 +#define IEP 0x00000004 +#define KUP 0x00000008 +#define INTMASK 0x0000ff00 +#define SW0 0x00000100 +#define SW1 0x00000200 +#define INTR0 0x00000400 +#define INTR1 0x00000800 +#define INTR2 0x00001000 +#define INTR3 0x00002000 +#define INTR4 0x00004000 +#define INTR5 0x00008000 +#define ISC 0x00010000 +#define SWC 0x00020000 +#define CU1 0x20000000 + +/* + * Traps + */ + +#define UTLBMISS (KSEG0+0x00) +#define EXCEPTION (KSEG0+0x80) + +/* + * Magic registers + */ + +#define MACH 25 /* R25 is m-> */ +#define USER 24 /* R24 is u-> */ +#define MPID 0xBF000000 /* long; low 3 bits identify mp bus slot */ +#define WBFLUSH 0xBC000000 /* D-CACHE data; used for write buffer flush */ + +/* + * Fundamental addresses + */ + +#define MACHADDR 0x80014000 +#define USERADDR 0xC0000000 +#define UREGADDR (USERADDR+BY2PG-4-0xA0) +/* + * MMU + */ + +#define KUSEG 0x00000000 +#define KSEG0 0x80000000 +#define KSEG1 0xA0000000 +#define KSEG2 0xC0000000 +#define KSEGM 0xE0000000 /* mask to check which seg */ + +#define PTEGLOBL (1<<8) +#define PTEVALID (1<<9) +#define PTEWRITE (1<<10) +#define PTEPID(n) ((n)<<6) + +#define NTLBPID 64 /* number of pids */ +#define NTLB 64 /* number of entries */ +#define TLBROFF 8 /* offset of first randomly indexed entry */ + +/* + * Address spaces + */ + +#define UZERO KUSEG /* base of user address space */ +#define UTZERO (UZERO+BY2PG) /* first address in user text */ +#define USTKTOP KZERO /* byte just beyond user stack */ +#define TSTKTOP (USERADDR+100*BY2PG) /* top of temporary stack */ +#define KZERO KSEG0 /* base of kernel address space */ +#define KTZERO (KSEG0+0x20000) /* first address in kernel text */ +#define USTACKSIZE (4*1024*1024) /* size of user stack */ +/* + * Exception codes + */ +#define CINT 0 /* external interrupt */ +#define CTLBM 1 /* TLB modification */ +#define CTLBL 2 /* TLB miss (load or fetch) */ +#define CTLBS 3 /* TLB miss (store) */ +#define CADREL 4 /* address error (load or fetch) */ +#define CADRES 5 /* address error (store) */ +#define CBUSI 6 /* bus error (fetch) */ +#define CBUSD 7 /* bus error (data load or store) */ +#define CSYS 8 /* system call */ +#define CBRK 9 /* breakpoint */ +#define CRES 10 /* reserved instruction */ +#define CCPU 11 /* coprocessor unusable */ +#define COVF 12 /* arithmetic overflow */ +#define CUNK13 13 /* undefined 13 */ +#define CUNK14 14 /* undefined 14 */ +#define CUNK15 15 /* undefined 15 */ + +#define NSEG 5 + +#define SP R29 + +#define PROM (KSEG1+0x1FC00000) +#define NOOP NOR R0,R0 +#define WAIT NOOP; NOOP + +/* + * Boot first processor + * - why is the processor number loaded from R0 ????? + */ +TEXT start(SB), $-4 + + MOVW $setR30(SB), R30 + MOVW $(CU1|INTR5|INTR4|INTR3|INTR2|INTR1|SW1|SW0), R1 + MOVW R1, M(STATUS) + WAIT + + MOVW $(0x1C<<7), R1 + MOVW R1, FCR31 /* permit only inexact and underflow */ + NOOP + MOVD $0.5, F26 + SUBD F26, F26, F24 + ADDD F26, F26, F28 + ADDD F28, F28, F30 + + MOVD F24, F0 + MOVD F24, F2 + MOVD F24, F4 + MOVD F24, F6 + MOVD F24, F8 + MOVD F24, F10 + MOVD F24, F12 + MOVD F24, F14 + MOVD F24, F16 + MOVD F24, F18 + MOVD F24, F20 + MOVD F24, F22 + + MOVW $MACHADDR, R(MACH) + ADDU $(BY2PG-4), R(MACH), SP + MOVW $0, R(USER) + MOVW R0, 0(R(MACH)) + + MOVW $edata(SB), R1 + MOVW $end(SB), R2 + +clrbss: + MOVB $0, (R1) + ADDU $1, R1 + BNE R1, R2, clrbss + + MOVW R4, _argc(SB) + MOVW R5, _argv(SB) + MOVW R6, _env(SB) + JAL main(SB) + JMP (R0) + +/* + * Take first processor into user mode + * - argument is stack pointer to user + */ + +TEXT touser(SB), $-4 + + MOVW M(STATUS), R1 + OR $(KUP|IEP), R1 + MOVW R1, M(STATUS) + NOOP + MOVW 0(FP), SP + MOVW $(UTZERO+32), R26 /* header appears in text */ + RFE (R26) + +/* + * Bring subsequent processors on line + */ +TEXT newstart(SB), $0 + + MOVW $setR30(SB), R30 + MOVW $(INTR5|INTR4|INTR3|INTR2|INTR1|SW1|SW0), R1 + MOVW R1, M(STATUS) + NOOP + MOVW $MACHADDR, R(MACH) + MOVB (MPID+3), R1 + AND $7, R1 + SLL $PGSHIFT, R1, R2 + ADDU R2, R(MACH) + ADDU $(BY2PG-4), R(MACH), SP + MOVW $0, R(USER) + MOVW R1, 0(R(MACH)) + JAL online(SB) + JMP (R0) + +TEXT firmware(SB), $0 + + MOVW $(PROM+0x18), R1 /**/ +/* MOVW $(PROM+0x00), R1 /**/ + JMP (R1) + +TEXT splhi(SB), $0 + + MOVW M(STATUS), R1 + AND $~IEC, R1, R2 + MOVW R2, M(STATUS) + NOOP + RET + +TEXT spllo(SB), $0 + + MOVW M(STATUS), R1 + OR $IEC, R1, R2 + MOVW R2, M(STATUS) + NOOP + RET + +TEXT splx(SB), $0 + + MOVW 0(FP), R1 + MOVW M(STATUS), R2 + AND $IEC, R1 + AND $~IEC, R2 + OR R2, R1 + MOVW R1, M(STATUS) + NOOP + RET + +TEXT wbflush(SB), $-4 + + MOVW $WBFLUSH, R1 + MOVW 0(R1), R1 + RET + +TEXT setlabel(SB), $0 + + MOVW 0(FP), R2 + MOVW $0, R1 + MOVW R31, 0(R2) + MOVW R29, 4(R2) + RET + +TEXT gotolabel(SB), $0 + + MOVW 0(FP), R2 + MOVW $1, R1 + MOVW 0(R2), R31 + MOVW 4(R2), R29 + RET + +TEXT gotopc(SB), $8 + + MOVW 0(FP), R7 /* save arguments for later */ + MOVW _argc(SB), R4 + MOVW _argv(SB), R5 + MOVW _env(SB), R6 + MOVW R0, 4(SP) + MOVW $(64*1024), R1 + MOVW R1, 8(SP) + JAL icflush(SB) + JMP (R7) + +TEXT puttlb(SB), $4 + + JAL splhi(SB) + MOVW 0(FP), R2 + MOVW 4(FP), R3 + MOVW R1, 4(SP) + MOVW R2, M(TLBVIRT) + MOVW R3, M(TLBPHYS) + NOOP + TLBP + NOOP + MOVW M(INDEX), R4 + BGEZ R4, index + TLBWR + NOOP + JAL splx(SB) + RET +index: + TLBWI + NOOP + JAL splx(SB) + RET + +TEXT puttlbx(SB), $0 + + MOVW 0(FP), R4 + MOVW 4(FP), R2 + MOVW 8(FP), R3 + SLL $8, R4 + MOVW R2, M(TLBVIRT) + MOVW R3, M(TLBPHYS) + MOVW R4, M(INDEX) + NOOP + TLBWI + NOOP + RET + +TEXT tlbp(SB), $0 + TLBP + NOOP + MOVW M(INDEX), R1 + RET + +TEXT tlbvirt(SB), $0 + TLBP + NOOP + MOVW M(TLBVIRT), R1 + RET + + +TEXT gettlb(SB), $0 + + MOVW 0(FP), R3 + MOVW 4(FP), R4 + SLL $8, R3 + MOVW R3, M(INDEX) + NOOP + TLBR + NOOP + MOVW M(TLBVIRT), R1 + MOVW M(TLBPHYS), R2 + NOOP + MOVW R1, 0(R4) + MOVW R2, 4(R4) + RET + +TEXT gettlbvirt(SB), $0 + + MOVW 0(FP), R3 + SLL $8, R3 + MOVW R3, M(INDEX) + NOOP + TLBR + NOOP + MOVW M(TLBVIRT), R1 + NOOP + RET + +TEXT vector80(SB), $-4 + + MOVW $exception(SB), R26 + JMP (R26) + +TEXT exception(SB), $-4 + + MOVW M(STATUS), R26 + AND $KUP, R26 + BEQ R26, waskernel + +wasuser: + MOVW SP, R26 + /* + * set kernel sp: ureg - ureg* - pc + * done in 2 steps because R30 is not set + * and the loader will make a literal + */ + MOVW $((UREGADDR-2*BY2WD) & 0xffff0000), SP + OR $((UREGADDR-2*BY2WD) & 0xffff), SP + MOVW R26, 0x10(SP) /* user SP */ + MOVW R31, 0x28(SP) + MOVW R30, 0x2C(SP) + MOVW M(CAUSE), R26 + MOVW R(MACH), 0x3C(SP) + MOVW R(USER), 0x40(SP) + AND $(0xF<<2), R26 + SUB $(CSYS<<2), R26 + + JAL saveregs(SB) + + MOVW $setR30(SB), R30 + SUBU $(UREGADDR-2*BY2WD-USERADDR), SP, R(USER) + MOVW $MPID, R1 + MOVB 3(R1), R1 + MOVW $MACHADDR, R(MACH) /* locn of mach 0 */ + AND $7, R1 + SLL $PGSHIFT, R1 + ADDU R1, R(MACH) /* add offset for mach # */ + + BNE R26, notsys + + JAL syscall(SB) + + MOVW 0x28(SP), R31 + MOVW 0x08(SP), R26 + MOVW 0x2C(SP), R30 + MOVW R26, M(STATUS) + NOOP + MOVW 0x0C(SP), R26 /* old pc */ + MOVW 0x10(SP), SP + RFE (R26) + +notsys: + JAL trap(SB) + +restore: + JAL restregs(SB) + MOVW 0x28(SP), R31 + MOVW 0x2C(SP), R30 + MOVW 0x3C(SP), R(MACH) + MOVW 0x40(SP), R(USER) + MOVW 0x10(SP), SP + RFE (R26) + +waskernel: + MOVW $1, R26 /* not sys call */ + MOVW SP, -0x90(SP) /* drop this if possible */ + SUB $0xA0, SP + MOVW R31, 0x28(SP) + JAL saveregs(SB) + JAL trap(SB) + JAL restregs(SB) + MOVW 0x28(SP), R31 + ADD $0xA0, SP + RFE (R26) + +TEXT saveregs(SB), $-4 + MOVW R1, 0x9C(SP) + MOVW R2, 0x98(SP) + ADDU $8, SP, R1 + MOVW R1, 0x04(SP) /* arg to base of regs */ + MOVW M(STATUS), R1 + MOVW M(EPC), R2 + MOVW R1, 0x08(SP) + MOVW R2, 0x0C(SP) + + BEQ R26, return /* sys call, don't save */ + + MOVW M(CAUSE), R1 + MOVW M(BADVADDR), R2 + MOVW R1, 0x14(SP) + MOVW M(TLBVIRT), R1 + MOVW R2, 0x18(SP) + MOVW R1, 0x1C(SP) + MOVW HI, R1 + MOVW LO, R2 + MOVW R1, 0x20(SP) + MOVW R2, 0x24(SP) + /* LINK,SB,SP missing */ + MOVW R28, 0x30(SP) + /* R27, R26 not saved */ + /* R25, R24 missing */ + MOVW R23, 0x44(SP) + MOVW R22, 0x48(SP) + MOVW R21, 0x4C(SP) + MOVW R20, 0x50(SP) + MOVW R19, 0x54(SP) + MOVW R18, 0x58(SP) + MOVW R17, 0x5C(SP) + MOVW R16, 0x60(SP) + MOVW R15, 0x64(SP) + MOVW R14, 0x68(SP) + MOVW R13, 0x6C(SP) + MOVW R12, 0x70(SP) + MOVW R11, 0x74(SP) + MOVW R10, 0x78(SP) + MOVW R9, 0x7C(SP) + MOVW R8, 0x80(SP) + MOVW R7, 0x84(SP) + MOVW R6, 0x88(SP) + MOVW R5, 0x8C(SP) + MOVW R4, 0x90(SP) + MOVW R3, 0x94(SP) +return: + RET + +TEXT restregs(SB), $-4 + /* LINK,SB,SP missing */ + MOVW 0x30(SP), R28 + /* R27, R26 not saved */ + /* R25, R24 missing */ + MOVW 0x44(SP), R23 + MOVW 0x48(SP), R22 + MOVW 0x4C(SP), R21 + MOVW 0x50(SP), R20 + MOVW 0x54(SP), R19 + MOVW 0x58(SP), R18 + MOVW 0x5C(SP), R17 + MOVW 0x60(SP), R16 + MOVW 0x64(SP), R15 + MOVW 0x68(SP), R14 + MOVW 0x6C(SP), R13 + MOVW 0x70(SP), R12 + MOVW 0x74(SP), R11 + MOVW 0x78(SP), R10 + MOVW 0x7C(SP), R9 + MOVW 0x80(SP), R8 + MOVW 0x84(SP), R7 + MOVW 0x88(SP), R6 + MOVW 0x8C(SP), R5 + MOVW 0x90(SP), R4 + MOVW 0x94(SP), R3 + MOVW 0x24(SP), R2 + MOVW 0x20(SP), R1 + MOVW R2, LO + MOVW R1, HI + MOVW 0x08(SP), R1 + MOVW 0x98(SP), R2 + MOVW R1, M(STATUS) + NOOP + MOVW 0x9C(SP), R1 + MOVW 0x0C(SP), R26 /* old pc */ + RET + +TEXT rfnote(SB), $0 + MOVW 0(FP), R26 /* 1st arg is &uregpointer */ + SUBU $(BY2WD), R26, SP /* pc hole */ + BNE R26, restore + + +TEXT clrfpintr(SB), $0 + MOVW FCR31, R1 + MOVW R1, R2 + AND $~(0x3F<<12), R2 + MOVW R2, FCR31 + RET + +TEXT savefpregs(SB), $0 + MOVW M(STATUS), R3 + MOVW 0(FP), R1 + MOVW FCR31, R2 + + MOVD F0, 0x00(R1) + MOVD F2, 0x08(R1) + MOVD F4, 0x10(R1) + MOVD F6, 0x18(R1) + MOVD F8, 0x20(R1) + MOVD F10, 0x28(R1) + MOVD F12, 0x30(R1) + MOVD F14, 0x38(R1) + MOVD F16, 0x40(R1) + MOVD F18, 0x48(R1) + MOVD F20, 0x50(R1) + MOVD F22, 0x58(R1) + MOVD F24, 0x60(R1) + MOVD F26, 0x68(R1) + MOVD F28, 0x70(R1) + MOVD F30, 0x78(R1) + + MOVW R2, 0x80(R1) + AND $~CU1, R3 + MOVW R3, M(STATUS) + RET + +TEXT restfpregs(SB), $0 + + MOVW M(STATUS), R3 + MOVW 0(FP), R1 + OR $CU1, R3 + MOVW R3, M(STATUS) + MOVW 0x80(R1), R2 + + MOVD 0x00(R1), F0 + MOVD 0x08(R1), F2 + MOVD 0x10(R1), F4 + MOVD 0x18(R1), F6 + MOVD 0x20(R1), F8 + MOVD 0x28(R1), F10 + MOVD 0x30(R1), F12 + MOVD 0x38(R1), F14 + MOVD 0x40(R1), F16 + MOVD 0x48(R1), F18 + MOVD 0x50(R1), F20 + MOVD 0x58(R1), F22 + MOVD 0x60(R1), F24 + MOVD 0x68(R1), F26 + MOVD 0x70(R1), F28 + MOVD 0x78(R1), F30 + + MOVW R2, FCR31 + AND $~CU1, R3 + MOVW R3, M(STATUS) + RET + +/* + * we avoid using R4, R5, R6, and R7 so gotopc can call us without saving them + */ +TEXT icflush(SB), $-4 /* icflush(physaddr, nbytes) */ + + MOVW M(STATUS), R10 + MOVW 0(FP), R8 + MOVW 4(FP), R9 + MOVW $KSEG0, R3 + OR R3, R8 + MOVW $0, M(STATUS) + MOVW $WBFLUSH, R1 /* wbflush */ + MOVW 0(R1), R1 + NOOP + MOVW $KSEG1, R3 + MOVW $icflush0(SB), R2 /* make sure PC is in uncached address space */ + MOVW $(SWC|ISC), R1 + OR R3, R2 + JMP (R2) + +TEXT icflush0(SB), $-4 + + MOVW R1, M(STATUS) /* swap and isolate cache, splhi */ + MOVW $icflush1(SB), R2 + JMP (R2) + +TEXT icflush1(SB), $-4 + +_icflush1: + MOVBU R0, 0x00(R8) + MOVBU R0, 0x04(R8) + MOVBU R0, 0x08(R8) + MOVBU R0, 0x0C(R8) + MOVBU R0, 0x10(R8) + MOVBU R0, 0x14(R8) + MOVBU R0, 0x18(R8) + MOVBU R0, 0x1C(R8) + MOVBU R0, 0x20(R8) + MOVBU R0, 0x24(R8) + MOVBU R0, 0x28(R8) + MOVBU R0, 0x2C(R8) + MOVBU R0, 0x30(R8) + MOVBU R0, 0x34(R8) + MOVBU R0, 0x38(R8) + MOVBU R0, 0x3C(R8) + SUB $0x40, R9 + ADD $0x40, R8 + BGTZ R9, _icflush1 + MOVW $icflush2(SB), R2 /* make sure PC is in uncached address space */ + OR R3, R2 + JMP (R2) + +TEXT icflush2(SB), $-4 + + MOVW $0, M(STATUS) /* swap back caches, de-isolate them, and stay splhi */ + NOOP /* +++ */ + MOVW R10, M(STATUS) + RET + +TEXT dcflush(SB), $-4 /* dcflush(physaddr, nbytes) */ + + MOVW M(STATUS), R6 + MOVW 0(FP), R4 + MOVW 4(FP), R5 + MOVW $KSEG0, R3 + OR R3, R4 + MOVW $0, M(STATUS) + MOVW $WBFLUSH, R1 + MOVW 0(R1), R1 + NOOP + MOVW $ISC, R1 + MOVW R1, M(STATUS) +_dcflush0: + MOVBU R0, 0x00(R4) + MOVBU R0, 0x04(R4) + MOVBU R0, 0x08(R4) + MOVBU R0, 0x0C(R4) + MOVBU R0, 0x10(R4) + MOVBU R0, 0x14(R4) + MOVBU R0, 0x18(R4) + MOVBU R0, 0x1C(R4) + MOVBU R0, 0x20(R4) + MOVBU R0, 0x24(R4) + MOVBU R0, 0x28(R4) + MOVBU R0, 0x2C(R4) + MOVBU R0, 0x30(R4) + MOVBU R0, 0x34(R4) + MOVBU R0, 0x38(R4) + MOVBU R0, 0x3C(R4) + SUB $0x40, R5 + ADD $0x40, R4 + BGTZ R5, _dcflush0 + MOVW $0, M(STATUS) + NOOP /* +++ */ + MOVW R6, M(STATUS) + RET diff --git a/sys/src/cmd/4a/lex.c b/sys/src/cmd/4a/lex.c new file mode 100644 index 0000000..309d9a5 --- /dev/null +++ b/sys/src/cmd/4a/lex.c @@ -0,0 +1,704 @@ +#include +#define EXTERN +#include "a.h" +#include "y.tab.h" + +void +main(int argc, char *argv[]) +{ + char *p; + int nout, nproc, status, i, c; + + thechar = '4'; + thestring = "mips2"; + memset(debug, 0, sizeof(debug)); + cinit(); + outfile = 0; + include[ninclude++] = "."; + ARGBEGIN { + default: + c = ARGC(); + if(c >= 0 || c < sizeof(debug)) + debug[c] = 1; + break; + + case 'o': + outfile = ARGF(); + break; + + case 'D': + p = ARGF(); + if(p) + Dlist[nDlist++] = p; + break; + + case 'I': + p = ARGF(); + setinclude(p); + break; + } ARGEND + if(*argv == 0) { + print("usage: %ca [-options] file.s\n", thechar); + errorexit(); + } + if(argc > 1 && systemtype(Windows)){ + print("can't assemble multiple files on windows\n"); + errorexit(); + } + if(argc > 1 && !systemtype(Windows)) { + nproc = 1; + if(p = getenv("NPROC")) + nproc = atol(p); /* */ + c = 0; + nout = 0; + for(;;) { + while(nout < nproc && argc > 0) { + i = myfork(); + if(i < 0) { + i = mywait(&status); + if(i < 0) + errorexit(); + if(status) + c++; + nout--; + continue; + } + if(i == 0) { + print("%s:\n", *argv); + if(assemble(*argv)) + errorexit(); + exits(0); + } + nout++; + argc--; + argv++; + } + i = mywait(&status); + if(i < 0) { + if(c) + errorexit(); + exits(0); + } + if(status) + c++; + nout--; + } + } + if(assemble(argv[0])) + errorexit(); + exits(0); +} + +int +assemble(char *file) +{ + char ofile[100], incfile[20], *p; + int i, of; + + strcpy(ofile, file); + p = utfrrune(ofile, pathchar()); + if(p) { + include[0] = ofile; + *p++ = 0; + } else + p = ofile; + if(outfile == 0) { + outfile = p; + if(outfile){ + p = utfrrune(outfile, '.'); + if(p) + if(p[1] == 's' && p[2] == 0) + p[0] = 0; + p = utfrune(outfile, 0); + p[0] = '.'; + p[1] = thechar; + p[2] = 0; + } else + outfile = "/dev/null"; + } + p = getenv("INCLUDE"); + if(p) { + setinclude(p); + } else { + if(systemtype(Plan9)) { + sprint(incfile,"/%s/include", thestring); + setinclude(strdup(incfile)); + } + } + + of = mycreat(outfile, 0664); + if(of < 0) { + yyerror("%ca: cannot create %s", thechar, outfile); + errorexit(); + } + Binit(&obuf, of, OWRITE); + + pass = 1; + pinit(file); + for(i=0; itype = itab[i].type; + s->value = itab[i].value; + } + + pathname = alloc(100); + if(mygetwd(pathname, 99) == 0) { + pathname = allocn(pathname, 100, 900); + if(mygetwd(pathname, 999) == 0) + strcpy(pathname, "/???"); + } +} + +void +syminit(Sym *s) +{ + + s->type = LNAME; + s->value = 0; +} + +int +isreg(Gen *g) +{ + + USED(g); + return 1; +} + +void +cclean(void) +{ + + outcode(AEND, &nullgen, NREG, &nullgen); + Bflush(&obuf); +} + +void +zname(char *n, int t, int s) +{ + + Bputc(&obuf, ANAME); + Bputc(&obuf, t); /* type */ + Bputc(&obuf, s); /* sym */ + while(*n) { + Bputc(&obuf, *n); + n++; + } + Bputc(&obuf, 0); +} + +void +zaddr(Gen *a, int s) +{ + long l; + int i; + char *n; + Ieee e; + vlong v; + + Bputc(&obuf, a->type); + Bputc(&obuf, a->reg); + Bputc(&obuf, s); + Bputc(&obuf, a->name); + switch(a->type) { + default: + print("unknown type %d\n", a->type); + exits("arg"); + + case D_NONE: + case D_REG: + case D_FREG: + case D_MREG: + case D_FCREG: + case D_LO: + case D_HI: + break; + + case D_OREG: + case D_CONST: + case D_OCONST: + case D_BRANCH: + l = a->offset; + Bputc(&obuf, l); + Bputc(&obuf, l>>8); + Bputc(&obuf, l>>16); + Bputc(&obuf, l>>24); + break; + + case D_SCONST: + n = a->sval; + for(i=0; idval); + Bputc(&obuf, e.l); + Bputc(&obuf, e.l>>8); + Bputc(&obuf, e.l>>16); + Bputc(&obuf, e.l>>24); + Bputc(&obuf, e.h); + Bputc(&obuf, e.h>>8); + Bputc(&obuf, e.h>>16); + Bputc(&obuf, e.h>>24); + break; + + case D_VCONST: + v = a->vval; + Bputc(&obuf, v); + Bputc(&obuf, v>>8); + Bputc(&obuf, v>>16); + Bputc(&obuf, v>>24); + Bputc(&obuf, v>>32); + Bputc(&obuf, v>>40); + Bputc(&obuf, v>>48); + Bputc(&obuf, v>>56); + break; + } +} + +void +outcode(int a, Gen *g1, int reg, Gen *g2) +{ + int sf, st, t; + Sym *s; + + if(pass == 1) + goto out; +jackpot: + sf = 0; + s = g1->sym; + while(s != S) { + sf = s->sym; + if(sf < 0 || sf >= NSYM) + sf = 0; + t = g1->name; + if(h[sf].type == t) + if(h[sf].sym == s) + break; + zname(s->name, t, sym); + s->sym = sym; + h[sym].sym = s; + h[sym].type = t; + sf = sym; + sym++; + if(sym >= NSYM) + sym = 1; + break; + } + st = 0; + s = g2->sym; + while(s != S) { + st = s->sym; + if(st < 0 || st >= NSYM) + st = 0; + t = g2->name; + if(h[st].type == t) + if(h[st].sym == s) + break; + zname(s->name, t, sym); + s->sym = sym; + h[sym].sym = s; + h[sym].type = t; + st = sym; + sym++; + if(sym >= NSYM) + sym = 1; + if(st == sf) + goto jackpot; + break; + } + Bputc(&obuf, a); + Bputc(&obuf, reg|nosched); + Bputc(&obuf, lineno); + Bputc(&obuf, lineno>>8); + Bputc(&obuf, lineno>>16); + Bputc(&obuf, lineno>>24); + zaddr(g1, sf); + zaddr(g2, st); + +out: + if(a != AGLOBL && a != ADATA) + pc++; +} + +void +outhist(void) +{ + Gen g; + Hist *h; + char *p, *q, *op, c; + int n; + + g = nullgen; + c = pathchar(); + for(h = hist; h != H; h = h->link) { + p = h->name; + op = 0; + if(p && p[0] != c && h->offset == 0 && pathname){ + /* on windows skip drive specifier in pathname */ + if(systemtype(Windows) && pathname[2] == c) { + op = p; + p = pathname+2; + *p = '/'; + } else if(pathname[0] == c){ + op = p; + p = pathname; + } + } + while(p) { + q = strchr(p, c); + if(q) { + n = q-p; + if(n == 0) + n = 1; /* leading "/" */ + q++; + } else { + n = strlen(p); + q = 0; + } + if(n) { + Bputc(&obuf, ANAME); + Bputc(&obuf, D_FILE); /* type */ + Bputc(&obuf, 1); /* sym */ + Bputc(&obuf, '<'); + Bwrite(&obuf, p, n); + Bputc(&obuf, 0); + } + p = q; + if(p == 0 && op) { + p = op; + op = 0; + } + } + g.offset = h->offset; + + Bputc(&obuf, AHISTORY); + Bputc(&obuf, 0); + Bputc(&obuf, h->line); + Bputc(&obuf, h->line>>8); + Bputc(&obuf, h->line>>16); + Bputc(&obuf, h->line>>24); + zaddr(&nullgen, 0); + zaddr(&g, 0); + } +} + +#include "lexbody" +#include "../cc/macbody" +#include "../cc/compat" diff --git a/sys/src/cmd/4a/mkfile b/sys/src/cmd/4a/mkfile new file mode 100644 index 0000000..2516a9b --- /dev/null +++ b/sys/src/cmd/4a/mkfile @@ -0,0 +1,26 @@ +b[i]) + return 1; + return 0; +} + +/* +int +beq(Bits a, Bits b) +{ + int i; + + for(i=0; iname; + if(strlen(str) + strlen(s) + 1 >= STRINGSZ) + break; + strcat(str, s); + bits.b[i/32] &= ~(1L << (i%32)); + } + strconv(str, fp); + return 0; +} diff --git a/sys/src/cmd/4c/cgen.c b/sys/src/cmd/4c/cgen.c new file mode 100644 index 0000000..ed74774 --- /dev/null +++ b/sys/src/cmd/4c/cgen.c @@ -0,0 +1,1157 @@ +#include "gc.h" + +void +cgen(Node *n, Node *nn) +{ + Node *l, *r; + Prog *p1; + Node nod, nod1, nod2, nod3, nod4; + int o; + long v, curs; + + if(debug['g']) { + prtree(nn, "cgen lhs"); + prtree(n, "cgen"); + } + if(n == Z || n->type == T) + return; + if(typesu[n->type->etype]) { + sugen(n, nn, n->type->width); + return; + } + l = n->left; + r = n->right; + o = n->op; + if(n->addable >= INDEXED) { + if(nn == Z) { + switch(o) { + default: + nullwarn(Z, Z); + break; + case OINDEX: + nullwarn(l, r); + break; + } + return; + } + gmove(n, nn); + return; + } + curs = cursafe; + + if(n->complex >= FNX) + if(l->complex >= FNX) + if(r != Z && r->complex >= FNX) + switch(o) { + default: + regret(&nod, r); + cgen(r, &nod); + + regsalloc(&nod1, r); + gopcode(OAS, &nod, Z, &nod1); + + regfree(&nod); + nod = *n; + nod.right = &nod1; + cgen(&nod, nn); + return; + + case OFUNC: + case OCOMMA: + case OANDAND: + case OOROR: + case OCOND: + case ODOT: + break; + } + + switch(o) { + default: + diag(n, "unknown op in cgen: %O", o); + break; + + case OAS: + if(l->op == OBIT) + goto bitas; + if(l->addable >= INDEXED && l->complex < FNX) { + if(nn != Z || r->addable < INDEXED) { + if(r->complex >= FNX && nn == Z) + regret(&nod, r); + else + regalloc(&nod, r, nn); + cgen(r, &nod); + gmove(&nod, l); + if(nn != Z) + gmove(&nod, nn); + regfree(&nod); + } else + gmove(r, l); + break; + } + if(l->complex >= r->complex) { + reglcgen(&nod1, l, Z); + if(r->addable >= INDEXED) { + gmove(r, &nod1); + if(nn != Z) + gmove(r, nn); + regfree(&nod1); + break; + } + regalloc(&nod, r, nn); + cgen(r, &nod); + } else { + regalloc(&nod, r, nn); + cgen(r, &nod); + reglcgen(&nod1, l, Z); + } + gmove(&nod, &nod1); + regfree(&nod); + regfree(&nod1); + break; + + bitas: + n = l->left; + regalloc(&nod, r, nn); + if(l->complex >= r->complex) { + reglcgen(&nod1, n, Z); + cgen(r, &nod); + } else { + cgen(r, &nod); + reglcgen(&nod1, n, Z); + } + regalloc(&nod2, n, Z); + gopcode(OAS, &nod1, Z, &nod2); + bitstore(l, &nod, &nod1, &nod2, nn); + break; + + case OBIT: + if(nn == Z) { + nullwarn(l, Z); + break; + } + bitload(n, &nod, Z, Z, nn); + gopcode(OAS, &nod, Z, nn); + regfree(&nod); + break; + + case OADD: + case OSUB: + case OAND: + case OOR: + case OXOR: + case OLSHR: + case OASHL: + case OASHR: + /* + * immediate operands + */ + if(nn != Z) + if(r->op == OCONST) + if(!typefd[n->type->etype]) { + cgen(l, nn); + if(r->vconst == 0) + if(o != OAND) + break; + if(nn != Z) + gopcode(o, r, Z, nn); + break; + } + + case OLMUL: + case OLDIV: + case OLMOD: + case OMUL: + case ODIV: + case OMOD: + if(nn == Z) { + nullwarn(l, r); + break; + } + if(o == OMUL || o == OLMUL) { + if(mulcon(n, nn)) + break; + } + if(l->complex >= r->complex) { + regalloc(&nod, l, nn); + cgen(l, &nod); + regalloc(&nod1, r, Z); + cgen(r, &nod1); + gopcode(o, &nod1, Z, &nod); + } else { + regalloc(&nod, r, nn); + cgen(r, &nod); + regalloc(&nod1, l, Z); + cgen(l, &nod1); + gopcode(o, &nod, &nod1, &nod); + } + gopcode(OAS, &nod, Z, nn); + regfree(&nod); + regfree(&nod1); + break; + + case OASLSHR: + case OASASHL: + case OASASHR: + case OASAND: + case OASADD: + case OASSUB: + case OASXOR: + case OASOR: + if(l->op == OBIT) + goto asbitop; + if(r->op == OCONST) + if(!typefd[n->type->etype]) { + if(l->addable < INDEXED) + reglcgen(&nod2, l, Z); + else + nod2 = *l; + + regalloc(&nod, n, nn); + gopcode(OAS, &nod2, Z, &nod); + gopcode(o, r, Z, &nod); + gopcode(OAS, &nod, Z, &nod2); + + regfree(&nod); + if(l->addable < INDEXED) + regfree(&nod2); + break; + } + + case OASLMUL: + case OASLDIV: + case OASLMOD: + case OASMUL: + case OASDIV: + case OASMOD: + if(l->op == OBIT) + goto asbitop; + if(l->complex >= r->complex) { + if(l->addable < INDEXED) + reglcgen(&nod2, l, Z); + else + nod2 = *l; + regalloc(&nod1, r, Z); + cgen(r, &nod1); + } else { + regalloc(&nod1, r, Z); + cgen(r, &nod1); + if(l->addable < INDEXED) + reglcgen(&nod2, l, Z); + else + nod2 = *l; + } + + regalloc(&nod, n, nn); + gmove(&nod2, &nod); + gopcode(o, &nod1, Z, &nod); + gmove(&nod, &nod2); + if(nn != Z) + gopcode(OAS, &nod, Z, nn); + regfree(&nod); + regfree(&nod1); + if(l->addable < INDEXED) + regfree(&nod2); + break; + + asbitop: + regalloc(&nod4, n, nn); + if(l->complex >= r->complex) { + bitload(l, &nod, &nod1, &nod2, &nod4); + regalloc(&nod3, r, Z); + cgen(r, &nod3); + } else { + regalloc(&nod3, r, Z); + cgen(r, &nod3); + bitload(l, &nod, &nod1, &nod2, &nod4); + } + gmove(&nod, &nod4); + gopcode(o, &nod3, Z, &nod4); + regfree(&nod3); + gmove(&nod4, &nod); + regfree(&nod4); + bitstore(l, &nod, &nod1, &nod2, nn); + break; + + case OADDR: + if(nn == Z) { + nullwarn(l, Z); + break; + } + lcgen(l, nn); + break; + + case OFUNC: + if(l->complex >= FNX) { + if(l->op != OIND) + diag(n, "bad function call"); + + regret(&nod, l->left); + cgen(l->left, &nod); + regsalloc(&nod1, l->left); + gopcode(OAS, &nod, Z, &nod1); + regfree(&nod); + + nod = *n; + nod.left = &nod2; + nod2 = *l; + nod2.left = &nod1; + nod2.complex = 1; + cgen(&nod, nn); + + return; + } + o = reg[REGARG]; + gargs(r, &nod, &nod1); + if(l->addable < INDEXED) { + reglcgen(&nod, l, Z); + gopcode(OFUNC, Z, Z, &nod); + regfree(&nod); + } else + gopcode(OFUNC, Z, Z, l); + if(REGARG) + if(o != reg[REGARG]) + reg[REGARG]--; + if(nn != Z) { + regret(&nod, n); + gopcode(OAS, &nod, Z, nn); + regfree(&nod); + } + break; + + case OIND: + if(nn == Z) { + nullwarn(l, Z); + break; + } + regialloc(&nod, n, nn); + r = l; + while(r->op == OADD) + r = r->right; + if(sconst(r)) { + v = r->vconst; + r->vconst = 0; + cgen(l, &nod); + nod.xoffset += v; + r->vconst = v; + } else + cgen(l, &nod); + regind(&nod, n); + gopcode(OAS, &nod, Z, nn); + regfree(&nod); + break; + + case OEQ: + case ONE: + case OLE: + case OLT: + case OGE: + case OGT: + case OLO: + case OLS: + case OHI: + case OHS: + if(nn == Z) { + nullwarn(l, r); + break; + } + boolgen(n, 1, nn); + break; + + case OANDAND: + case OOROR: + boolgen(n, 1, nn); + if(nn == Z) + patch(p, pc); + break; + + case ONOT: + if(nn == Z) { + nullwarn(l, Z); + break; + } + boolgen(n, 1, nn); + break; + + case OCOMMA: + cgen(l, Z); + cgen(r, nn); + break; + + case OCAST: + if(nn == Z) { + nullwarn(l, Z); + break; + } + /* + * convert from types l->n->nn + */ + if(nocast(l->type, n->type)) { + if(nocast(n->type, nn->type)) { + cgen(l, nn); + break; + } + } + regalloc(&nod, l, nn); + cgen(l, &nod); + regalloc(&nod1, n, &nod); + gopcode(OAS, &nod, Z, &nod1); + gopcode(OAS, &nod1, Z, nn); + regfree(&nod1); + regfree(&nod); + break; + + case ODOT: + sugen(l, nodrat, l->type->width); + if(nn != Z) { + warn(n, "non-interruptable temporary"); + nod = *nodrat; + if(!r || r->op != OCONST) { + diag(n, "DOT and no offset"); + break; + } + nod.xoffset += (long)r->vconst; + nod.type = n->type; + cgen(&nod, nn); + } + break; + + case OCOND: + bcgen(l, 1); + p1 = p; + cgen(r->left, nn); + gbranch(OGOTO); + patch(p1, pc); + p1 = p; + cgen(r->right, nn); + patch(p1, pc); + break; + + case OPOSTINC: + case OPOSTDEC: + v = 1; + if(l->type->etype == TIND) + v = l->type->link->width; + if(o == OPOSTDEC) + v = -v; + if(l->op == OBIT) + goto bitinc; + if(nn == Z) + goto pre; + + if(l->addable < INDEXED) + reglcgen(&nod2, l, Z); + else + nod2 = *l; + + regalloc(&nod, l, nn); + gopcode(OAS, &nod2, Z, &nod); + regalloc(&nod1, l, Z); + if(typefd[l->type->etype]) { + regalloc(&nod3, l, Z); + if(v < 0) { + gopcode(OAS, nodfconst(-v), Z, &nod3); + gopcode(OSUB, &nod3, &nod, &nod1); + } else { + gopcode(OAS, nodfconst(v), Z, &nod3); + gopcode(OADD, &nod3, &nod, &nod1); + } + regfree(&nod3); + } else + gopcode(OADD, nodconst(v), &nod, &nod1); + gopcode(OAS, &nod1, Z, &nod2); + + regfree(&nod); + regfree(&nod1); + if(l->addable < INDEXED) + regfree(&nod2); + break; + + case OPREINC: + case OPREDEC: + v = 1; + if(l->type->etype == TIND) + v = l->type->link->width; + if(o == OPREDEC) + v = -v; + if(l->op == OBIT) + goto bitinc; + + pre: + if(l->addable < INDEXED) + reglcgen(&nod2, l, Z); + else + nod2 = *l; + + regalloc(&nod, l, nn); + gopcode(OAS, &nod2, Z, &nod); + if(typefd[l->type->etype]) { + regalloc(&nod3, l, Z); + if(v < 0) { + gopcode(OAS, nodfconst(-v), Z, &nod3); + gopcode(OSUB, &nod3, Z, &nod); + } else { + gopcode(OAS, nodfconst(v), Z, &nod3); + gopcode(OADD, &nod3, Z, &nod); + } + regfree(&nod3); + } else + gopcode(OADD, nodconst(v), Z, &nod); + gopcode(OAS, &nod, Z, &nod2); + + regfree(&nod); + if(l->addable < INDEXED) + regfree(&nod2); + break; + + bitinc: + if(nn != Z && (o == OPOSTINC || o == OPOSTDEC)) { + bitload(l, &nod, &nod1, &nod2, Z); + gopcode(OAS, &nod, Z, nn); + gopcode(OADD, nodconst(v), Z, &nod); + bitstore(l, &nod, &nod1, &nod2, Z); + break; + } + bitload(l, &nod, &nod1, &nod2, nn); + gopcode(OADD, nodconst(v), Z, &nod); + bitstore(l, &nod, &nod1, &nod2, nn); + break; + } + cursafe = curs; +} + +void +reglcgen(Node *t, Node *n, Node *nn) +{ + Node *r; + long v; + + regialloc(t, n, nn); + if(n->op == OIND) { + r = n->left; + while(r->op == OADD) + r = r->right; + if(sconst(r)) { + v = r->vconst; + r->vconst = 0; + lcgen(n, t); + t->xoffset += v; + r->vconst = v; + regind(t, n); + return; + } + } + lcgen(n, t); + regind(t, n); +} + +void +lcgen(Node *n, Node *nn) +{ + Prog *p1; + Node nod; + + if(debug['g']) { + prtree(nn, "lcgen lhs"); + prtree(n, "lcgen"); + } + if(n == Z || n->type == T) + return; + if(nn == Z) { + nn = &nod; + regalloc(&nod, n, Z); + } + switch(n->op) { + default: + if(n->addable < INDEXED) { + diag(n, "unknown op in lcgen: %O", n->op); + break; + } + nod = *n; + nod.op = OADDR; + nod.left = n; + nod.right = Z; + nod.type = types[TIND]; + gopcode(OAS, &nod, Z, nn); + break; + + case OCOMMA: + cgen(n->left, n->left); + lcgen(n->right, nn); + break; + + case OIND: + cgen(n->left, nn); + break; + + case OCOND: + bcgen(n->left, 1); + p1 = p; + lcgen(n->right->left, nn); + gbranch(OGOTO); + patch(p1, pc); + p1 = p; + lcgen(n->right->right, nn); + patch(p1, pc); + break; + } +} + +void +bcgen(Node *n, int true) +{ + + if(n->type == T) + gbranch(OGOTO); + else + boolgen(n, true, Z); +} + +void +boolgen(Node *n, int true, Node *nn) +{ + int o; + Prog *p1, *p2; + Node *l, *r, nod, nod1; + long curs; + + if(debug['g']) { + prtree(nn, "boolgen lhs"); + prtree(n, "boolgen"); + } + curs = cursafe; + l = n->left; + r = n->right; + switch(n->op) { + + default: + regalloc(&nod, n, nn); + cgen(n, &nod); + if(nn == Z || typefd[n->type->etype]) { + o = ONE; + if(true) + o = comrel[relindex(o)]; + if(typefd[n->type->etype]) { + nodreg(&nod1, n, NREG+FREGZERO); + gopcode(o, &nod, &nod1, Z); + } else + gopcode(o, &nod, Z, Z); + regfree(&nod); + goto com; + } + if(true) + gopcode(OCOND, &nod, nodconst(0), &nod); + else + gopcode(OCOND, nodconst(1), &nod, &nod); + gopcode(OAS, &nod, Z, nn); + regfree(&nod); + break; + + case OCONST: + o = vconst(n); + if(!true) + o = !o; + gbranch(OGOTO); + if(o) { + p1 = p; + gbranch(OGOTO); + patch(p1, pc); + } + goto com; + + case OCOMMA: + cgen(l, Z); + boolgen(r, true, nn); + break; + + case ONOT: + boolgen(l, !true, nn); + break; + + case OCOND: + bcgen(l, 1); + p1 = p; + bcgen(r->left, true); + p2 = p; + gbranch(OGOTO); + patch(p1, pc); + p1 = p; + bcgen(r->right, !true); + patch(p2, pc); + p2 = p; + gbranch(OGOTO); + patch(p1, pc); + patch(p2, pc); + goto com; + + case OANDAND: + if(!true) + goto caseor; + + caseand: + bcgen(l, true); + p1 = p; + bcgen(r, !true); + p2 = p; + patch(p1, pc); + gbranch(OGOTO); + patch(p2, pc); + goto com; + + case OOROR: + if(!true) + goto caseand; + + caseor: + bcgen(l, !true); + p1 = p; + bcgen(r, !true); + p2 = p; + gbranch(OGOTO); + patch(p1, pc); + patch(p2, pc); + goto com; + + case OEQ: + case ONE: + case OLE: + case OLT: + case OGE: + case OGT: + case OHI: + case OHS: + case OLO: + case OLS: + o = n->op; + if(true) + o = comrel[relindex(o)]; + if(l->complex >= FNX && r->complex >= FNX) { + regret(&nod, r); + cgen(r, &nod); + regsalloc(&nod1, r); + gopcode(OAS, &nod, Z, &nod1); + regfree(&nod); + nod = *n; + nod.right = &nod1; + boolgen(&nod, true, nn); + break; + } + if(nn != Z && !typefd[l->type->etype]) { + if(l->complex >= r->complex) { + regalloc(&nod1, l, nn); + cgen(l, &nod1); + regalloc(&nod, r, Z); + cgen(r, &nod); + } else { + regalloc(&nod, r, nn); + cgen(r, &nod); + regalloc(&nod1, l, Z); + cgen(l, &nod1); + } + switch(o) { + case OEQ: + gopcode(OSUB, &nod1, &nod, &nod); + gopcode(OCOND, &nod, nodconst(0), &nod); + break; + case ONE: + gopcode(OSUB, &nod1, &nod, &nod); + gopcode(OCOND, nodconst(1), &nod, &nod); + break; + case OLE: + gopcode(OCOMMA, &nod1, &nod, &nod); + break; + case OGT: + gopcode(OCOMMA, &nod1, &nod, &nod); + gopcode(OXOR, nodconst(1), &nod, &nod); + break; + case OLT: + gopcode(OCOMMA, &nod, &nod1, &nod); + gopcode(OXOR, nodconst(1), &nod, &nod); + break; + case OGE: + gopcode(OCOMMA, &nod, &nod1, &nod); + break; + case OLS: + gopcode(OCOND, &nod1, &nod, &nod); + break; + case OHI: + gopcode(OCOND, &nod1, &nod, &nod); + gopcode(OXOR, nodconst(1), &nod, &nod); + break; + case OLO: + gopcode(OCOND, &nod, &nod1, &nod); + gopcode(OXOR, nodconst(1), &nod, &nod); + break; + case OHS: + gopcode(OCOND, &nod, &nod1, &nod); + break; + } + gopcode(OAS, &nod, Z, nn); + regfree(&nod); + regfree(&nod1); + break; + } + if(sconst(l)) { + switch(o) { + default: + if(l->vconst != 0) + break; + + case OGT: + case OHI: + case OLE: + case OLS: + regalloc(&nod, r, nn); + cgen(r, &nod); + gopcode(o, l, &nod, Z); + regfree(&nod); + goto com; + } + } + if(sconst(r)) { + switch(o) { + default: + if(r->vconst != 0) + break; + + case OGE: + case OHS: + case OLT: + case OLO: + regalloc(&nod, l, nn); + cgen(l, &nod); + gopcode(o, &nod, r, Z); + regfree(&nod); + goto com; + } + } + if(l->complex >= r->complex) { + regalloc(&nod1, l, nn); + cgen(l, &nod1); + regalloc(&nod, r, Z); + cgen(r, &nod); + } else { + regalloc(&nod, r, nn); + cgen(r, &nod); + regalloc(&nod1, l, Z); + cgen(l, &nod1); + } + gopcode(o, &nod1, &nod, Z); + regfree(&nod); + regfree(&nod1); + + com: + if(nn != Z) { + p1 = p; + gopcode(OAS, nodconst(1), Z, nn); + gbranch(OGOTO); + p2 = p; + patch(p1, pc); + gopcode(OAS, nodconst(0), Z, nn); + patch(p2, pc); + } + break; + } + cursafe = curs; +} + +void +sugen(Node *n, Node *nn, long w) +{ + Prog *p1; + Node nod0, nod1, nod2, nod3, nod4, *l, *r; + Type *t; + long pc1; + int i, m, c; + + if(n == Z || n->type == T) + return; + if(debug['g']) { + prtree(nn, "sugen lhs"); + prtree(n, "sugen"); + } + if(nn == nodrat) + if(w > nrathole) + nrathole = w; + switch(n->op) { + case OIND: + if(nn == Z) { + nullwarn(n->left, Z); + break; + } + + default: + goto copy; + + case ODOT: + l = n->left; + sugen(l, nodrat, l->type->width); + if(nn != Z) { + warn(n, "non-interruptable temporary"); + nod1 = *nodrat; + r = n->right; + if(!r || r->op != OCONST) { + diag(n, "DOT and no offset"); + break; + } + nod1.xoffset += (long)r->vconst; + nod1.type = n->type; + sugen(&nod1, nn, w); + } + break; + + case OSTRUCT: + /* + * rewrite so lhs has no fn call + */ + if(nn != Z && nn->complex >= FNX) { + nod1 = *n; + nod1.type = typ(TIND, n->type); + regret(&nod2, &nod1); + lcgen(nn, &nod2); + regsalloc(&nod0, &nod1); + gopcode(OAS, &nod2, Z, &nod0); + regfree(&nod2); + + nod1 = *n; + nod1.op = OIND; + nod1.left = &nod0; + nod1.right = Z; + nod1.complex = 1; + + sugen(n, &nod1, w); + return; + } + + r = n->left; + for(t = n->type->link; t != T; t = t->down) { + l = r; + if(r->op == OLIST) { + l = r->left; + r = r->right; + } + if(nn == Z) { + cgen(l, nn); + continue; + } + /* + * hand craft *(&nn + o) = l + */ + nod0 = znode; + nod0.op = OAS; + nod0.type = t; + nod0.left = &nod1; + nod0.right = l; + + nod1 = znode; + nod1.op = OIND; + nod1.type = t; + nod1.left = &nod2; + + nod2 = znode; + nod2.op = OADD; + nod2.type = typ(TIND, t); + nod2.left = &nod3; + nod2.right = &nod4; + + nod3 = znode; + nod3.op = OADDR; + nod3.type = nod2.type; + nod3.left = nn; + + nod4 = znode; + nod4.op = OCONST; + nod4.type = nod2.type; + nod4.vconst = t->offset; + + ccom(&nod0); + acom(&nod0); + xcom(&nod0); + nod0.addable = 0; + + cgen(&nod0, Z); + } + break; + + case OAS: + if(nn == Z) { + if(n->addable < INDEXED) + sugen(n->right, n->left, w); + break; + } + sugen(n->right, nodrat, w); + warn(n, "non-interruptable temporary"); + sugen(nodrat, n->left, w); + sugen(nodrat, nn, w); + break; + + case OFUNC: + if(nn == Z) { + sugen(n, nodrat, w); + break; + } + if(nn->op != OIND) { + nn = new1(OADDR, nn, Z); + nn->type = types[TIND]; + nn->addable = 0; + } else + nn = nn->left; + n = new(OFUNC, n->left, new(OLIST, nn, n->right)); + n->type = types[TVOID]; + n->left->type = types[TVOID]; + cgen(n, Z); + break; + + case OCOND: + bcgen(n->left, 1); + p1 = p; + sugen(n->right->left, nn, w); + gbranch(OGOTO); + patch(p1, pc); + p1 = p; + sugen(n->right->right, nn, w); + patch(p1, pc); + break; + + case OCOMMA: + cgen(n->left, Z); + sugen(n->right, nn, w); + break; + } + return; + +copy: + if(nn == Z) + return; + if(n->complex >= FNX && nn->complex >= FNX) { + t = nn->type; + nn->type = types[TLONG]; + regialloc(&nod1, nn, Z); + lcgen(nn, &nod1); + regsalloc(&nod2, nn); + nn->type = t; + + gopcode(OAS, &nod1, Z, &nod2); + regfree(&nod1); + + nod2.type = typ(TIND, t); + + nod1 = nod2; + nod1.op = OIND; + nod1.left = &nod2; + nod1.right = Z; + nod1.complex = 1; + nod1.type = t; + + sugen(n, &nod1, w); + return; + } + + if(n->complex > nn->complex) { + t = n->type; + n->type = types[TVLONG]; + reglcgen(&nod1, n, Z); + n->type = t; + + t = nn->type; + nn->type = types[TVLONG]; + reglcgen(&nod2, nn, Z); + nn->type = t; + } else { + t = nn->type; + nn->type = types[TVLONG]; + reglcgen(&nod2, nn, Z); + nn->type = t; + + t = n->type; + n->type = types[TVLONG]; + reglcgen(&nod1, n, Z); + n->type = t; + } + + w /= SZ_VLONG; + if(w <= 5) { + layout(&nod1, &nod2, w, 0, Z); + goto out; + } + + /* + * minimize space for unrolling loop + * 3,4,5 times. (6 or more is never minimum) + * if small structure, try 2 also. + */ + c = 0; /* set */ + m = 100; + i = 3; + if(w <= 15) + i = 2; + for(; i<=5; i++) + if(i + w%i <= m) { + c = i; + m = c + w%c; + } + + regalloc(&nod3, ®node, Z); + layout(&nod1, &nod2, w%c, w/c, &nod3); + + pc1 = pc; + layout(&nod1, &nod2, c, 0, Z); + + gopcode(OSUB, nodconst(1), Z, &nod3); + nod1.op = OREGISTER; + gopcode(OADD, nodconst(c*SZ_VLONG), Z, &nod1); + nod2.op = OREGISTER; + gopcode(OADD, nodconst(c*SZ_VLONG), Z, &nod2); + + gopcode(OEQ, &nod3, Z, Z); + p->as = ABGTZ; + patch(p, pc1); + + regfree(&nod3); +out: + regfree(&nod1); + regfree(&nod2); +} + +void +layout(Node *f, Node *t, int c, int cv, Node *cn) +{ + Node t1, t2; + + while(c > 3) { + layout(f, t, 2, 0, Z); + c -= 2; + } + + regalloc(&t1, ®node, Z); + regalloc(&t2, ®node, Z); + t1.type = types[TVLONG]; + t2.type = types[TVLONG]; + if(c > 0) { + gopcode(OAS, f, Z, &t1); + f->xoffset += SZ_VLONG; + } + if(cn != Z) + gopcode(OAS, nodconst(cv), Z, cn); + if(c > 1) { + gopcode(OAS, f, Z, &t2); + f->xoffset += SZ_VLONG; + } + if(c > 0) { + gopcode(OAS, &t1, Z, t); + t->xoffset += SZ_VLONG; + } + if(c > 2) { + gopcode(OAS, f, Z, &t1); + f->xoffset += SZ_VLONG; + } + if(c > 1) { + gopcode(OAS, &t2, Z, t); + t->xoffset += SZ_VLONG; + } + if(c > 2) { + gopcode(OAS, &t1, Z, t); + t->xoffset += SZ_VLONG; + } + regfree(&t1); + regfree(&t2); +} diff --git a/sys/src/cmd/4c/gc.h b/sys/src/cmd/4c/gc.h new file mode 100644 index 0000000..4b05abb --- /dev/null +++ b/sys/src/cmd/4c/gc.h @@ -0,0 +1,332 @@ +#include "../cc/cc.h" +#include "../vc/v.out.h" + +/* + * 4c/mips2 + * Mips 4000 + */ +#define SZ_CHAR 1 +#define SZ_SHORT 2 +#define SZ_INT 4 +#define SZ_LONG 4 +#define SZ_IND 8 +#define SZ_FLOAT 4 +#define SZ_VLONG 8 +#define SZ_DOUBLE 8 +#define FNX 100 + +typedef struct Adr Adr; +typedef struct Prog Prog; +typedef struct Case Case; +typedef struct C1 C1; +typedef struct Multab Multab; +typedef struct Hintab Hintab; +typedef struct Var Var; +typedef struct Reg Reg; +typedef struct Rgn Rgn; + +struct Adr +{ + long offset; + double dval; + vlong vval; + char sval[NSNAME]; + Ieee ieee; + + Sym* sym; + char type; + char reg; + char name; + char etype; +}; +#define A ((Adr*)0) + +#define INDEXED 9 +struct Prog +{ + Adr from; + Adr to; + Prog* link; + long lineno; + char as; + char reg; +}; +#define P ((Prog*)0) + +struct Case +{ + Case* link; + long val; + u64int label; + char def; +}; +#define C ((Case*)0) + +struct C1 +{ + long val; + long label; +}; + +struct Multab +{ + long val; + char code[20]; +}; + +struct Hintab +{ + ushort val; + char hint[10]; +}; + +struct Var +{ + long offset; + Sym* sym; + char name; + char etype; +}; + +struct Reg +{ + u64int pc; + long rpo; /* reverse post ordering */ + + Bits set; + Bits use1; + Bits use2; + + Bits refbehind; + Bits refahead; + Bits calbehind; + Bits calahead; + Bits regdiff; + Bits act; + + long regu; + long loop; /* could be shorter */ + + Reg* log5; + long active; + + Reg* p1; + Reg* p2; + Reg* p2link; + Reg* s1; + Reg* s2; + Reg* link; + Prog* prog; +}; +#define R ((Reg*)0) + +#define NRGN 600 +struct Rgn +{ + Reg* enter; + short cost; + short varno; + short regno; +}; + +EXTERN long breakpc; +EXTERN Case* cases; +EXTERN Node constnode; +EXTERN Node fconstnode; +EXTERN long continpc; +EXTERN long curarg; +EXTERN long cursafe; +EXTERN Prog* firstp; +EXTERN Prog* lastp; +EXTERN long maxargsafe; +EXTERN int mnstring; +EXTERN Multab multab[20]; +EXTERN int retok; +EXTERN int hintabsize; +EXTERN Node* nodrat; +EXTERN Node* nodret; +EXTERN Node* nodsafe; +EXTERN long nrathole; +EXTERN long nstring; +EXTERN Prog* p; +EXTERN u64int pc; +EXTERN Node regnode; +EXTERN char string[NSNAME]; +EXTERN Sym* symrathole; +EXTERN Node znode; +EXTERN Prog zprog; +EXTERN char reg[NREG+NREG]; +EXTERN u64int exregoffset; +EXTERN u64int exfregoffset; + +#define BLOAD(r) band(bnot(r->refbehind), r->refahead) +#define BSTORE(r) band(bnot(r->calbehind), r->calahead) +#define LOAD(r) (~r->refbehind.b[z] & r->refahead.b[z]) +#define STORE(r) (~r->calbehind.b[z] & r->calahead.b[z]) + +#define bset(a,n) ((a).b[(n)/32]&(1L<<(n)%32)) + +#define CLOAD 4 +#define CREF 5 +#define CINF 1000 +#define LOOP 3 + +EXTERN Rgn region[NRGN]; +EXTERN Rgn* rgp; +EXTERN int nregion; +EXTERN int nvar; + +EXTERN Bits externs; +EXTERN Bits params; +EXTERN Bits consts; +EXTERN Bits addrs; + +EXTERN long regbits; +EXTERN long exregbits; + +EXTERN int change; + +EXTERN Reg* firstr; +EXTERN Reg* lastr; +EXTERN Reg zreg; +EXTERN Reg* freer; +EXTERN Var var[NVAR]; +EXTERN long* idom; +EXTERN Reg** rpo2r; +EXTERN long maxnr; + +extern char* anames[]; +extern Hintab hintab[]; + +/* + * sgen.c + */ +void codgen(Node*, Node*); +void gen(Node*); +void noretval(int); +void xcom(Node*); +void bcomplex(Node*); +void usedset(Node*, int); + +/* + * cgen.c + */ +void cgen(Node*, Node*); +void reglcgen(Node*, Node*, Node*); +void lcgen(Node*, Node*); +void bcgen(Node*, int); +void boolgen(Node*, int, Node*); +void sugen(Node*, Node*, long); +void layout(Node*, Node*, int, int, Node*); + +/* + * txt.c + */ +void ginit(void); +void gclean(void); +void nextpc(void); +void gargs(Node*, Node*, Node*); +void garg1(Node*, Node*, Node*, int, Node**); +Node* nodconst(long); +Node* nodfconst(double); +void nodreg(Node*, Node*, int); +void regret(Node*, Node*); +void regalloc(Node*, Node*, Node*); +void regfree(Node*); +void regialloc(Node*, Node*, Node*); +void regsalloc(Node*, Node*); +void regaalloc1(Node*, Node*); +void regaalloc(Node*, Node*); +void regind(Node*, Node*); +void gprep(Node*, Node*); +void raddr(Node*, Prog*); +void naddr(Node*, Adr*); +void gmove(Node*, Node*); +void gins(int a, Node*, Node*); +void gopcode(int, Node*, Node*, Node*); +int samaddr(Node*, Node*); +void gbranch(int); +void patch(Prog*, vlong); +int sconst(Node*); +int llconst(Node*); +int sval(long); +void gpseudo(int, Sym*, Node*); + +/* + * swt.c + */ +int swcmp(const void*, const void*); +void doswit(Node*); +void swit1(C1*, int, long, Node*, Node*); +void cas(void); +void bitload(Node*, Node*, Node*, Node*, Node*); +void bitstore(Node*, Node*, Node*, Node*, Node*); +long outstring(char*, long); +int mulcon(Node*, Node*); +Multab* mulcon0(long); +void nullwarn(Node*, Node*); +void sextern(Sym*, Node*, long, long); +void gextern(Sym*, Node*, long, long); +void outcode(void); +void ieeedtod(Ieee*, double); + +/* + * list + */ +void listinit(void); +int Pconv(Fmt*); +int Aconv(Fmt*); +int Dconv(Fmt*); +int Sconv(Fmt*); +int Nconv(Fmt*); +int Bconv(Fmt*); + +/* + * reg.c + */ +Reg* rega(void); +int rcmp(const void*, const void*); +void regopt(Prog*); +void addmove(Reg*, int, int, int); +Bits mkvar(Adr*, int); +void prop(Reg*, Bits, Bits); +void loopit(Reg*, long); +void synch(Reg*, Bits); +ulong allreg(ulong, Rgn*); +void paint1(Reg*, int); +ulong paint2(Reg*, int); +void paint3(Reg*, int, long, int); +void addreg(Adr*, int); + +/* + * peep.c + */ +void peep(void); +void excise(Reg*); +Reg* uniqp(Reg*); +Reg* uniqs(Reg*); +int regtyp(Adr*); +int regzer(Adr*); +int anyvar(Adr*); +int subprop(Reg*); +int copyprop(Reg*); +int copy1(Adr*, Adr*, Reg*, int); +int copyu(Prog*, Adr*, Adr*); + +int copyas(Adr*, Adr*); +int copyau(Adr*, Adr*); +int copyau1(Prog*, Adr*); +int copysub(Adr*, Adr*, Adr*, int); +int copysub1(Prog*, Adr*, Adr*, int); + +long RtoB(int); +long FtoB(int); +int BtoR(long); +int BtoF(long); + +#pragma varargck type "A" int +#pragma varargck type "B" Bits +#pragma varargck type "D" Adr* +#pragma varargck type "N" Adr* +#pragma varargck type "P" Prog* +#pragma varargck type "S" char* diff --git a/sys/src/cmd/4c/list.c b/sys/src/cmd/4c/list.c new file mode 100644 index 0000000..20fb77e --- /dev/null +++ b/sys/src/cmd/4c/list.c @@ -0,0 +1,249 @@ +#define EXTERN +#include "gc.h" + +void +listinit(void) +{ + + fmtinstall('A', Aconv); + fmtinstall('P', Pconv); + fmtinstall('S', Sconv); + fmtinstall('N', Nconv); + fmtinstall('B', Bconv); + fmtinstall('D', Dconv); +} + +int +Bconv(Fmt *fp) +{ + char str[STRINGSZ], ss[STRINGSZ], *s; + Bits bits; + int i; + + str[0] = 0; + bits = va_arg(fp->args, Bits); + while(bany(&bits)) { + i = bnum(bits); + if(str[0]) + strcat(str, " "); + if(var[i].sym == S) { + sprint(ss, "$%ld", var[i].offset); + s = ss; + } else + s = var[i].sym->name; + if(strlen(str) + strlen(s) + 1 >= STRINGSZ) + break; + strcat(str, s); + bits.b[i/32] &= ~(1L << (i%32)); + } + return fmtstrcpy(fp, str); +} + +int +Pconv(Fmt *fp) +{ + char str[STRINGSZ]; + Prog *p; + int a; + + p = va_arg(fp->args, Prog*); + a = p->as; + if(a == ADATA) + sprint(str, " %A %D/%d,%D", a, &p->from, p->reg, &p->to); + else + if(p->as == ATEXT) + sprint(str, " %A %D,%d,%D", a, &p->from, p->reg, &p->to); + else + if(p->reg == NREG) + sprint(str, " %A %D,%D", a, &p->from, &p->to); + else + if(p->from.type != D_FREG) + sprint(str, " %A %D,R%d,%D", a, &p->from, p->reg, &p->to); + else + sprint(str, " %A %D,F%d,%D", a, &p->from, p->reg, &p->to); + return fmtstrcpy(fp, str); +} + +int +Aconv(Fmt *fp) +{ + char *s; + int a; + + a = va_arg(fp->args, int); + s = "???"; + if(a >= AXXX && a < ALAST) + s = anames[a]; + return fmtstrcpy(fp, s); +} + +int +Dconv(Fmt *fp) +{ + char str[STRINGSZ]; + Adr *a; + + a = va_arg(fp->args, Adr*); + switch(a->type) { + + default: + sprint(str, "GOK-type(%d)", a->type); + break; + + case D_NONE: + str[0] = 0; + if(a->name != D_NONE || a->reg != NREG || a->sym != S) + sprint(str, "%N(R%d)(NONE)", a, a->reg); + break; + + case D_CONST: + if(a->reg != NREG) + sprint(str, "$%N(R%d)", a, a->reg); + else + sprint(str, "$%N", a); + break; + + case D_OREG: + if(a->reg != NREG) + sprint(str, "%N(R%d)", a, a->reg); + else + sprint(str, "%N", a); + break; + + case D_REG: + sprint(str, "R%d", a->reg); + if(a->name != D_NONE || a->sym != S) + sprint(str, "%N(R%d)(REG)", a, a->reg); + break; + + case D_FREG: + sprint(str, "F%d", a->reg); + if(a->name != D_NONE || a->sym != S) + sprint(str, "%N(R%d)(REG)", a, a->reg); + break; + + case D_FCREG: + sprint(str, "FCR%d", a->reg); + if(a->name != D_NONE || a->sym != S) + sprint(str, "%N(R%d)(REG)", a, a->reg); + break; + + case D_LO: + sprint(str, "LO"); + if(a->name != D_NONE || a->sym != S) + sprint(str, "%N(LO)(REG)", a); + break; + + case D_HI: + sprint(str, "HI"); + if(a->name != D_NONE || a->sym != S) + sprint(str, "%N(HI)(REG)", a); + break; + + case D_BRANCH: + sprint(str, "%lld(PC)", a->offset-pc); + break; + + case D_FCONST: + sprint(str, "$%.17e", a->dval); + break; + + case D_SCONST: + sprint(str, "$\"%S\"", a->sval); + break; + + case D_VCONST: + sprint(str, "$0x%llux", a->vval); + break; + } + return fmtstrcpy(fp, str); +} + +int +Sconv(Fmt *fp) +{ + int i, c; + char str[STRINGSZ], *p, *a; + + a = va_arg(fp->args, char*); + p = str; + for(i=0; i= 'a' && c <= 'z' || + c >= 'A' && c <= 'Z' || + c >= '0' && c <= '9' || + c == ' ' || c == '%') { + *p++ = c; + continue; + } + *p++ = '\\'; + switch(c) { + case 0: + *p++ = 'z'; + continue; + case '\\': + case '"': + *p++ = c; + continue; + case '\n': + *p++ = 'n'; + continue; + case '\t': + *p++ = 't'; + continue; + case '\r': + *p++ = 'r'; + continue; + case '\f': + *p++ = 'f'; + continue; + } + *p++ = (c>>6) + '0'; + *p++ = ((c>>3) & 7) + '0'; + *p++ = (c & 7) + '0'; + } + *p = 0; + return fmtstrcpy(fp, str); +} + +int +Nconv(Fmt *fp) +{ + char str[STRINGSZ]; + Adr *a; + Sym *s; + + a = va_arg(fp->args, Adr*); + s = a->sym; + if(s == S) { + sprint(str, "%ld", a->offset); + goto out; + } + switch(a->name) { + default: + sprint(str, "GOK-name(%d)", a->name); + break; + + case D_NONE: + sprint(str, "%ld", a->offset); + break; + + case D_EXTERN: + sprint(str, "%s+%ld(SB)", s->name, a->offset); + break; + + case D_STATIC: + sprint(str, "%s<>+%ld(SB)", s->name, a->offset); + break; + + case D_AUTO: + sprint(str, "%s-%ld(SP)", s->name, -a->offset); + break; + + case D_PARAM: + sprint(str, "%s+%ld(FP)", s->name, a->offset); + break; + } +out: + return fmtstrcpy(fp, str); +} diff --git a/sys/src/cmd/4c/mkenam b/sys/src/cmd/4c/mkenam new file mode 100644 index 0000000..f09ef75 --- /dev/null +++ b/sys/src/cmd/4c/mkenam @@ -0,0 +1,15 @@ +ed - ../vc/v.out.h <<'!' +v/^ A/d +,s/^ A/ "/ +g/ .*$/s/// +,s/,*$/",/ +1i +char* anames[] = +{ +. +$a +}; +. +w enam.c +Q +! diff --git a/sys/src/cmd/4c/mkfile b/sys/src/cmd/4c/mkfile new file mode 100644 index 0000000..9fbfd07 --- /dev/null +++ b/sys/src/cmd/4c/mkfile @@ -0,0 +1,42 @@ +val == v) { + if(m->code[0] == 0) + return 0; + return m; + } + m++; + } + + /* + * select a spot in cache to overwrite + */ + multabp++; + if(multabp < 0 || multabp >= nelem(multab)) + multabp = 0; + m = multab+multabp; + m->val = v; + mulval = v; + + /* + * look in execption hint table + */ + a1 = 0; + a2 = hintabsize; + for(;;) { + if(a1 >= a2) + goto no; + g = (a2 + a1)/2; + if(v < hintab[g].val) { + a2 = g; + continue; + } + if(v > hintab[g].val) { + a1 = g+1; + continue; + } + break; + } + + if(docode(hintab[g].hint, m->code, 1, 0)) + return m; + print("multiply table failure %ld\n", v); + m->code[0] = 0; + return 0; + +no: + /* + * try to search + */ + hint[0] = 0; + for(g=1; g<=6; g++) { + if(g >= 6 && v >= 65535) + break; + mulcp = hint+g; + *mulcp = 0; + if(gen1(g)) { + if(docode(hint, m->code, 1, 0)) + return m; + print("multiply table failure %ld\n", v); + break; + } + } + + /* + * try a recur followed by a shift + */ + g = 0; + while(!(v & 1)) { + g++; + v >>= 1; + } + if(g) { + m1 = mulcon0(v); + if(m1) { + strcpy(m->code, m1->code); + sprint(strchr(m->code, 0), "%c0", g+'a'); + return m; + } + } + m->code[0] = 0; + return 0; +} + +static int +docode(char *hp, char *cp, int r0, int r1) +{ + int c, i; + + c = *hp++; + *cp = c; + cp += 2; + switch(c) { + default: + c -= 'a'; + if(c < 1 || c >= 30) + break; + for(i=0; i<4; i++) { + switch(i) { + case 0: + if(docode(hp, cp, r0<= mulval) + break; + } + if(mulval == 1) + return 1; + + len--; + for(i=1; i<=shmax; i++) + if(gen2(len, 1<= r1 || + r1 > valmax) + return 0; + + len--; + if(len == 0) + goto calcr0; + + if(!(flag & UR1)) { + f1 = UR1|SR1; + for(i=1; i<=shmax; i++) { + x = r0< valmax) + break; + if(gen3(len, r0, x, f1)) { + i += 'a'; + goto out; + } + } + } + + if(!(flag & UR0)) { + f1 = UR1|SR1; + for(i=1; i<=shmax; i++) { + x = r1< valmax) + break; + if(gen3(len, r1, x, f1)) { + i += 'a'; + goto out; + } + } + } + + if(!(flag & SR1)) { + f1 = UR1|SR1|(flag&UR0); + for(i=1; i<=shmax; i++) { + x = r1< valmax) + break; + if(gen3(len, r0, x, f1)) { + i += 'a'; + goto out; + } + } + } + + if(!(flag & SR0)) { + f1 = UR0|SR0|(flag&(SR1|UR1)); + + f2 = UR1|SR1; + if(flag & UR1) + f2 |= UR0; + if(flag & SR1) + f2 |= SR0; + + for(i=1; i<=shmax; i++) { + x = r0< valmax) + break; + if(x > r1) { + if(gen3(len, r1, x, f2)) { + i += 'a'; + goto out; + } + } else + if(gen3(len, x, r1, f1)) { + i += 'a'; + goto out; + } + } + } + + x = r1+r0; + if(gen3(len, r0, x, UR1)) { + i = '+'; + goto out; + } + + if(gen3(len, r1, x, UR1)) { + i = '+'; + goto out; + } + + x = r1-r0; + if(gen3(len, x, r1, UR0)) { + i = '-'; + goto out; + } + + if(x > r0) { + if(gen3(len, r0, x, UR1)) { + i = '-'; + goto out; + } + } else + if(gen3(len, x, r0, UR0)) { + i = '-'; + goto out; + } + + return 0; + +calcr0: + f1 = flag & (UR0|UR1); + if(f1 == UR1) { + for(i=1; i<=shmax; i++) { + x = r1<= mulval) { + if(x == mulval) { + i += 'a'; + goto out; + } + break; + } + } + } + + if(mulval == r1+r0) { + i = '+'; + goto out; + } + if(mulval == r1-r0) { + i = '-'; + goto out; + } + + return 0; + +out: + *--mulcp = i; + return 1; +} + +/* + * hint table has numbers that + * the search algorithm fails on. + * <1000: + * all numbers + * <5000: + * ÷ by 5 + * <10000: + * ÷ by 50 + * <65536: + * ÷ by 250 + */ +Hintab hintab[] = +{ + 683, "b++d+e+", + 687, "b+e++e-", + 691, "b++d+e+", + 731, "b++d+e+", + 811, "b++d+i+", + 821, "b++e+e+", + 843, "b+d++e+", + 851, "b+f-+e-", + 853, "b++e+e+", + 877, "c++++g-", + 933, "b+c++g-", + 981, "c-+e-d+", + 1375, "b+c+b+h-", + 1675, "d+b++h+", + 2425, "c++f-e+", + 2675, "c+d++f-", + 2750, "b+d-b+h-", + 2775, "c-+g-e-", + 3125, "b++e+g+", + 3275, "b+c+g+e+", + 3350, "c++++i+", + 3475, "c-+e-f-", + 3525, "c-+d+g-", + 3625, "c-+e-j+", + 3675, "b+d+d+e+", + 3725, "b+d-+h+", + 3925, "b+d+f-d-", + 4275, "b+g++e+", + 4325, "b+h-+d+", + 4425, "b+b+g-j-", + 4525, "b+d-d+f+", + 4675, "c++d-g+", + 4775, "b+d+b+g-", + 4825, "c+c-+i-", + 4850, "c++++i-", + 4925, "b++e-g-", + 4975, "c+f++e-", + 5500, "b+g-c+d+", + 6700, "d+b++i+", + 9700, "d++++j-", + 11000, "b+f-c-h-", + 11750, "b+d+g+j-", + 12500, "b+c+e-k+", + 13250, "b+d+e-f+", + 13750, "b+h-c-d+", + 14250, "b+g-c+e-", + 14500, "c+f+j-d-", + 14750, "d-g--f+", + 16750, "b+e-d-n+", + 17750, "c+h-b+e+", + 18250, "d+b+h-d+", + 18750, "b+g-++f+", + 19250, "b+e+b+h+", + 19750, "b++h--f-", + 20250, "b+e-l-c+", + 20750, "c++bi+e-", + 21250, "b+i+l+c+", + 22000, "b+e+d-g-", + 22250, "b+d-h+k-", + 22750, "b+d-e-g+", + 23250, "b+c+h+e-", + 23500, "b+g-c-g-", + 23750, "b+g-b+h-", + 24250, "c++g+m-", + 24750, "b+e+e+j-", + 25000, "b++dh+g+", + 25250, "b+e+d-g-", + 25750, "b+e+b+j+", + 26250, "b+h+c+e+", + 26500, "b+h+c+g+", + 26750, "b+d+e+g-", + 27250, "b+e+e+f+", + 27500, "c-i-c-d+", + 27750, "b+bd++j+", + 28250, "d-d-++i-", + 28500, "c+c-h-e-", + 29000, "b+g-d-f+", + 29500, "c+h+++e-", + 29750, "b+g+f-c+", + 30250, "b+f-g-c+", + 33500, "c-f-d-n+", + 33750, "b+d-b+j-", + 34250, "c+e+++i+", + 35250, "e+b+d+k+", + 35500, "c+e+d-g-", + 35750, "c+i-++e+", + 36250, "b+bh-d+e+", + 36500, "c+c-h-e-", + 36750, "d+e--i+", + 37250, "b+g+g+b+", + 37500, "b+h-b+f+", + 37750, "c+be++j-", + 38500, "b+e+b+i+", + 38750, "d+i-b+d+", + 39250, "b+g-l-+d+", + 39500, "b+g-c+g-", + 39750, "b+bh-c+f-", + 40250, "b+bf+d+g-", + 40500, "b+g-c+g+", + 40750, "c+b+i-e+", + 41250, "d++bf+h+", + 41500, "b+j+c+d-", + 41750, "c+f+b+h-", + 42500, "c+h++g+", + 42750, "b+g+d-f-", + 43250, "b+l-e+d-", + 43750, "c+bd+h+f-", + 44000, "b+f+g-d-", + 44250, "b+d-g--f+", + 44500, "c+e+c+h+", + 44750, "b+e+d-h-", + 45250, "b++g+j-g+", + 45500, "c+d+e-g+", + 45750, "b+d-h-e-", + 46250, "c+bd++j+", + 46500, "b+d-c-j-", + 46750, "e-e-b+g-", + 47000, "b+c+d-j-", + 47250, "b+e+e-g-", + 47500, "b+g-c-h-", + 47750, "b+f-c+h-", + 48250, "d--h+n-", + 48500, "b+c-g+m-", + 48750, "b+e+e-g+", + 49500, "c-f+e+j-", + 49750, "c+c+g++f-", + 50000, "b+e+e+k+", + 50250, "b++i++g+", + 50500, "c+g+f-i+", + 50750, "b+e+d+k-", + 51500, "b+i+c-f+", + 51750, "b+bd+g-e-", + 52250, "b+d+g-j+", + 52500, "c+c+f+g+", + 52750, "b+c+e+i+", + 53000, "b+i+c+g+", + 53500, "c+g+g-n+", + 53750, "b+j+d-c+", + 54250, "b+d-g-j-", + 54500, "c-f+e+f+", + 54750, "b+f-+c+g+", + 55000, "b+g-d-g-", + 55250, "b+e+e+g+", + 55500, "b+cd++j+", + 55750, "b+bh-d-f-", + 56250, "c+d-b+j-", + 56500, "c+d+c+i+", + 56750, "b+e+d++h-", + 57000, "b+d+g-f+", + 57250, "b+f-m+d-", + 57750, "b+i+c+e-", + 58000, "b+e+d+h+", + 58250, "c+b+g+g+", + 58750, "d-e-j--e+", + 59000, "d-i-+e+", + 59250, "e--h-m+", + 59500, "c+c-h+f-", + 59750, "b+bh-e+i-", + 60250, "b+bh-e-e-", + 60500, "c+c-g-g-", + 60750, "b+e-l-e-", + 61250, "b+g-g-c+", + 61750, "b+g-c+g+", + 62250, "f--+c-i-", + 62750, "e+f--+g+", + 64750, "b+f+d+p-", +}; +int hintabsize = nelem(hintab); diff --git a/sys/src/cmd/4c/peep.c b/sys/src/cmd/4c/peep.c new file mode 100644 index 0000000..8b9bb6a --- /dev/null +++ b/sys/src/cmd/4c/peep.c @@ -0,0 +1,693 @@ +#include "gc.h" + +void +peep(void) +{ + Reg *r, *r1, *r2; + Prog *p, *p1; + int t; +/* + * complete R structure + */ + t = 0; + for(r=firstr; r!=R; r=r1) { + r1 = r->link; + if(r1 == R) + break; + p = r->prog->link; + while(p != r1->prog) + switch(p->as) { + default: + r2 = rega(); + r->link = r2; + r2->link = r1; + + r2->prog = p; + r2->p1 = r; + r->s1 = r2; + r2->s1 = r1; + r1->p1 = r2; + + r = r2; + t++; + + case ADATA: + case AGLOBL: + case ANAME: + p = p->link; + } + } + +loop1: + t = 0; + for(r=firstr; r!=R; r=r->link) { + p = r->prog; + if(p->as == AMOVW || p->as == AMOVF || p->as == AMOVD) + if(regtyp(&p->to)) { + if(regtyp(&p->from)) + if(p->from.type == p->to.type) { + if(copyprop(r)) { + excise(r); + t++; + } else + if(subprop(r) && copyprop(r)) { + excise(r); + t++; + } + } + if(regzer(&p->from)) + if(p->to.type == D_REG) { + p->from.type = D_REG; + p->from.reg = 0; + if(copyprop(r)) { + excise(r); + t++; + } else + if(subprop(r) && copyprop(r)) { + excise(r); + t++; + } + } + } + } + if(t) + goto loop1; + /* + * look for MOVB x,R; MOVB R,R + */ + for(r=firstr; r!=R; r=r->link) { + p = r->prog; + switch(p->as) { + default: + continue; + case AMOVH: + case AMOVHU: + case AMOVB: + case AMOVBU: + if(p->to.type != D_REG) + continue; + break; + } + r1 = r->link; + if(r1 == R) + continue; + p1 = r1->prog; + if(p1->as != p->as) + continue; + if(p1->from.type != D_REG || p1->from.reg != p->to.reg) + continue; + if(p1->to.type != D_REG || p1->to.reg != p->to.reg) + continue; + excise(r1); + } +} + +void +excise(Reg *r) +{ + Prog *p; + + p = r->prog; + p->as = ANOP; + p->from = zprog.from; + p->to = zprog.to; + p->reg = zprog.reg; /**/ +} + +Reg* +uniqp(Reg *r) +{ + Reg *r1; + + r1 = r->p1; + if(r1 == R) { + r1 = r->p2; + if(r1 == R || r1->p2link != R) + return R; + } else + if(r->p2 != R) + return R; + return r1; +} + +Reg* +uniqs(Reg *r) +{ + Reg *r1; + + r1 = r->s1; + if(r1 == R) { + r1 = r->s2; + if(r1 == R) + return R; + } else + if(r->s2 != R) + return R; + return r1; +} + +int +regzer(Adr *a) +{ + + if(a->type == D_CONST) + if(a->sym == S) + if(a->offset == 0) + return 1; + if(a->type == D_REG) + if(a->reg == 0) + return 1; + return 0; +} + +int +regtyp(Adr *a) +{ + + if(a->type == D_REG) { + if(a->reg != 0) + return 1; + return 0; + } + if(a->type == D_FREG) + return 1; + return 0; +} + +/* + * the idea is to substitute + * one register for another + * from one MOV to another + * MOV a, R0 + * ADD b, R0 / no use of R1 + * MOV R0, R1 + * would be converted to + * MOV a, R1 + * ADD b, R1 + * MOV R1, R0 + * hopefully, then the former or latter MOV + * will be eliminated by copy propagation. + */ +int +subprop(Reg *r0) +{ + Prog *p; + Adr *v1, *v2; + Reg *r; + int t; + + p = r0->prog; + v1 = &p->from; + if(!regtyp(v1)) + return 0; + v2 = &p->to; + if(!regtyp(v2)) + return 0; + for(r=uniqp(r0); r!=R; r=uniqp(r)) { + if(uniqs(r) == R) + break; + p = r->prog; + switch(p->as) { + case AJAL: + return 0; + + case ASGT: + case ASGTU: + + case AADD: + case AADDU: + case ASUB: + case ASUBU: + case ASLL: + case ASRL: + case ASRA: + case AOR: + case AAND: + case AXOR: + case AMUL: + case AMULU: + case ADIV: + case ADIVU: + + case AADDD: + case AADDF: + case ASUBD: + case ASUBF: + case AMULD: + case AMULF: + case ADIVD: + case ADIVF: + if(p->to.type == v1->type) + if(p->to.reg == v1->reg) { + if(p->reg == NREG) + p->reg = p->to.reg; + goto gotit; + } + break; + + case AMOVF: + case AMOVD: + case AMOVW: + if(p->to.type == v1->type) + if(p->to.reg == v1->reg) + goto gotit; + break; + } + if(copyau(&p->from, v2) || + copyau1(p, v2) || + copyau(&p->to, v2)) + break; + if(copysub(&p->from, v1, v2, 0) || + copysub1(p, v1, v2, 0) || + copysub(&p->to, v1, v2, 0)) + break; + } + return 0; + +gotit: + copysub(&p->to, v1, v2, 1); + if(debug['P']) { + print("gotit: %D->%D\n%P", v1, v2, r->prog); + if(p->from.type == v2->type) + print(" excise"); + print("\n"); + } + for(r=uniqs(r); r!=r0; r=uniqs(r)) { + p = r->prog; + copysub(&p->from, v1, v2, 1); + copysub1(p, v1, v2, 1); + copysub(&p->to, v1, v2, 1); + if(debug['P']) + print("%P\n", r->prog); + } + t = v1->reg; + v1->reg = v2->reg; + v2->reg = t; + if(debug['P']) + print("%P last\n", r->prog); + return 1; +} + +/* + * The idea is to remove redundant copies. + * v1->v2 F=0 + * (use v2 s/v2/v1/)* + * set v1 F=1 + * use v2 return fail + * ----------------- + * v1->v2 F=0 + * (use v2 s/v2/v1/)* + * set v1 F=1 + * set v2 return success + */ +int +copyprop(Reg *r0) +{ + Prog *p; + Adr *v1, *v2; + Reg *r; + + p = r0->prog; + v1 = &p->from; + v2 = &p->to; + if(copyas(v1, v2)) + return 1; + for(r=firstr; r!=R; r=r->link) + r->active = 0; + return copy1(v1, v2, r0->s1, 0); +} + +int +copy1(Adr *v1, Adr *v2, Reg *r, int f) +{ + int t; + Prog *p; + + if(r->active) { + if(debug['P']) + print("act set; return 1\n"); + return 1; + } + r->active = 1; + if(debug['P']) + print("copy %D->%D f=%d\n", v1, v2, f); + for(; r != R; r = r->s1) { + p = r->prog; + if(debug['P']) + print("%P", p); + if(!f && uniqp(r) == R) { + f = 1; + if(debug['P']) + print("; merge; f=%d", f); + } + t = copyu(p, v2, A); + switch(t) { + case 2: /* rar, cant split */ + if(debug['P']) + print("; %Drar; return 0\n", v2); + return 0; + + case 3: /* set */ + if(debug['P']) + print("; %Dset; return 1\n", v2); + return 1; + + case 1: /* used, substitute */ + case 4: /* use and set */ + if(f) { + if(!debug['P']) + return 0; + if(t == 4) + print("; %Dused+set and f=%d; return 0\n", v2, f); + else + print("; %Dused and f=%d; return 0\n", v2, f); + return 0; + } + if(copyu(p, v2, v1)) { + if(debug['P']) + print("; sub fail; return 0\n"); + return 0; + } + if(debug['P']) + print("; sub%D/%D", v2, v1); + if(t == 4) { + if(debug['P']) + print("; %Dused+set; return 1\n", v2); + return 1; + } + break; + } + if(!f) { + t = copyu(p, v1, A); + if(!f && (t == 2 || t == 3 || t == 4)) { + f = 1; + if(debug['P']) + print("; %Dset and !f; f=%d", v1, f); + } + } + if(debug['P']) + print("\n"); + if(r->s2) + if(!copy1(v1, v2, r->s2, f)) + return 0; + } + return 1; +} + +/* + * return + * 1 if v only used (and substitute), + * 2 if read-alter-rewrite + * 3 if set + * 4 if set and used + * 0 otherwise (not touched) + */ +copyu(Prog *p, Adr *v, Adr *s) +{ + + switch(p->as) { + + default: + if(debug['P']) + print(" (???)"); + return 2; + + + case ANOP: /* read, write */ + case AMOVW: + case AMOVF: + case AMOVD: + case AMOVH: + case AMOVHU: + case AMOVB: + case AMOVBU: + case AMOVDW: + case AMOVWD: + case AMOVFD: + case AMOVDF: + if(s != A) { + if(copysub(&p->from, v, s, 1)) + return 1; + if(!copyas(&p->to, v)) + if(copysub(&p->to, v, s, 1)) + return 1; + return 0; + } + if(copyas(&p->to, v)) { + if(copyau(&p->from, v)) + return 4; + return 3; + } + if(copyau(&p->from, v)) + return 1; + if(copyau(&p->to, v)) + return 1; + return 0; + + case ASGT: /* read, read, write */ + case ASGTU: + + case AADD: + case AADDU: + case ASUB: + case ASUBU: + case ASLL: + case ASRL: + case ASRA: + case AOR: + case ANOR: + case AAND: + case AXOR: + case AMUL: + case AMULU: + case ADIV: + case ADIVU: + + case AADDF: + case AADDD: + case ASUBF: + case ASUBD: + case AMULF: + case AMULD: + case ADIVF: + case ADIVD: + if(s != A) { + if(copysub(&p->from, v, s, 1)) + return 1; + if(copysub1(p, v, s, 1)) + return 1; + if(!copyas(&p->to, v)) + if(copysub(&p->to, v, s, 1)) + return 1; + return 0; + } + if(copyas(&p->to, v)) { + if(p->reg == NREG) + p->reg = p->to.reg; + if(copyau(&p->from, v)) + return 4; + if(copyau1(p, v)) + return 4; + return 3; + } + if(copyau(&p->from, v)) + return 1; + if(copyau1(p, v)) + return 1; + if(copyau(&p->to, v)) + return 1; + return 0; + + case ABEQ: /* read, read */ + case ABNE: + case ABGTZ: + case ABGEZ: + case ABLTZ: + case ABLEZ: + + case ACMPEQD: + case ACMPEQF: + case ACMPGED: + case ACMPGEF: + case ACMPGTD: + case ACMPGTF: + case ABFPF: + case ABFPT: + if(s != A) { + if(copysub(&p->from, v, s, 1)) + return 1; + return copysub1(p, v, s, 1); + } + if(copyau(&p->from, v)) + return 1; + if(copyau1(p, v)) + return 1; + return 0; + + case AJMP: /* funny */ + if(s != A) { + if(copysub(&p->to, v, s, 1)) + return 1; + return 0; + } + if(copyau(&p->to, v)) + return 1; + return 0; + + case ARET: /* funny */ + if(v->type == D_REG) + if(v->reg == REGRET) + return 2; + if(v->type == D_FREG) + if(v->reg == FREGRET) + return 2; + + case AJAL: /* funny */ + if(v->type == D_REG) { + if(v->reg <= REGEXT && v->reg > exregoffset) + return 2; + if(REGARG && v->reg == REGARG) + return 2; + } + if(v->type == D_FREG) + if(v->reg <= FREGEXT && v->reg > exfregoffset) + return 2; + + if(s != A) { + if(copysub(&p->to, v, s, 1)) + return 1; + return 0; + } + if(copyau(&p->to, v)) + return 4; + return 3; + + case ATEXT: /* funny */ + if(v->type == D_REG) + if(v->reg == REGARG) + return 3; + return 0; + } + return 0; +} + +int +a2type(Prog *p) +{ + + switch(p->as) { + case ABEQ: + case ABNE: + case ABGTZ: + case ABGEZ: + case ABLTZ: + case ABLEZ: + + case ASGT: + case ASGTU: + + case AADD: + case AADDU: + case ASUB: + case ASUBU: + case ASLL: + case ASRL: + case ASRA: + case AOR: + case AAND: + case AXOR: + case AMUL: + case AMULU: + case ADIV: + case ADIVU: + return D_REG; + + case ACMPEQD: + case ACMPEQF: + case ACMPGED: + case ACMPGEF: + case ACMPGTD: + case ACMPGTF: + + case AADDF: + case AADDD: + case ASUBF: + case ASUBD: + case AMULF: + case AMULD: + case ADIVF: + case ADIVD: + return D_FREG; + } + return D_NONE; +} + +/* + * direct reference, + * could be set/use depending on + * semantics + */ +int +copyas(Adr *a, Adr *v) +{ + + if(regtyp(v)) + if(a->type == v->type) + if(a->reg == v->reg) + return 1; + return 0; +} + +/* + * either direct or indirect + */ +int +copyau(Adr *a, Adr *v) +{ + + if(copyas(a, v)) + return 1; + if(v->type == D_REG) + if(a->type == D_OREG) + if(v->reg == a->reg) + return 1; + return 0; +} + +int +copyau1(Prog *p, Adr *v) +{ + + if(regtyp(v)) + if(p->from.type == v->type || p->to.type == v->type) + if(p->reg == v->reg) { + if(a2type(p) != v->type) + print("botch a2type %P\n", p); + return 1; + } + return 0; +} + +/* + * substitute s for v in a + * return failure to substitute + */ +int +copysub(Adr *a, Adr *v, Adr *s, int f) +{ + + if(f) + if(copyau(a, v)) + a->reg = s->reg; + return 0; +} + +int +copysub1(Prog *p1, Adr *v, Adr *s, int f) +{ + + if(f) + if(copyau1(p1, v)) + p1->reg = s->reg; + return 0; +} diff --git a/sys/src/cmd/4c/reg.c b/sys/src/cmd/4c/reg.c new file mode 100644 index 0000000..bf121ab --- /dev/null +++ b/sys/src/cmd/4c/reg.c @@ -0,0 +1,1149 @@ +#include "gc.h" + +void addsplits(void); + +Reg* +rega(void) +{ + Reg *r; + + r = freer; + if(r == R) { + r = alloc(sizeof(*r)); + } else + freer = r->link; + + *r = zreg; + return r; +} + +int +rcmp(const void *a1, const void *a2) +{ + Rgn *p1, *p2; + int c1, c2; + + p1 = (Rgn*)a1; + p2 = (Rgn*)a2; + c1 = p2->cost; + c2 = p1->cost; + if(c1 -= c2) + return c1; + return p2->varno - p1->varno; +} + +void +regopt(Prog *p) +{ + Reg *r, *r1, *r2; + Prog *p1; + int i, z; + long initpc, val, npc; + ulong vreg; + Bits bit; + struct + { + long m; + long c; + Reg* p; + } log5[6], *lp; + + firstr = R; + lastr = R; + nvar = 0; + regbits = 0; + for(z=0; zm = val; + lp->c = 0; + lp->p = R; + val /= 5L; + lp++; + } + val = 0; + for(; p != P; p = p->link) { + switch(p->as) { + case ADATA: + case AGLOBL: + case ANAME: + continue; + } + r = rega(); + if(firstr == R) { + firstr = r; + lastr = r; + } else { + lastr->link = r; + r->p1 = lastr; + lastr->s1 = r; + lastr = r; + } + r->prog = p; + r->pc = val; + val++; + + lp = log5; + for(i=0; i<5; i++) { + lp->c--; + if(lp->c <= 0) { + lp->c = lp->m; + if(lp->p != R) + lp->p->log5 = r; + lp->p = r; + (lp+1)->c = 0; + break; + } + lp++; + } + + r1 = r->p1; + if(r1 != R) + switch(r1->prog->as) { + case ARET: + case AJMP: + case ARFE: + r->p1 = R; + r1->s1 = R; + } + + /* + * left side always read + */ + bit = mkvar(&p->from, p->as==AMOVW); + for(z=0; zuse1.b[z] |= bit.b[z]; + + /* + * right side depends on opcode + */ + bit = mkvar(&p->to, 0); + if(bany(&bit)) + switch(p->as) { + default: + diag(Z, "reg: unknown asop: %A", p->as); + break; + + /* + * right side write + */ + case ANOP: + case AMOVB: + case AMOVBU: + case AMOVH: + case AMOVHU: + case AMOVW: + case AMOVV: + case AMOVF: + case AMOVD: + for(z=0; zset.b[z] |= bit.b[z]; + break; + + /* + * funny + */ + case AJAL: + for(z=0; zlink) { + p = r->prog; + if(p->to.type == D_BRANCH) { + val = p->to.offset - initpc; + r1 = firstr; + while(r1 != R) { + r2 = r1->log5; + if(r2 != R && val >= r2->pc) { + r1 = r2; + continue; + } + if(r1->pc == val) + break; + r1 = r1->link; + } + if(r1 == R) { + nearln = p->lineno; + diag(Z, "ref not found\n%P", p); + continue; + } + if(r1 == r) { + nearln = p->lineno; + diag(Z, "ref to self\n%P", p); + continue; + } + r->s2 = r1; + r->p2link = r1->p2; + r1->p2 = r; + } + } + if(debug['R']) { + p = firstr->prog; + print("\n%L %D\n", p->lineno, &p->from); + } + + /* + * pass 2.5 + * find looping structure + */ + for(r = firstr; r != R; r = r->link) + r->active = 0; + change = 0; + loopit(firstr, npc); + + /* + * pass 3 + * iterate propagating usage + * back until flow graph is complete + */ +loop1: + change = 0; + for(r = firstr; r != R; r = r->link) + r->active = 0; + for(r = firstr; r != R; r = r->link) + if(r->prog->as == ARET) + prop(r, zbits, zbits); +loop11: + /* pick up unreachable code */ + i = 0; + for(r = firstr; r != R; r = r1) { + r1 = r->link; + if(r1 && r1->active && !r->active) { + prop(r, zbits, zbits); + i = 1; + } + } + if(i) + goto loop11; + if(change) + goto loop1; + + + /* + * pass 4 + * iterate propagating register/variable synchrony + * forward until graph is complete + */ +loop2: + change = 0; + for(r = firstr; r != R; r = r->link) + r->active = 0; + synch(firstr, zbits); + if(change) + goto loop2; + + addsplits(); + + if(debug['R'] && debug['v']) { + print("\nprop structure:\n"); + for(r = firstr; r != R; r = r->link) { + print("%ld:%P", r->loop, r->prog); + for(z=0; zset.b[z] | + r->refahead.b[z] | r->calahead.b[z] | + r->refbehind.b[z] | r->calbehind.b[z] | + r->use1.b[z] | r->use2.b[z]; + if(bany(&bit)) { + print("\t"); + if(bany(&r->use1)) + print(" u1=%B", r->use1); + if(bany(&r->use2)) + print(" u2=%B", r->use2); + if(bany(&r->set)) + print(" st=%B", r->set); + if(bany(&r->refahead)) + print(" ra=%B", r->refahead); + if(bany(&r->calahead)) + print(" ca=%B", r->calahead); + if(bany(&r->refbehind)) + print(" rb=%B", r->refbehind); + if(bany(&r->calbehind)) + print(" cb=%B", r->calbehind); + } + print("\n"); + } + } + + /* + * pass 5 + * isolate regions + * calculate costs (paint1) + */ + r = firstr; + if(r) { + for(z=0; zrefahead.b[z] | r->calahead.b[z]) & + ~(externs.b[z] | params.b[z] | addrs.b[z] | consts.b[z]); + if(bany(&bit)) { + nearln = r->prog->lineno; + warn(Z, "used and not set: %B", bit); + if(debug['R'] && !debug['w']) + print("used and not set: %B\n", bit); + } + } + + for(r = firstr; r != R; r = r->link) + r->act = zbits; + rgp = region; + nregion = 0; + for(r = firstr; r != R; r = r->link) { + for(z=0; zset.b[z] & + ~(r->refahead.b[z] | r->calahead.b[z] | addrs.b[z]); + if(bany(&bit)) { + nearln = r->prog->lineno; + warn(Z, "set and not used: %B", bit); + if(debug['R']) + print("set and not used: %B\n", bit); + excise(r); + } + for(z=0; zact.b[z] | addrs.b[z]); + while(bany(&bit)) { + i = bnum(bit); + rgp->enter = r; + rgp->varno = i; + change = 0; + if(debug['R'] && debug['v']) + print("\n"); + paint1(r, i); + bit.b[i/32] &= ~(1L<<(i%32)); + if(change <= 0) { + if(debug['R']) + print("%L $%d: %B\n", + r->prog->lineno, change, blsh(i)); + continue; + } + rgp->cost = change; + nregion++; + if(nregion >= NRGN) { + warn(Z, "too many regions"); + goto brk; + } + rgp++; + } + } +brk: + qsort(region, nregion, sizeof(region[0]), rcmp); + + /* + * pass 6 + * determine used registers (paint2) + * replace code (paint3) + */ + rgp = region; + for(i=0; ivarno); + vreg = paint2(rgp->enter, rgp->varno); + vreg = allreg(vreg, rgp); + if(debug['R']) { + if(rgp->regno >= NREG) + print("%L $%d F%d: %B\n", + rgp->enter->prog->lineno, + rgp->cost, + rgp->regno-NREG, + bit); + else + print("%L $%d R%d: %B\n", + rgp->enter->prog->lineno, + rgp->cost, + rgp->regno, + bit); + } + if(rgp->regno != 0) + paint3(rgp->enter, rgp->varno, vreg, rgp->regno); + rgp++; + } + /* + * pass 7 + * peep-hole on basic block + */ + if(!debug['R'] || debug['P']) + peep(); + + /* + * pass 8 + * recalculate pc + */ + val = initpc; + for(r = firstr; r != R; r = r1) { + r->pc = val; + p = r->prog; + p1 = P; + r1 = r->link; + if(r1 != R) + p1 = r1->prog; + for(; p != p1; p = p->link) { + switch(p->as) { + default: + val++; + break; + + case ANOP: + case ADATA: + case AGLOBL: + case ANAME: + break; + } + } + } + pc = val; + + /* + * fix up branches + */ + if(debug['R']) + if(bany(&addrs)) + print("addrs: %B\n", addrs); + + r1 = 0; /* set */ + for(r = firstr; r != R; r = r->link) { + p = r->prog; + if(p->to.type == D_BRANCH) + p->to.offset = r->s2->pc; + r1 = r; + } + + /* + * last pass + * eliminate nops + * free aux structures + */ + for(p = firstr->prog; p != P; p = p->link){ + while(p->link && p->link->as == ANOP) + p->link = p->link->link; + } + if(r1 != R) { + r1->link = freer; + freer = firstr; + } +} + +void +addsplits(void) +{ + Reg *r, *r1; + int z, i; + Bits bit; + + for(r = firstr; r != R; r = r->link) { + if(r->loop > 1) + continue; + if(r->prog->as == AJAL) + continue; + for(r1 = r->p2; r1 != R; r1 = r1->p2link) { + if(r1->loop <= 1) + continue; + for(z=0; zcalbehind.b[z] & + (r->refahead.b[z] | r->use1.b[z] | r->use2.b[z]) & + ~(r->calahead.b[z] & addrs.b[z]); + while(bany(&bit)) { + i = bnum(bit); + bit.b[i/32] &= ~(1L << (i%32)); + } + } + } +} + +/* + * add mov b,rn + * just after r + */ +void +addmove(Reg *r, int bn, int rn, int f) +{ + Prog *p, *p1; + Adr *a; + Var *v; + + p1 = alloc(sizeof(*p1)); + *p1 = zprog; + p = r->prog; + + p1->link = p->link; + p->link = p1; + p1->lineno = p->lineno; + + v = var + bn; + + a = &p1->to; + a->sym = v->sym; + a->name = v->name; + a->offset = v->offset; + a->etype = v->etype; + a->type = D_OREG; + if(a->etype == TARRAY || a->sym == S) + a->type = D_CONST; + + p1->as = AMOVW; + if(v->etype == TCHAR || v->etype == TUCHAR) + p1->as = AMOVB; + if(v->etype == TSHORT || v->etype == TUSHORT) + p1->as = AMOVH; + if(v->etype == TVLONG || v->etype == TUVLONG || v->etype == TIND) + p1->as = AMOVV; + if(v->etype == TFLOAT) + p1->as = AMOVF; + if(v->etype == TDOUBLE) + p1->as = AMOVD; + + p1->from.type = D_REG; + p1->from.reg = rn; + if(rn >= NREG) { + p1->from.type = D_FREG; + p1->from.reg = rn-NREG; + } + if(!f) { + p1->from = *a; + *a = zprog.from; + a->type = D_REG; + a->reg = rn; + if(rn >= NREG) { + a->type = D_FREG; + a->reg = rn-NREG; + } + if(v->etype == TUCHAR) + p1->as = AMOVBU; + if(v->etype == TUSHORT) + p1->as = AMOVHU; + } + if(debug['R']) + print("%P\t.a%P\n", p, p1); +} + +Bits +mkvar(Adr *a, int docon) +{ + Var *v; + int i, t, n, et, z; + long o; + Bits bit; + Sym *s; + + t = a->type; + if(t == D_REG && a->reg != NREG) + regbits |= RtoB(a->reg); + if(t == D_FREG && a->reg != NREG) + regbits |= FtoB(a->reg); + s = a->sym; + o = a->offset; + et = a->etype; + if(s == S) { + if(t != D_CONST || !docon || a->reg != NREG) + goto none; + et = TLONG; + } + if(t == D_CONST) { + if(s == S && sval(o)) + goto none; + } + + n = a->name; + v = var; + for(i=0; isym) + if(n == v->name) + if(o == v->offset) + goto out; + v++; + } + if(s) + if(s->name[0] == '.') + goto none; + if(nvar >= NVAR) { + if(debug['w'] > 1 && s) + warn(Z, "variable not optimized: %s", s->name); + goto none; + } + i = nvar; + nvar++; + v = &var[i]; + v->sym = s; + v->offset = o; + v->etype = et; + v->name = n; + if(debug['R']) + print("bit=%2d et=%2d %D\n", i, et, a); +out: + bit = blsh(i); + if(n == D_EXTERN || n == D_STATIC) + for(z=0; zetype != et || !typechlpfd[et]) /* funny punning */ + for(z=0; zp1) { + for(z=0; zrefahead.b[z]; + if(ref.b[z] != r1->refahead.b[z]) { + r1->refahead.b[z] = ref.b[z]; + change++; + } + cal.b[z] |= r1->calahead.b[z]; + if(cal.b[z] != r1->calahead.b[z]) { + r1->calahead.b[z] = cal.b[z]; + change++; + } + } + switch(r1->prog->as) { + case AJAL: + for(z=0; zset.b[z]) | + r1->use1.b[z] | r1->use2.b[z]; + cal.b[z] &= ~(r1->set.b[z] | r1->use1.b[z] | r1->use2.b[z]); + r1->refbehind.b[z] = ref.b[z]; + r1->calbehind.b[z] = cal.b[z]; + } + if(r1->active) + break; + r1->active = 1; + } + for(; r != r1; r = r->p1) + for(r2 = r->p2; r2 != R; r2 = r2->p2link) + prop(r2, r->refbehind, r->calbehind); +} + +/* + * find looping structure + * + * 1) find reverse postordering + * 2) find approximate dominators, + * the actual dominators if the flow graph is reducible + * otherwise, dominators plus some other non-dominators. + * See Matthew S. Hecht and Jeffrey D. Ullman, + * "Analysis of a Simple Algorithm for Global Data Flow Problems", + * Conf. Record of ACM Symp. on Principles of Prog. Langs, Boston, Massachusetts, + * Oct. 1-3, 1973, pp. 207-217. + * 3) find all nodes with a predecessor dominated by the current node. + * such a node is a loop head. + * recursively, all preds with a greater rpo number are in the loop + */ +long +postorder(Reg *r, Reg **rpo2r, long n) +{ + Reg *r1; + + r->rpo = 1; + r1 = r->s1; + if(r1 && !r1->rpo) + n = postorder(r1, rpo2r, n); + r1 = r->s2; + if(r1 && !r1->rpo) + n = postorder(r1, rpo2r, n); + rpo2r[n] = r; + n++; + return n; +} + +long +rpolca(long *idom, long rpo1, long rpo2) +{ + long t; + + if(rpo1 == -1) + return rpo2; + while(rpo1 != rpo2){ + if(rpo1 > rpo2){ + t = rpo2; + rpo2 = rpo1; + rpo1 = t; + } + while(rpo1 < rpo2){ + t = idom[rpo2]; + if(t >= rpo2) + sysfatal("bad idom"); + rpo2 = t; + } + } + return rpo1; +} + +int +doms(long *idom, long r, long s) +{ + while(s > r) + s = idom[s]; + return s == r; +} + +int +loophead(long *idom, Reg *r) +{ + long src; + + src = r->rpo; + if(r->p1 != R && doms(idom, src, r->p1->rpo)) + return 1; + for(r = r->p2; r != R; r = r->p2link) + if(doms(idom, src, r->rpo)) + return 1; + return 0; +} + +void +loopmark(Reg **rpo2r, long head, Reg *r) +{ + if(r->rpo < head || r->active == head) + return; + r->active = head; + r->loop += LOOP; + if(r->p1 != R) + loopmark(rpo2r, head, r->p1); + for(r = r->p2; r != R; r = r->p2link) + loopmark(rpo2r, head, r); +} + +void +loopit(Reg *r, long nr) +{ + Reg *r1; + long i, d, me; + + if(nr > maxnr) { + rpo2r = alloc(nr * sizeof(Reg*)); + idom = alloc(nr * sizeof(long)); + maxnr = nr; + } + + d = postorder(r, rpo2r, 0); + if(d > nr) + sysfatal("too many reg nodes"); + nr = d; + for(i = 0; i < nr / 2; i++){ + r1 = rpo2r[i]; + rpo2r[i] = rpo2r[nr - 1 - i]; + rpo2r[nr - 1 - i] = r1; + } + for(i = 0; i < nr; i++) + rpo2r[i]->rpo = i; + + idom[0] = 0; + for(i = 0; i < nr; i++){ + r1 = rpo2r[i]; + me = r1->rpo; + d = -1; + if(r1->p1 != R && r1->p1->rpo < me) + d = r1->p1->rpo; + for(r1 = r1->p2; r1 != nil; r1 = r1->p2link) + if(r1->rpo < me) + d = rpolca(idom, d, r1->rpo); + idom[i] = d; + } + + for(i = 0; i < nr; i++){ + r1 = rpo2r[i]; + r1->loop++; + if(r1->p2 != R && loophead(idom, r1)) + loopmark(rpo2r, i, r1); + } +} + +void +synch(Reg *r, Bits dif) +{ + Reg *r1; + int z; + + for(r1 = r; r1 != R; r1 = r1->s1) { + for(z=0; zrefbehind.b[z] & r1->refahead.b[z])) | + r1->set.b[z] | r1->regdiff.b[z]; + if(dif.b[z] != r1->regdiff.b[z]) { + r1->regdiff.b[z] = dif.b[z]; + change++; + } + } + if(r1->active) + break; + r1->active = 1; + for(z=0; zcalbehind.b[z] & r1->calahead.b[z]); + if(r1->s2 != R) + synch(r1->s2, dif); + } +} + +ulong +allreg(ulong b, Rgn *r) +{ + Var *v; + int i; + + v = var + r->varno; + r->regno = 0; + switch(v->etype) { + + default: + diag(Z, "unknown etype %d/%d", bitno(b), v->etype); + break; + + case TCHAR: + case TUCHAR: + case TSHORT: + case TUSHORT: + case TINT: + case TUINT: + case TLONG: + case TULONG: + case TVLONG: + case TUVLONG: + case TIND: + case TARRAY: + i = BtoR(~b); + if(i && r->cost >= 0) { + r->regno = i; + return RtoB(i); + } + break; + + case TDOUBLE: + case TFLOAT: + i = BtoF(~b); + if(i && r->cost >= 0) { + r->regno = i+NREG; + return FtoB(i); + } + break; + } + return 0; +} + +void +paint1(Reg *r, int bn) +{ + Reg *r1; + Prog *p; + int z; + ulong bb; + + z = bn/32; + bb = 1L<<(bn%32); + if(r->act.b[z] & bb) + return; + for(;;) { + if(!(r->refbehind.b[z] & bb)) + break; + r1 = r->p1; + if(r1 == R) + break; + if(!(r1->refahead.b[z] & bb)) + break; + if(r1->act.b[z] & bb) + break; + r = r1; + } + + if(LOAD(r) & ~(r->set.b[z] & ~(r->use1.b[z]|r->use2.b[z])) & bb) { + change -= CLOAD * r->loop; + if(debug['R'] && debug['v']) + print("%ld%P\tld %B $%d\n", r->loop, + r->prog, blsh(bn), change); + } + for(;;) { + r->act.b[z] |= bb; + p = r->prog; + + if(r->use1.b[z] & bb) { + change += CREF * r->loop; + if(debug['R'] && debug['v']) + print("%ld%P\tu1 %B $%d\n", r->loop, + p, blsh(bn), change); + } + + if((r->use2.b[z]|r->set.b[z]) & bb) { + change += CREF * r->loop; + if(debug['R'] && debug['v']) + print("%ld%P\tu2 %B $%d\n", r->loop, + p, blsh(bn), change); + } + + if(STORE(r) & r->regdiff.b[z] & bb) { + change -= CLOAD * r->loop; + if(debug['R'] && debug['v']) + print("%ld%P\tst %B $%d\n", r->loop, + p, blsh(bn), change); + } + + if(r->refbehind.b[z] & bb) + for(r1 = r->p2; r1 != R; r1 = r1->p2link) + if(r1->refahead.b[z] & bb) + paint1(r1, bn); + + if(!(r->refahead.b[z] & bb)) + break; + r1 = r->s2; + if(r1 != R) + if(r1->refbehind.b[z] & bb) + paint1(r1, bn); + r = r->s1; + if(r == R) + break; + if(r->act.b[z] & bb) + break; + if(!(r->refbehind.b[z] & bb)) + break; + } +} + +ulong +paint2(Reg *r, int bn) +{ + Reg *r1; + int z; + ulong bb, vreg; + + z = bn/32; + bb = 1L << (bn%32); + vreg = regbits; + if(!(r->act.b[z] & bb)) + return vreg; + for(;;) { + if(!(r->refbehind.b[z] & bb)) + break; + r1 = r->p1; + if(r1 == R) + break; + if(!(r1->refahead.b[z] & bb)) + break; + if(!(r1->act.b[z] & bb)) + break; + r = r1; + } + for(;;) { + r->act.b[z] &= ~bb; + + vreg |= r->regu; + + if(r->refbehind.b[z] & bb) + for(r1 = r->p2; r1 != R; r1 = r1->p2link) + if(r1->refahead.b[z] & bb) + vreg |= paint2(r1, bn); + + if(!(r->refahead.b[z] & bb)) + break; + r1 = r->s2; + if(r1 != R) + if(r1->refbehind.b[z] & bb) + vreg |= paint2(r1, bn); + r = r->s1; + if(r == R) + break; + if(!(r->act.b[z] & bb)) + break; + if(!(r->refbehind.b[z] & bb)) + break; + } + return vreg; +} + +void +paint3(Reg *r, int bn, long rb, int rn) +{ + Reg *r1; + Prog *p; + int z; + ulong bb; + + z = bn/32; + bb = 1L << (bn%32); + if(r->act.b[z] & bb) + return; + for(;;) { + if(!(r->refbehind.b[z] & bb)) + break; + r1 = r->p1; + if(r1 == R) + break; + if(!(r1->refahead.b[z] & bb)) + break; + if(r1->act.b[z] & bb) + break; + r = r1; + } + + if(LOAD(r) & ~(r->set.b[z] & ~(r->use1.b[z]|r->use2.b[z])) & bb) + addmove(r, bn, rn, 0); + for(;;) { + r->act.b[z] |= bb; + p = r->prog; + + if(r->use1.b[z] & bb) { + if(debug['R']) + print("%P", p); + addreg(&p->from, rn); + if(debug['R']) + print("\t.c%P\n", p); + } + if((r->use2.b[z]|r->set.b[z]) & bb) { + if(debug['R']) + print("%P", p); + addreg(&p->to, rn); + if(debug['R']) + print("\t.c%P\n", p); + } + + if(STORE(r) & r->regdiff.b[z] & bb) + addmove(r, bn, rn, 1); + r->regu |= rb; + + if(r->refbehind.b[z] & bb) + for(r1 = r->p2; r1 != R; r1 = r1->p2link) + if(r1->refahead.b[z] & bb) + paint3(r1, bn, rb, rn); + + if(!(r->refahead.b[z] & bb)) + break; + r1 = r->s2; + if(r1 != R) + if(r1->refbehind.b[z] & bb) + paint3(r1, bn, rb, rn); + r = r->s1; + if(r == R) + break; + if(r->act.b[z] & bb) + break; + if(!(r->refbehind.b[z] & bb)) + break; + } +} + +void +addreg(Adr *a, int rn) +{ + + a->sym = 0; + a->name = D_NONE; + a->type = D_REG; + a->reg = rn; + if(rn >= NREG) { + a->type = D_FREG; + a->reg = rn-NREG; + } +} + +/* + * bit reg + * 0 R3 + * 1 R4 + * ... ... + * 19 R22 + * 20 R23 + */ +long +RtoB(int r) +{ + + if(r < 3 || r > 23) + return 0; + return 1L << (r-3); +} + +BtoR(long b) +{ + + b &= 0x001fffffL; + if(b == 0) + return 0; + return bitno(b) + 3; +} + +/* + * bit reg + * 22 F4 + * 23 F6 + * ... ... + * 31 F22 + */ +long +FtoB(int f) +{ + + if(f < 4 || f > 22 || (f&1)) + return 0; + return 1L << (f/2 + 20); +} + +int +BtoF(long b) +{ + + b &= 0xffc00000L; + if(b == 0) + return 0; + return bitno(b)*2 - 40; +} diff --git a/sys/src/cmd/4c/sgen.c b/sys/src/cmd/4c/sgen.c new file mode 100644 index 0000000..bc5c9ca --- /dev/null +++ b/sys/src/cmd/4c/sgen.c @@ -0,0 +1,569 @@ +#include "gc.h" + +void +codgen(Node *n, Node *nn) +{ + Prog *sp; + Node *n1, nod, nod1; + + cursafe = 0; + curarg = 0; + maxargsafe = 0; + + /* + * isolate name + */ + for(n1 = nn;; n1 = n1->left) { + if(n1 == Z) { + diag(nn, "cant find function name"); + return; + } + if(n1->op == ONAME) + break; + } + nearln = nn->lineno; + gpseudo(ATEXT, n1->sym, nodconst(stkoff)); + sp = p; + + /* + * isolate first argument + */ + if(REGARG) { + if(typesu[thisfn->link->etype]) { + nod1 = *nodret->left; + nodreg(&nod, &nod1, REGARG); + gopcode(OAS, &nod, Z, &nod1); + } else + if(firstarg && typechlp[firstargtype->etype]) { + nod1 = *nodret->left; + nod1.sym = firstarg; + nod1.type = firstargtype; + nod1.xoffset = align(0, firstargtype, Aarg1); + nod1.etype = firstargtype->etype; + nodreg(&nod, &nod1, REGARG); + gopcode(OAS, &nod, Z, &nod1); + } + } + + retok = 0; + gen(n); + if(!retok) + if(thisfn->link->etype != TVOID) + warn(Z, "no return at end of function: %s", n1->sym->name); + noretval(3); + gbranch(ORETURN); + + if(!debug['N'] || debug['R'] || debug['P']) + regopt(sp); + + sp->to.offset += maxargsafe; +} + +void +gen(Node *n) +{ + Node *l, nod; + Prog *sp, *spc, *spb; + Case *cn; + u64int sbc, scc; + int o; + +loop: + if(n == Z) + return; + nearln = n->lineno; + o = n->op; + if(debug['G']) + if(o != OLIST) + print("%L %O\n", nearln, o); + + retok = 0; + switch(o) { + + default: + complex(n); + cgen(n, Z); + break; + + case OLIST: + gen(n->left); + + rloop: + n = n->right; + goto loop; + + case ORETURN: + retok = 1; + complex(n); + if(n->type == T) + break; + l = n->left; + if(l == Z) { + noretval(3); + gbranch(ORETURN); + break; + } + if(typesu[n->type->etype]) { + sugen(l, nodret, n->type->width); + noretval(3); + gbranch(ORETURN); + break; + } + regret(&nod, n); + cgen(l, &nod); + regfree(&nod); + if(typefd[n->type->etype]) + noretval(1); + else + noretval(2); + gbranch(ORETURN); + break; + + case OLABEL: + l = n->left; + if(l) { + l->pc = pc; + if(l->label) + patch(l->label, pc); + } + gbranch(OGOTO); /* prevent self reference in reg */ + patch(p, pc); + goto rloop; + + case OGOTO: + retok = 1; + n = n->left; + if(n == Z) + return; + if(n->complex == 0) { + diag(Z, "label undefined: %s", n->sym->name); + return; + } + gbranch(OGOTO); + if(n->pc) { + patch(p, n->pc); + return; + } + if(n->label) + patch(n->label, pc-1); + n->label = p; + return; + + case OCASE: + l = n->left; + if(cases == C) + diag(n, "case/default outside a switch"); + if(l == Z) { + cas(); + cases->val = 0; + cases->def = 1; + cases->label = pc; + goto rloop; + } + complex(l); + if(l->type == T) + goto rloop; + if(l->op == OCONST) + if(typechl[l->type->etype]) { + cas(); + cases->val = l->vconst; + cases->def = 0; + cases->label = pc; + goto rloop; + } + diag(n, "case expression must be integer constant"); + goto rloop; + + case OSWITCH: + l = n->left; + complex(l); + if(l->type == T) + break; + if(!typechl[l->type->etype]) { + diag(n, "switch expression must be integer"); + break; + } + + gbranch(OGOTO); /* entry */ + sp = p; + + cn = cases; + cases = C; + cas(); + + sbc = breakpc; + breakpc = pc; + gbranch(OGOTO); + spb = p; + + gen(n->right); + gbranch(OGOTO); + patch(p, breakpc); + + patch(sp, pc); + regalloc(&nod, l, Z); + nod.type = types[TLONG]; + cgen(l, &nod); + doswit(&nod); + regfree(&nod); + patch(spb, pc); + + cases = cn; + breakpc = sbc; + break; + + case OWHILE: + case ODWHILE: + l = n->left; + gbranch(OGOTO); /* entry */ + sp = p; + + scc = continpc; + continpc = pc; + gbranch(OGOTO); + spc = p; + + sbc = breakpc; + breakpc = pc; + gbranch(OGOTO); + spb = p; + + patch(spc, pc); + if(n->op == OWHILE) + patch(sp, pc); + bcomplex(l); /* test */ + patch(p, breakpc); + + if(n->op == ODWHILE) + patch(sp, pc); + gen(n->right); /* body */ + gbranch(OGOTO); + patch(p, continpc); + + patch(spb, pc); + continpc = scc; + breakpc = sbc; + break; + + case OFOR: + l = n->left; + gen(l->right->left); /* init */ + gbranch(OGOTO); /* entry */ + sp = p; + + scc = continpc; + continpc = pc; + gbranch(OGOTO); + spc = p; + + sbc = breakpc; + breakpc = pc; + gbranch(OGOTO); + spb = p; + + patch(spc, pc); + gen(l->right->right); /* inc */ + patch(sp, pc); + if(l->left != Z) { /* test */ + bcomplex(l->left); + patch(p, breakpc); + } + gen(n->right); /* body */ + gbranch(OGOTO); + patch(p, continpc); + + patch(spb, pc); + continpc = scc; + breakpc = sbc; + break; + + case OCONTINUE: + if(continpc < 0) { + diag(n, "continue not in a loop"); + break; + } + gbranch(OGOTO); + patch(p, continpc); + break; + + case OBREAK: + if(breakpc < 0) { + diag(n, "break not in a loop"); + break; + } + gbranch(OGOTO); + patch(p, breakpc); + break; + + case OIF: + l = n->left; + bcomplex(l); + sp = p; + if(n->right->left != Z) + gen(n->right->left); + if(n->right->right != Z) { + gbranch(OGOTO); + patch(sp, pc); + sp = p; + gen(n->right->right); + } + patch(sp, pc); + break; + + case OSET: + case OUSED: + usedset(n->left, o); + break; + } +} + +void +usedset(Node *n, int o) +{ + if(n->op == OLIST) { + usedset(n->left, o); + usedset(n->right, o); + return; + } + complex(n); + switch(n->op) { + case OADDR: /* volatile */ + gins(ANOP, n, Z); + break; + case ONAME: + if(o == OSET) + gins(ANOP, Z, n); + else + gins(ANOP, n, Z); + break; + } +} + +void +noretval(int n) +{ + + if(n & 1) { + gins(ANOP, Z, Z); + p->to.type = D_REG; + p->to.reg = REGRET; + } + if(n & 2) { + gins(ANOP, Z, Z); + p->to.type = D_FREG; + p->to.reg = FREGRET; + } +} + +/* + * calculate addressability as follows + * CONST ==> 20 $value + * NAME ==> 10 name + * REGISTER ==> 11 register + * INDREG ==> 12 *[(reg)+offset] + * &10 ==> 2 $name + * ADD(2, 20) ==> 2 $name+offset + * ADD(3, 20) ==> 3 $(reg)+offset + * &12 ==> 3 $(reg)+offset + * *11 ==> 11 ?? + * *2 ==> 10 name + * *3 ==> 12 *(reg)+offset + * calculate complexity (number of registers) + */ +void +xcom(Node *n) +{ + Node *l, *r; + int t; + + if(n == Z) + return; + l = n->left; + r = n->right; + n->addable = 0; + n->complex = 0; + switch(n->op) { + case OCONST: + n->addable = 20; + return; + + case OREGISTER: + n->addable = 11; + return; + + case OINDREG: + n->addable = 12; + return; + + case ONAME: + n->addable = 10; + return; + + case OADDR: + xcom(l); + if(l->addable == 10) + n->addable = 2; + if(l->addable == 12) + n->addable = 3; + break; + + case OIND: + xcom(l); + if(l->addable == 11) + n->addable = 12; + if(l->addable == 3) + n->addable = 12; + if(l->addable == 2) + n->addable = 10; + break; + + case OADD: + xcom(l); + xcom(r); + if(l->addable == 20) { + if(r->addable == 2) + n->addable = 2; + if(r->addable == 3) + n->addable = 3; + } + if(r->addable == 20) { + if(l->addable == 2) + n->addable = 2; + if(l->addable == 3) + n->addable = 3; + } + break; + + case OASLMUL: + case OASMUL: + xcom(l); + xcom(r); + t = vlog(r); + if(t >= 0) { + n->op = OASASHL; + r->vconst = t; + r->type = types[TINT]; + } + break; + + case OMUL: + case OLMUL: + xcom(l); + xcom(r); + t = vlog(r); + if(t >= 0) { + n->op = OASHL; + r->vconst = t; + r->type = types[TINT]; + } + t = vlog(l); + if(t >= 0) { + n->op = OASHL; + n->left = r; + n->right = l; + r = l; + l = n->left; + r->vconst = t; + r->type = types[TINT]; + } + break; + + case OASLDIV: + xcom(l); + xcom(r); + t = vlog(r); + if(t >= 0) { + n->op = OASLSHR; + r->vconst = t; + r->type = types[TINT]; + } + break; + + case OLDIV: + xcom(l); + xcom(r); + t = vlog(r); + if(t >= 0) { + n->op = OLSHR; + r->vconst = t; + r->type = types[TINT]; + } + break; + + case OASLMOD: + xcom(l); + xcom(r); + t = vlog(r); + if(t >= 0) { + n->op = OASAND; + r->vconst--; + } + break; + + case OLMOD: + xcom(l); + xcom(r); + t = vlog(r); + if(t >= 0) { + n->op = OAND; + r->vconst--; + } + break; + + default: + if(l != Z) + xcom(l); + if(r != Z) + xcom(r); + break; + } + if(n->addable >= 10) + return; + + if(l != Z) + n->complex = l->complex; + if(r != Z) { + if(r->complex == n->complex) + n->complex = r->complex+1; + else + if(r->complex > n->complex) + n->complex = r->complex; + } + if(n->complex == 0) + n->complex++; + + switch(n->op) { + case OFUNC: + n->complex = FNX; + break; + + case OADD: + case OXOR: + case OAND: + case OOR: + case OEQ: + case ONE: + /* + * immediate operators, make const on right + */ + if(l->op == OCONST) { + n->left = r; + n->right = l; + } + break; + } +} + +void +bcomplex(Node *n) +{ + + complex(n); + if(n->type != T) + if(tcompat(n, T, n->type, tnot)) + n->type = T; + if(n->type != T) + boolgen(n, 1, Z); + else + gbranch(OGOTO); +} diff --git a/sys/src/cmd/4c/swt.c b/sys/src/cmd/4c/swt.c new file mode 100644 index 0000000..e109b19 --- /dev/null +++ b/sys/src/cmd/4c/swt.c @@ -0,0 +1,702 @@ +#include "gc.h" + +int +swcmp(const void *a1, const void *a2) +{ + C1 *p1, *p2; + + p1 = (C1*)a1; + p2 = (C1*)a2; + if(p1->val < p2->val) + return -1; + return p1->val > p2->val; +} + +void +doswit(Node *n) +{ + Case *c; + C1 *q, *iq; + long def, nc, i; + Node tn; + + def = 0; + nc = 0; + for(c = cases; c->link != C; c = c->link) { + if(c->def) { + if(def) + diag(n, "more than one default in switch"); + def = c->label; + continue; + } + nc++; + } + + iq = alloc(nc*sizeof(C1)); + q = iq; + for(c = cases; c->link != C; c = c->link) { + if(c->def) + continue; + q->label = c->label; + q->val = c->val; + q++; + } + qsort(iq, nc, sizeof(C1), swcmp); + if(debug['W']) + for(i=0; ival); + gmove(nodconst(q->val), tn); + gopcode(OEQ, n, tn, Z); + patch(p, q->label); + q++; + } + gbranch(OGOTO); + patch(p, def); + return; + } + i = nc / 2; + r = q+i; + if(debug['W']) + print("case > %.8lux\n", r->val); + gmove(nodconst(r->val), tn); + gopcode(OLT, tn, n, Z); + sp = p; + gopcode(OEQ, n, tn, Z); + patch(p, r->label); + swit1(q, i, def, n, tn); + + if(debug['W']) + print("case < %.8lux\n", r->val); + patch(sp, pc); + swit1(r+1, nc-i-1, def, n, tn); +} + +void +cas(void) +{ + Case *c; + + c = alloc(sizeof(*c)); + c->link = cases; + cases = c; +} + +void +bitload(Node *b, Node *n1, Node *n2, Node *n3, Node *nn) +{ + int sh; + long v; + Node *l; + + /* + * n1 gets adjusted/masked value + * n2 gets address of cell + * n3 gets contents of cell + */ + l = b->left; + if(n2 != Z) { + regalloc(n1, l, nn); + reglcgen(n2, l, Z); + regalloc(n3, l, Z); + gopcode(OAS, n2, Z, n3); + gopcode(OAS, n3, Z, n1); + } else { + regalloc(n1, l, nn); + cgen(l, n1); + } + if(b->type->shift == 0 && typeu[b->type->etype]) { + v = ~0 + (1L << b->type->nbits); + gopcode(OAND, nodconst(v), Z, n1); + } else { + sh = 32 - b->type->shift - b->type->nbits; + if(sh > 0) + gopcode(OASHL, nodconst(sh), Z, n1); + sh += b->type->shift; + if(sh > 0) + if(typeu[b->type->etype]) + gopcode(OLSHR, nodconst(sh), Z, n1); + else + gopcode(OASHR, nodconst(sh), Z, n1); + } +} + +void +bitstore(Node *b, Node *n1, Node *n2, Node *n3, Node *nn) +{ + long v; + Node nod, *l; + int sh; + + /* + * n1 has adjusted/masked value + * n2 has address of cell + * n3 has contents of cell + */ + l = b->left; + regalloc(&nod, l, Z); + v = ~0 + (1L << b->type->nbits); + gopcode(OAND, nodconst(v), Z, n1); + gopcode(OAS, n1, Z, &nod); + if(nn != Z) + gopcode(OAS, n1, Z, nn); + sh = b->type->shift; + if(sh > 0) + gopcode(OASHL, nodconst(sh), Z, &nod); + v <<= sh; + gopcode(OAND, nodconst(~v), Z, n3); + gopcode(OOR, n3, Z, &nod); + gopcode(OAS, &nod, Z, n2); + + regfree(&nod); + regfree(n1); + regfree(n2); + regfree(n3); +} + +long +outstring(char *s, long n) +{ + long r; + + r = nstring; + while(n) { + string[mnstring] = *s++; + mnstring++; + nstring++; + if(mnstring >= NSNAME) { + gpseudo(ADATA, symstring, nodconst(0L)); + p->from.offset += nstring - NSNAME; + p->reg = NSNAME; + p->to.type = D_SCONST; + memmove(p->to.sval, string, NSNAME); + mnstring = 0; + } + n--; + } + return r; +} + +long +outlstring(ushort *s, long n) +{ + char buf[2]; + int c; + long r; + + while(nstring & 1) + outstring("", 1); + r = nstring; + while(n > 0) { + c = *s++; + if(align(0, types[TCHAR], Aarg1)) { + buf[0] = c>>8; + buf[1] = c; + } else { + buf[0] = c; + buf[1] = c>>8; + } + outstring(buf, 2); + n -= sizeof(ushort); + } + return r; +} + +int +mulcon(Node *n, Node *nn) +{ + Node *l, *r, nod1, nod2; + Multab *m; + long v; + int o; + char code[sizeof(m->code)+2], *p; + + if(typefd[n->type->etype]) + return 0; + l = n->left; + r = n->right; + if(l->op == OCONST) { + l = r; + r = n->left; + } + if(r->op != OCONST) + return 0; + v = convvtox(r->vconst, n->type->etype); + if(v != r->vconst) { + if(debug['M']) + print("%L multiply conv: %lld\n", n->lineno, r->vconst); + return 0; + } + m = mulcon0(v); + if(!m) { + if(debug['M']) + print("%L multiply table: %lld\n", n->lineno, r->vconst); + return 0; + } + if(debug['M'] && debug['v']) + print("%L multiply: %ld\n", n->lineno, v); + + memmove(code, m->code, sizeof(m->code)); + code[sizeof(m->code)] = 0; + + p = code; + if(p[1] == 'i') + p += 2; + regalloc(&nod1, n, nn); + cgen(l, &nod1); + if(v < 0) + gopcode(OSUB, &nod1, nodconst(0), &nod1); + regalloc(&nod2, n, Z); + +loop: + switch(*p) { + case 0: + regfree(&nod2); + gopcode(OAS, &nod1, Z, nn); + regfree(&nod1); + return 1; + case '+': + o = OADD; + goto addsub; + case '-': + o = OSUB; + addsub: /* number is r,n,l */ + v = p[1] - '0'; + r = &nod1; + if(v&4) + r = &nod2; + n = &nod1; + if(v&2) + n = &nod2; + l = &nod1; + if(v&1) + l = &nod2; + gopcode(o, l, n, r); + break; + default: /* op is shiftcount, number is r,l */ + v = p[1] - '0'; + r = &nod1; + if(v&2) + r = &nod2; + l = &nod1; + if(v&1) + l = &nod2; + v = *p - 'a'; + if(v < 0 || v >= 32) { + diag(n, "mulcon unknown op: %c%c", p[0], p[1]); + break; + } + gopcode(OASHL, nodconst(v), l, r); + break; + } + p += 2; + goto loop; +} + +void +nullwarn(Node *l, Node *r) +{ + warn(Z, "result of operation not used"); + if(l != Z) + cgen(l, Z); + if(r != Z) + cgen(r, Z); +} + +void +sextern(Sym *s, Node *a, long o, long w) +{ + long e, lw; + + for(e=0; efrom.offset += o+e; + p->reg = lw; + p->to.type = D_SCONST; + memmove(p->to.sval, a->cstring+e, lw); + } +} + +void +gextern(Sym *s, Node *a, long o, long w) +{ + + if(a->op == OCONST && typev[a->type->etype]) { + gpseudo(ADATA, s, nodconst(a->vconst>>32)); + p->from.offset += o; + p->reg = 4; + gpseudo(ADATA, s, nodconst(a->vconst)); + p->from.offset += o + 4; + p->reg = 4; + return; + } + gpseudo(ADATA, s, a); + p->from.offset += o; + p->reg = w; + if(p->to.type == D_OREG) + p->to.type = D_CONST; +} + +void zname(Biobuf*, char*, int, int); +char* zaddr(char*, Adr*, int); +void zwrite(Biobuf*, Prog*, int, int); +void outhist(Biobuf*); + +void +zwrite(Biobuf *b, Prog *p, int sf, int st) +{ + char bf[100], *bp; + + bf[0] = p->as; + bf[1] = p->reg; + bf[2] = p->lineno; + bf[3] = p->lineno>>8; + bf[4] = p->lineno>>16; + bf[5] = p->lineno>>24; + bp = zaddr(bf+6, &p->from, sf); + bp = zaddr(bp, &p->to, st); + Bwrite(b, bf, bp-bf); +} + +void +outcode(void) +{ + struct { Sym *sym; short type; } h[NSYM]; + Prog *p; + Sym *s; + int sf, st, t, sym; + + if(debug['S']) { + for(p = firstp; p != P; p = p->link) + if(p->as != ADATA && p->as != AGLOBL) + pc--; + for(p = firstp; p != P; p = p->link) { + print("%P\n", p); + if(p->as != ADATA && p->as != AGLOBL) + pc++; + } + } + outhist(&outbuf); + for(sym=0; symlink) { + jackpot: + sf = 0; + s = p->from.sym; + while(s != S) { + sf = s->sym; + if(sf < 0 || sf >= NSYM) + sf = 0; + t = p->from.name; + if(h[sf].type == t) + if(h[sf].sym == s) + break; + zname(&outbuf, s->name, t, sym); + s->sym = sym; + h[sym].sym = s; + h[sym].type = t; + sf = sym; + sym++; + if(sym >= NSYM) + sym = 1; + break; + } + st = 0; + s = p->to.sym; + while(s != S) { + st = s->sym; + if(st < 0 || st >= NSYM) + st = 0; + t = p->to.name; + if(h[st].type == t) + if(h[st].sym == s) + break; + zname(&outbuf, s->name, t, sym); + s->sym = sym; + h[sym].sym = s; + h[sym].type = t; + st = sym; + sym++; + if(sym >= NSYM) + sym = 1; + if(st == sf) + goto jackpot; + break; + } + zwrite(&outbuf, p, sf, st); + } + firstp = P; + lastp = P; +} + +void +outhist(Biobuf *b) +{ + Hist *h; + char *p, *q, *op, c; + Prog pg; + int n; + + pg = zprog; + pg.as = AHISTORY; + c = pathchar(); + for(h = hist; h != H; h = h->link) { + p = h->name; + op = 0; + if(p && p[0] != c && h->offset == 0 && pathname){ + /* on windows skip drive specifier in pathname */ + if(systemtype(Windows) && pathname[2] == c) { + op = p; + p = pathname+2; + *p = '/'; + } else if(pathname[0] == c){ + op = p; + p = pathname; + } + } + while(p) { + q = utfrune(p, c); + if(q) { + n = q-p; + if(n == 0) + n = 1; /* leading "/" */ + q++; + } else { + n = strlen(p); + q = 0; + } + if(n) { + Bputc(b, ANAME); + Bputc(b, D_FILE); + Bputc(b, 1); + Bputc(b, '<'); + Bwrite(b, p, n); + Bputc(b, 0); + } + p = q; + if(p == 0 && op) { + p = op; + op = 0; + } + } + pg.lineno = h->line; + pg.to.type = zprog.to.type; + pg.to.offset = h->offset; + if(h->offset) + pg.to.type = D_CONST; + + zwrite(b, &pg, 0, 0); + } +} + +void +zname(Biobuf *b, char *n, int t, int s) +{ + char bf[3]; + + bf[0] = ANAME; + bf[1] = t; /* type */ + bf[2] = s; /* sym */ + Bwrite(b, bf, 3); + Bwrite(b, n, strlen(n)+1); +} + +char* +zaddr(char *bp, Adr *a, int s) +{ + vlong v; + long l; + Ieee e; + + bp[0] = a->type; + bp[1] = a->reg; + bp[2] = s; + bp[3] = a->name; + bp += 4; + switch(a->type) { + default: + diag(Z, "unknown type %d in zaddr", a->type); + + case D_NONE: + case D_REG: + case D_FREG: + case D_MREG: + case D_FCREG: + case D_LO: + case D_HI: + break; + + case D_CONST: + case D_OREG: + case D_BRANCH: + l = a->offset; + bp[0] = l; + bp[1] = l>>8; + bp[2] = l>>16; + bp[3] = l>>24; + bp += 4; + break; + + case D_SCONST: + memmove(bp, a->sval, NSNAME); + bp += NSNAME; + break; + + case D_FCONST: + ieeedtod(&e, a->dval); + l = e.l; + bp[0] = l; + bp[1] = l>>8; + bp[2] = l>>16; + bp[3] = l>>24; + bp += 4; + l = e.h; + bp[0] = l; + bp[1] = l>>8; + bp[2] = l>>16; + bp[3] = l>>24; + bp += 4; + break; + + case D_VCONST: + v = a->vval; + bp[0] = v; + bp[1] = v>>8; + bp[2] = v>>16; + bp[3] = v>>24; + bp[4] = v>>32; + bp[5] = v>>40; + bp[6] = v>>48; + bp[7] = v>>56; + bp += 8; + break; + } + return bp; +} + +void +ieeedtod(Ieee *ieee, double native) +{ + double fr, ho, f; + int exp; + + if(native < 0) { + ieeedtod(ieee, -native); + ieee->h |= 0x80000000L; + return; + } + if(native == 0) { + ieee->l = 0; + ieee->h = 0; + return; + } + fr = frexp(native, &exp); + f = 2097152L; /* shouldnt use fp constants here */ + fr = modf(fr*f, &ho); + ieee->h = ho; + ieee->h &= 0xfffffL; + ieee->h |= (exp+1022L) << 20; + f = 65536L; + fr = modf(fr*f, &ho); + ieee->l = ho; + ieee->l <<= 16; + ieee->l |= (long)(fr*f); +} + +long +align(long i, Type *t, int op) +{ + long o; + Type *v; + int w; + + o = i; + w = 1; + switch(op) { + default: + diag(Z, "unknown align opcode %d", op); + break; + + case Asu2: /* padding at end of a struct */ + w = SZ_VLONG; + break; + + case Ael1: /* initial allign of struct element */ + for(v=t; v->etype==TARRAY; v=v->link) + ; + w = ewidth[v->etype]; + if(w <= 0 || w >= SZ_VLONG) + w = SZ_VLONG; + break; + + case Ael2: /* width of a struct element */ + o += t->width; + break; + + case Aarg0: /* initial passbyptr argument in arg list */ + if(typesu[t->etype]) { + o = align(o, types[TIND], Aarg1); + o = align(o, types[TIND], Aarg2); + } + break; + + case Aarg1: /* initial allign of parameter */ + w = ewidth[t->etype]; + if(w <= 0 || w >= SZ_VLONG) { + w = SZ_VLONG; + break; + } + o += SZ_LONG - w; /* big endian adjustment */ + w = 1; + break; + + case Aarg2: /* width of a parameter */ + o += t->width; + w = SZ_LONG; + break; + + case Aaut3: /* total allign of automatic */ + o = align(o, t, Ael1); + o = align(o, t, Ael2); + break; + } + o = round(o, w); + if(debug['A']) + print("align %s %ld %T = %ld\n", bnames[op], i, t, o); + return o; +} + +long +maxround(long max, long v) +{ + v += SZ_VLONG-1; + if(v > max) + max = round(v, SZ_VLONG); + return max; +} diff --git a/sys/src/cmd/4c/txt.c b/sys/src/cmd/4c/txt.c new file mode 100644 index 0000000..b8228cc --- /dev/null +++ b/sys/src/cmd/4c/txt.c @@ -0,0 +1,1459 @@ +#include "gc.h" + +void +ginit(void) +{ + int i; + Type *t; + + thechar = '4'; + thestring = "mips64"; + exregoffset = REGEXT; + exfregoffset = FREGEXT; + listinit(); + nstring = 0; + mnstring = 0; + nrathole = 0; + pc = 0; + breakpc = -1; + continpc = -1; + cases = C; + firstp = P; + lastp = P; + tfield = types[TLONG]; + + zprog.link = P; + zprog.as = AGOK; + zprog.reg = NREG; + zprog.from.type = D_NONE; + zprog.from.name = D_NONE; + zprog.from.reg = NREG; + zprog.to = zprog.from; + + regnode.op = OREGISTER; + regnode.class = CEXREG; + regnode.reg = REGTMP; + regnode.complex = 0; + regnode.addable = 11; + regnode.type = types[TLONG]; + + constnode.op = OCONST; + constnode.class = CXXX; + constnode.complex = 0; + constnode.addable = 20; + constnode.type = types[TLONG]; + + fconstnode.op = OCONST; + fconstnode.class = CXXX; + fconstnode.complex = 0; + fconstnode.addable = 20; + fconstnode.type = types[TDOUBLE]; + + nodsafe = new(ONAME, Z, Z); + nodsafe->sym = slookup(".safe"); + nodsafe->type = types[TINT]; + nodsafe->etype = types[TINT]->etype; + nodsafe->class = CAUTO; + complex(nodsafe); + + t = typ(TARRAY, types[TCHAR]); + symrathole = slookup(".rathole"); + symrathole->class = CGLOBL; + symrathole->type = t; + + nodrat = new(ONAME, Z, Z); + nodrat->sym = symrathole; + nodrat->type = types[TIND]; + nodrat->etype = TVOID; + nodrat->class = CGLOBL; + complex(nodrat); + nodrat->type = t; + + nodret = new(ONAME, Z, Z); + nodret->sym = slookup(".ret"); + nodret->type = types[TIND]; + nodret->etype = TIND; + nodret->class = CPARAM; + nodret = new(OIND, nodret, Z); + complex(nodret); + + memset(reg, 0, sizeof(reg)); + for(i=NREG; itype->width = nstring; + symrathole->type->width = nrathole; + for(i=0; ilink) { + if(s->type == T) + continue; + if(s->type->width == 0) + continue; + if(s->class != CGLOBL && s->class != CSTATIC) + continue; + if(s->type == types[TENUM]) + continue; + gpseudo(AGLOBL, s, nodconst(s->type->width)); + } + nextpc(); + p->as = AEND; + outcode(); +} + +void +nextpc(void) +{ + + p = alloc(sizeof(*p)); + *p = zprog; + p->lineno = nearln; + pc++; + if(firstp == P) { + firstp = p; + lastp = p; + return; + } + lastp->link = p; + lastp = p; +} + +void +gargs(Node *n, Node *tn1, Node *tn2) +{ + long regs; + Node fnxargs[20], *fnxp; + + regs = cursafe; + + fnxp = fnxargs; + garg1(n, tn1, tn2, 0, &fnxp); /* compile fns to temps */ + + curarg = 0; + fnxp = fnxargs; + garg1(n, tn1, tn2, 1, &fnxp); /* compile normal args and temps */ + + cursafe = regs; +} + +void +garg1(Node *n, Node *tn1, Node *tn2, int f, Node **fnxp) +{ + Node nod; + + if(n == Z) + return; + if(n->op == OLIST) { + garg1(n->left, tn1, tn2, f, fnxp); + garg1(n->right, tn1, tn2, f, fnxp); + return; + } + if(f == 0) { + if(n->complex >= FNX) { + regsalloc(*fnxp, n); + nod = znode; + nod.op = OAS; + nod.left = *fnxp; + nod.right = n; + nod.type = n->type; + cgen(&nod, Z); + (*fnxp)++; + } + return; + } + if(typesu[n->type->etype]) { + regaalloc(tn2, n); + if(n->complex >= FNX) { + sugen(*fnxp, tn2, n->type->width); + (*fnxp)++; + } else + sugen(n, tn2, n->type->width); + return; + } + if(REGARG && curarg == 0 && typechlp[n->type->etype]) { + regaalloc1(tn1, n); + if(n->complex >= FNX) { + cgen(*fnxp, tn1); + (*fnxp)++; + } else + cgen(n, tn1); + return; + } + if(vconst(n) == 0) { + regaalloc(tn2, n); + gopcode(OAS, n, Z, tn2); + return; + } + regalloc(tn1, n, Z); + if(n->complex >= FNX) { + cgen(*fnxp, tn1); + (*fnxp)++; + } else + cgen(n, tn1); + regaalloc(tn2, n); + gopcode(OAS, tn1, Z, tn2); + regfree(tn1); +} + +Node* +nodconst(long v) +{ + constnode.vconst = v; + return &constnode; +} + +Node* +nodfconst(double d) +{ + fconstnode.fconst = d; + return &fconstnode; +} + +void +nodreg(Node *n, Node *nn, int reg) +{ + *n = regnode; + n->reg = reg; + n->type = nn->type; + n->lineno = nn->lineno; +} + +void +regret(Node *n, Node *nn) +{ + int r; + + r = REGRET; + if(typefd[nn->type->etype]) + r = FREGRET+NREG; + nodreg(n, nn, r); + reg[r]++; +} + +int +tmpreg(void) +{ + int i; + + for(i=REGRET+1; itype->etype) { + case TCHAR: + case TUCHAR: + case TSHORT: + case TUSHORT: + case TINT: + case TUINT: + case TLONG: + case TULONG: + case TIND: + case TUVLONG: + case TVLONG: + if(o != Z && o->op == OREGISTER) { + i = o->reg; + if(i > 0 && i < NREG) + goto out; + } + j = lasti + REGRET+1; + for(i=REGRET+1; i= NREG) + j = REGRET+1; + if(reg[j] == 0) { + i = j; + goto out; + } + j++; + } + diag(tn, "out of fixed registers"); + goto err; + + case TFLOAT: + case TDOUBLE: + if(o != Z && o->op == OREGISTER) { + i = o->reg; + if(i >= NREG && i < NREG+NREG) + goto out; + } + j = 0*2 + NREG; + for(i=NREG; i= NREG+NREG) + j = NREG; + if(reg[j] == 0) { + i = j; + goto out; + } + j += 2; + } + diag(tn, "out of float registers"); + goto err; + } + diag(tn, "unknown type in regalloc: %T", tn->type); +err: + i = 0; +out: + if(i) + reg[i]++; + lasti++; + if(lasti >= 5) + lasti = 0; + nodreg(n, tn, i); +} + +void +regialloc(Node *n, Node *tn, Node *o) +{ + Node nod; + + nod = *tn; + nod.type = types[TIND]; + regalloc(n, &nod, o); +} + +void +regfree(Node *n) +{ + int i; + + i = 0; + if(n->op != OREGISTER && n->op != OINDREG) + goto err; + i = n->reg; + if(i < 0 || i >= sizeof(reg)) + goto err; + if(reg[i] <= 0) + goto err; + reg[i]--; + return; +err: + diag(n, "error in regfree: %d", i); +} + +void +regsalloc(Node *n, Node *nn) +{ + cursafe = align(cursafe, nn->type, Aaut3); + maxargsafe = maxround(maxargsafe, cursafe+curarg); + *n = *nodsafe; + n->xoffset = -(stkoff + cursafe); + n->type = nn->type; + n->etype = nn->type->etype; + n->lineno = nn->lineno; +} + +void +regaalloc1(Node *n, Node *nn) +{ + nodreg(n, nn, REGARG); + reg[REGARG]++; + curarg = align(curarg, nn->type, Aarg1); + curarg = align(curarg, nn->type, Aarg2); + maxargsafe = maxround(maxargsafe, cursafe+curarg); +} + +void +regaalloc(Node *n, Node *nn) +{ + curarg = align(curarg, nn->type, Aarg1); + *n = *nn; + n->op = OINDREG; + n->reg = REGSP; + n->xoffset = curarg + SZ_VLONG; + n->complex = 0; + n->addable = 20; + curarg = align(curarg, nn->type, Aarg2); + maxargsafe = maxround(maxargsafe, cursafe+curarg); +} + +void +regind(Node *n, Node *nn) +{ + + if(n->op != OREGISTER) { + diag(n, "regind not OREGISTER"); + return; + } + n->op = OINDREG; + n->type = nn->type; +} + +void +raddr(Node *n, Prog *p) +{ + Adr a; + + naddr(n, &a); + if(a.type == D_CONST && a.offset == 0) { + a.type = D_REG; + a.reg = 0; + } + if(a.type != D_REG && a.type != D_FREG) { + if(n) + diag(n, "bad in raddr: %O", n->op); + else + diag(n, "bad in raddr: "); + p->reg = NREG; + } else + p->reg = a.reg; +} + +void +naddr(Node *n, Adr *a) +{ + long v; + + a->type = D_NONE; + if(n == Z) + return; + switch(n->op) { + default: + bad: + diag(n, "bad in naddr: %O", n->op); + break; + + case OREGISTER: + a->type = D_REG; + a->sym = S; + a->reg = n->reg; + if(a->reg >= NREG) { + a->type = D_FREG; + a->reg -= NREG; + } + break; + + case OIND: + naddr(n->left, a); + if(a->type == D_REG) { + a->type = D_OREG; + break; + } + if(a->type == D_CONST) { + a->type = D_OREG; + break; + } + goto bad; + + case OINDREG: + a->type = D_OREG; + a->sym = S; + a->offset = n->xoffset; + a->reg = n->reg; + break; + + case ONAME: + a->etype = n->etype; + a->type = D_OREG; + a->name = D_STATIC; + a->sym = n->sym; + a->offset = n->xoffset; + if(n->class == CSTATIC) + break; + if(n->class == CEXTERN || n->class == CGLOBL) { + a->name = D_EXTERN; + break; + } + if(n->class == CAUTO) { + a->name = D_AUTO; + break; + } + if(n->class == CPARAM) { + a->name = D_PARAM; + break; + } + goto bad; + + case OCONST: + a->sym = S; + a->reg = NREG; + if(typefd[n->type->etype]) { + a->type = D_FCONST; + a->dval = n->fconst; + } else + if(llconst(n)) { + a->type = D_VCONST; + a->vval = n->vconst; + } else { + a->type = D_CONST; + a->offset = n->vconst; + } + break; + + case OADDR: + naddr(n->left, a); + if(a->type == D_OREG) { + a->type = D_CONST; + break; + } + goto bad; + + case OADD: + if(n->left->op == OCONST) { + naddr(n->left, a); + v = a->offset; + naddr(n->right, a); + } else { + naddr(n->right, a); + v = a->offset; + naddr(n->left, a); + } + a->offset += v; + break; + + } +} + +void +fop(int as, int f1, int f2, Node *t) +{ + Node nod1, nod2, nod3; + + nodreg(&nod1, t, NREG+f1); + nodreg(&nod2, t, NREG+f2); + regalloc(&nod3, t, t); + gopcode(as, &nod1, &nod2, &nod3); + gmove(&nod3, t); + regfree(&nod3); +} + +void +gmove(Node *f, Node *t) +{ + int ft, tt, a; + Node nod; + Prog *p1; + double d; + + ft = f->type->etype; + tt = t->type->etype; + + if(ft == TDOUBLE && f->op == OCONST) { + d = f->fconst; + if(d == 0.0) { + a = FREGZERO; + goto ffreg; + } + if(d == 0.5) { + a = FREGHALF; + goto ffreg; + } + if(d == 1.0) { + a = FREGONE; + goto ffreg; + } + if(d == 2.0) { + a = FREGTWO; + goto ffreg; + } + if(d == -.5) { + fop(OSUB, FREGHALF, FREGZERO, t); + return; + } + if(d == -1.0) { + fop(OSUB, FREGONE, FREGZERO, t); + return; + } + if(d == -2.0) { + fop(OSUB, FREGTWO, FREGZERO, t); + return; + } + if(d == 1.5) { + fop(OADD, FREGONE, FREGHALF, t); + return; + } + if(d == 2.5) { + fop(OADD, FREGTWO, FREGHALF, t); + return; + } + if(d == 3.0) { + fop(OADD, FREGTWO, FREGONE, t); + return; + } + } + if(ft == TFLOAT && f->op == OCONST) { + d = f->fconst; + if(d == 0) { + a = FREGZERO; + ffreg: + nodreg(&nod, f, NREG+a); + gmove(&nod, t); + return; + } + } + /* + * a load -- + * put it into a register then + * worry what to do with it. + */ + if(f->op == ONAME || f->op == OINDREG || f->op == OIND) { + switch(ft) { + default: + if(typefd[tt]) { + /* special case can load mem to Freg */ + regalloc(&nod, t, t); + gins(AMOVW, f, &nod); + a = AMOVWD; + if(tt == TFLOAT) + a = AMOVWF; + gins(a, &nod, &nod); + gmove(&nod, t); + regfree(&nod); + return; + } + a = AMOVW; + break; + case TCHAR: + a = AMOVB; + break; + case TUCHAR: + a = AMOVBU; + break; + case TSHORT: + a = AMOVH; + break; + case TUSHORT: + a = AMOVHU; + break; + case TFLOAT: + a = AMOVF; + break; + case TDOUBLE: + a = AMOVD; + break; + case TUVLONG: + case TVLONG: + case TIND: + a = AMOVV; + break; + } + if(typechlp[ft] && typeilp[tt]) + regalloc(&nod, t, t); + else + regalloc(&nod, f, t); + gins(a, f, &nod); + gmove(&nod, t); + regfree(&nod); + return; + } + + /* + * a store -- + * put it into a register then + * store it. + */ + if(t->op == ONAME || t->op == OINDREG || t->op == OIND) { + switch(tt) { + default: + a = AMOVW; + break; + case TUCHAR: + case TCHAR: + a = AMOVB; + break; + case TUSHORT: + case TSHORT: + a = AMOVH; + break; + case TFLOAT: + a = AMOVF; + break; + case TDOUBLE: + a = AMOVD; + break; + case TUVLONG: + case TVLONG: + case TIND: + a = AMOVV; + break; + } + if(!typefd[ft] && vconst(f) == 0) { + gins(a, f, t); + return; + } + if(ft == tt) + regalloc(&nod, t, f); + else + regalloc(&nod, t, Z); + gmove(f, &nod); + gins(a, &nod, t); + regfree(&nod); + return; + } + + /* + * type x type cross table + */ + a = AGOK; + switch(ft) { + case TUVLONG: + case TVLONG: + case TIND: + switch(tt) { + case TUVLONG: + case TVLONG: + case TIND: + a = AMOVV; + break; + case TINT: + case TUINT: + case TLONG: + case TULONG: + case TSHORT: + case TUSHORT: + case TCHAR: + case TUCHAR: + a = AMOVW; + break; + case TDOUBLE: + gins(AMOVW, f, t); + gins(AMOVWD, t, t); + return; + case TFLOAT: + gins(AMOVW, f, t); + gins(AMOVWF, t, t); + return; + } + break; + case TDOUBLE: + case TFLOAT: + switch(tt) { + case TDOUBLE: + a = AMOVD; + if(ft == TFLOAT) + a = AMOVFD; + break; + case TFLOAT: + a = AMOVDF; + if(ft == TFLOAT) + a = AMOVF; + break; + case TINT: + case TUINT: + case TLONG: + case TULONG: + case TSHORT: + case TUSHORT: + case TCHAR: + case TUCHAR: + regalloc(&nod, f, Z); + gins(ATRUNCDW, f, &nod); + if(ft == TFLOAT) + p->as = ATRUNCFW; + gins(AMOVW, &nod, t); + regfree(&nod); + return; + case TUVLONG: + case TVLONG: + case TIND: + regalloc(&nod, f, Z); + gins(ATRUNCDV, f, &nod); + if(ft == TFLOAT) + p->as = ATRUNCFV; + gins(AMOVV, &nod, t); + regfree(&nod); + return; + } + break; + case TINT: + case TUINT: + case TLONG: + case TULONG: + switch(tt) { + case TDOUBLE: + gins(AMOVW, f, t); + gins(AMOVWD, t, t); + if(ft == TULONG || ft == TUINT) { + regalloc(&nod, t, Z); + gins(ACMPGED, t, Z); + p->reg = FREGZERO; + gins(ABFPT, Z, Z); + p1 = p; + gins(AMOVD, nodfconst(4294967296.), &nod); + gins(AADDD, &nod, t); + patch(p1, pc); + regfree(&nod); + } + return; + case TFLOAT: + gins(AMOVW, f, t); + gins(AMOVWF, t, t); + if(ft == TULONG || ft == TUINT) { + regalloc(&nod, t, Z); + gins(ACMPGEF, t, Z); + p->reg = FREGZERO; + gins(ABFPT, Z, Z); + p1 = p; + gins(AMOVF, nodfconst(4294967296.), &nod); + gins(AADDF, &nod, t); + patch(p1, pc); + regfree(&nod); + } + return; + case TUVLONG: + case TVLONG: + case TIND: + if(ft == TULONG || ft == TUINT) { + a = AMOVWU; + break; + } + case TINT: + case TUINT: + case TLONG: + case TULONG: + case TSHORT: + case TUSHORT: + case TCHAR: + case TUCHAR: + a = AMOVW; + break; + } + break; + case TSHORT: + switch(tt) { + case TDOUBLE: + regalloc(&nod, f, Z); + gins(AMOVH, f, &nod); + gins(AMOVW, &nod, t); + gins(AMOVWD, t, t); + regfree(&nod); + return; + case TFLOAT: + regalloc(&nod, f, Z); + gins(AMOVH, f, &nod); + gins(AMOVW, &nod, t); + gins(AMOVWF, t, t); + regfree(&nod); + return; + case TINT: + case TUINT: + case TLONG: + case TULONG: + case TVLONG: + case TUVLONG: + case TIND: + a = AMOVH; + break; + case TSHORT: + case TUSHORT: + case TCHAR: + case TUCHAR: + a = AMOVW; + break; + } + break; + case TUSHORT: + switch(tt) { + case TDOUBLE: + regalloc(&nod, f, Z); + gins(AMOVHU, f, &nod); + gins(AMOVW, &nod, t); + gins(AMOVWD, t, t); + regfree(&nod); + return; + case TFLOAT: + regalloc(&nod, f, Z); + gins(AMOVHU, f, &nod); + gins(AMOVW, &nod, t); + gins(AMOVWF, t, t); + regfree(&nod); + return; + case TINT: + case TUINT: + case TLONG: + case TULONG: + case TVLONG: + case TUVLONG: + case TIND: + a = AMOVHU; + break; + case TSHORT: + case TUSHORT: + case TCHAR: + case TUCHAR: + a = AMOVW; + break; + } + break; + case TCHAR: + switch(tt) { + case TDOUBLE: + regalloc(&nod, f, Z); + gins(AMOVB, f, &nod); + gins(AMOVW, &nod, t); + gins(AMOVWD, t, t); + regfree(&nod); + return; + case TFLOAT: + regalloc(&nod, f, Z); + gins(AMOVB, f, &nod); + gins(AMOVW, &nod, t); + gins(AMOVWF, t, t); + regfree(&nod); + return; + case TINT: + case TUINT: + case TLONG: + case TULONG: + case TVLONG: + case TUVLONG: + case TIND: + case TSHORT: + case TUSHORT: + a = AMOVB; + break; + case TCHAR: + case TUCHAR: + a = AMOVW; + break; + } + break; + case TUCHAR: + switch(tt) { + case TDOUBLE: + regalloc(&nod, f, Z); + gins(AMOVBU, f, &nod); + gins(AMOVW, &nod, t); + gins(AMOVWD, t, t); + regfree(&nod); + return; + case TFLOAT: + regalloc(&nod, f, Z); + gins(AMOVBU, f, &nod); + gins(AMOVW, &nod, t); + gins(AMOVWF, t, t); + regfree(&nod); + return; + case TINT: + case TUINT: + case TLONG: + case TULONG: + case TVLONG: + case TUVLONG: + case TIND: + case TSHORT: + case TUSHORT: + a = AMOVBU; + break; + case TCHAR: + case TUCHAR: + a = AMOVW; + break; + } + break; + } + if(a == AGOK) + diag(Z, "bad opcode in gmove %T -> %T", f->type, t->type); + if(a == AMOVW || a == AMOVF || a == AMOVD || a == AMOVV) + if(samaddr(f, t)) + return; + gins(a, f, t); +} + +void +gins(int a, Node *f, Node *t) +{ + + nextpc(); + p->as = a; + if(f != Z) + naddr(f, &p->from); + if(t != Z) + naddr(t, &p->to); + if(debug['g']) + print("%P\n", p); +} + +void +gopcode(int o, Node *f1, Node *f2, Node *t) +{ + int a, et, ett; + Adr ta; + Node nod; + + et = TLONG; + if(f1 != Z && f1->type != T) + et = f1->type->etype; + ett = TLONG; + if(t != Z && t->type != T) + ett = t->type->etype; + if(llconst(f1) && o != OAS) { + regalloc(&nod, f1, Z); + gmove(f1, &nod); + gopcode(o, &nod, f2, t); + regfree(&nod); + return; + } + a = AGOK; + switch(o) { + case OAS: + gmove(f1, t); + return; + + case OASADD: + case OADD: + a = AADDU; + if(et == TVLONG || et == TUVLONG || et == TIND) + a = AADDVU; + else + if(et == TFLOAT) + a = AADDF; + else + if(et == TDOUBLE) + a = AADDD; + break; + + case OASSUB: + case OSUB: + a = ASUBU; + if(et == TVLONG || et == TUVLONG || et == TIND) + a = ASUBVU; + else + if(et == TFLOAT) + a = ASUBF; + else + if(et == TDOUBLE) + a = ASUBD; + break; + + case OASOR: + case OOR: + a = AOR; + break; + + case OASAND: + case OAND: + a = AAND; + break; + + case OASXOR: + case OXOR: + a = AXOR; + break; + + case OASLSHR: + case OLSHR: + a = ASRL; + if(ett == TVLONG || ett == TUVLONG || et == TIND) + a = ASRLV; + break; + + case OASASHR: + case OASHR: + a = ASRA; + if(ett == TVLONG || ett == TUVLONG || et == TIND) + a = ASRAV; + break; + + case OASASHL: + case OASHL: + a = ASLL; + if(ett == TVLONG || ett == TUVLONG || et == TIND) + a = ASLLV; + break; + + case OFUNC: + a = AJAL; + break; + + case OCOND: + a = ASGTU; + break; + + case OCOMMA: + a = ASGT; + break; + + case OASMUL: + case OMUL: + if(et == TFLOAT) { + a = AMULF; + break; + } else + if(et == TDOUBLE) { + a = AMULD; + break; + } + a = AMUL; + if(et == TVLONG || et == TUVLONG || et == TIND) + a = AMULV; + goto muldiv; + + case OASDIV: + case ODIV: + if(et == TFLOAT) { + a = ADIVF; + break; + } else + if(et == TDOUBLE) { + a = ADIVD; + break; + } + a = ADIV; + if(et == TVLONG || et == TUVLONG || et == TIND) + a = ADIVV; + goto muldiv; + + case OASMOD: + case OMOD: + a = ADIV; + o = OMOD; + if(et == TVLONG || et == TUVLONG || et == TIND) + a = ADIVV; + goto muldiv; + + case OASLMUL: + case OLMUL: + a = AMULU; + if(et == TVLONG || et == TUVLONG || et == TIND) + a = AMULVU; + goto muldiv; + + case OASLMOD: + case OLMOD: + o = OMOD; + + case OASLDIV: + case OLDIV: + a = ADIVU; + if(et == TVLONG || et == TUVLONG || et == TIND) + a = ADIVVU; + goto muldiv; + + muldiv: + nextpc(); + naddr(f1, &p->from); + if(f2 == Z) + raddr(t, p); + else + raddr(f2, p); + p->as = a; + if(debug['g']) + print("%P\n", p); + nextpc(); + p->as = AMOVW; + if(et == TVLONG || et == TUVLONG || et == TIND) + p->as = AMOVV; + a = D_LO; + if(o == OMOD) + a = D_HI; + p->from.type = a; + naddr(t, &p->to); + if(debug['g']) + print("%P\n", p); + return; + + case OEQ: + if(!typefd[et]) { + a = ABEQ; + break; + } + + case ONE: + if(!typefd[et]) { + a = ABNE; + break; + } + + case OLT: + case OLE: + case OGE: + case OGT: + if(typefd[et]) { + nextpc(); + if(et == TFLOAT) { + a = ACMPGTF; + if(o == OEQ || o == ONE) + a = ACMPEQF; + else + if(o == OLT || o == OGE) + a = ACMPGEF; + } else { + a = ACMPGTD; + if(o == OEQ || o == ONE) + a = ACMPEQD; + else + if(o == OLT || o == OGE) + a = ACMPGED; + } + p->as = a; + naddr(f1, &p->from); + raddr(f2, p); + if(debug['g']) + print("%P\n", p); + nextpc(); + a = ABFPF; + if(o == OEQ || o == OGE || o == OGT) + a = ABFPT; + p->as = a; + if(debug['g']) + print("%P\n", p); + return; + } + if(vconst(f1) == 0 || vconst(f2) == 0) { + if(vconst(f1) == 0) { + o = invrel[relindex(o)]; + f1 = f2; + } + switch(o) { + case OLT: + a = ABLTZ; + break; + case OLE: + a = ABLEZ; + break; + case OGE: + a = ABGEZ; + break; + case OGT: + a = ABGTZ; + break; + } + f2 = Z; + break; + } + + case OLO: + case OLS: + case OHS: + case OHI: + nextpc(); + if(o == OLE || o == OGT || o == OLS || o == OHI) { + naddr(f1, &p->from); + raddr(f2, p); + } else { + naddr(f2, &p->from); + raddr(f1, p); + } + naddr(®node, &p->to); + p->to.reg = tmpreg(); + a = ASGT; + if(o == OLO || o == OLS || o == OHS || o == OHI) + a = ASGTU; + p->as = a; + if(debug['g']) + print("%P\n", p); + + nextpc(); + naddr(®node, &p->from); + p->from.reg = tmpreg(); + a = ABEQ; + if(o == OLT || o == OGT || o == OLO || o == OHI) + a = ABNE; + p->as = a; + if(debug['g']) + print("%P\n", p); + return; + } + if(a == AGOK) + diag(Z, "bad in gopcode %O", o); + nextpc(); + p->as = a; + if(f1 != Z) + naddr(f1, &p->from); + if(f2 != Z) { + naddr(f2, &ta); + p->reg = ta.reg; + if(ta.type == D_CONST && ta.offset == 0) + p->reg = REGZERO; + } + if(t != Z) + naddr(t, &p->to); + if(debug['g']) + print("%P\n", p); +} + +int +samaddr(Node *f, Node *t) +{ + + if(f->op != t->op) + return 0; + switch(f->op) { + + case OREGISTER: + if(f->reg != t->reg) + break; + return 1; + } + return 0; +} + +void +gbranch(int o) +{ + int a; + + a = AGOK; + switch(o) { + case ORETURN: + a = ARET; + break; + case OGOTO: + a = AJMP; + break; + } + nextpc(); + if(a == AGOK) { + diag(Z, "bad in gbranch %O", o); + nextpc(); + } + p->as = a; +} + +void +patch(Prog *op, vlong pc) +{ + + op->to.offset = pc; + op->to.type = D_BRANCH; +} + +void +gpseudo(int a, Sym *s, Node *n) +{ + + nextpc(); + p->as = a; + p->from.type = D_OREG; + p->from.sym = s; + p->reg = (profileflg ? 0 : NOPROF); + p->from.name = D_EXTERN; + if(s->class == CSTATIC) + p->from.name = D_STATIC; + naddr(n, &p->to); + if(a == ADATA || a == AGLOBL) + pc--; +} + +int +sconst(Node *n) +{ + vlong vv; + + if(n->op == OCONST) { + if(!typefd[n->type->etype]) { + vv = n->vconst; + if(vv >= -32766LL && vv < 32766LL) + return 1; + } + } + return 0; +} + +int +llconst(Node *n) +{ + vlong vv; + + if(n != Z && n->op == OCONST) { + if(typev[n->type->etype] || n->type->etype == TIND) { + vv = n->vconst >> 32; + if(vv != 0 && vv != -1) + return 1; + } + } + return 0; +} + +int +sval(long v) +{ + if(v >= -32766L && v < 32766L) + return 1; + return 0; +} + +long +exreg(Type *t) +{ + long o; + + if(typechlp[t->etype]) { + if(exregoffset <= 16) + return 0; + o = exregoffset; + exregoffset--; + return o; + } + if(typefd[t->etype]) { + if(exfregoffset <= 16) + return 0; + o = exfregoffset + NREG; + exfregoffset--; + return o; + } + return 0; +} + +schar ewidth[NTYPE] = +{ + -1, /* [TXXX] */ + SZ_CHAR, /* [TCHAR] */ + SZ_CHAR, /* [TUCHAR] */ + SZ_SHORT, /* [TSHORT] */ + SZ_SHORT, /* [TUSHORT] */ + SZ_INT, /* [TINT] */ + SZ_INT, /* [TUINT] */ + SZ_LONG, /* [TLONG] */ + SZ_LONG, /* [TULONG] */ + SZ_VLONG, /* [TVLONG] */ + SZ_VLONG, /* [TUVLONG] */ + SZ_FLOAT, /* [TFLOAT] */ + SZ_DOUBLE, /* [TDOUBLE] */ + SZ_IND, /* [TIND] */ + 0, /* [TFUNC] */ + -1, /* [TARRAY] */ + 0, /* [TVOID] */ + -1, /* [TSTRUCT] */ + -1, /* [TUNION] */ + SZ_INT, /* [TENUM] */ +}; + +long ncast[NTYPE] = +{ + 0, /* [TXXX] */ + BCHAR|BUCHAR, /* [TCHAR] */ + BCHAR|BUCHAR, /* [TUCHAR] */ + BSHORT|BUSHORT, /* [TSHORT] */ + BSHORT|BUSHORT, /* [TUSHORT] */ + BINT|BUINT|BLONG|BULONG, /* [TINT] */ + BINT|BUINT|BLONG|BULONG, /* [TUINT] */ + BINT|BUINT|BLONG|BULONG, /* [TLONG] */ + BINT|BUINT|BLONG|BULONG, /* [TULONG] */ + BVLONG|BUVLONG|BIND, /* [TVLONG] */ + BVLONG|BUVLONG|BIND, /* [TUVLONG] */ + BFLOAT, /* [TFLOAT] */ + BDOUBLE, /* [TDOUBLE] */ + BVLONG|BUVLONG|BIND, /* [TIND] */ + 0, /* [TFUNC] */ + 0, /* [TARRAY] */ + 0, /* [TVOID] */ + BSTRUCT, /* [TSTRUCT] */ + BUNION, /* [TUNION] */ + 0, /* [TENUM] */ +}; diff --git a/sys/src/cmd/4l/asm.c b/sys/src/cmd/4l/asm.c new file mode 100644 index 0000000..909f49f --- /dev/null +++ b/sys/src/cmd/4l/asm.c @@ -0,0 +1,1477 @@ +#include "l.h" + +long OFFSET; +/* +long BADOFFSET = -1; + + if(OFFSET <= BADOFFSET && OFFSET+4 > BADOFFSET)\ + abort();\ + OFFSET += 4;\ + + if(OFFSET == BADOFFSET)\ + abort();\ + OFFSET++;\ +*/ + +#define VPUT(c)\ + {\ + cbp[0] = (c)>>56;\ + cbp[1] = (c)>>48;\ + cbp[2] = (c)>>40;\ + cbp[3] = (c)>>32;\ + cbp[4] = (c)>>24;\ + cbp[5] = (c)>>16;\ + cbp[6] = (c)>>8;\ + cbp[7] = (c);\ + cbp += 8;\ + cbc -= 8;\ + if(cbc <= 0)\ + cflush();\ + } + +#define LPUT(c)\ + {\ + cbp[0] = (c)>>24;\ + cbp[1] = (c)>>16;\ + cbp[2] = (c)>>8;\ + cbp[3] = (c);\ + cbp += 4;\ + cbc -= 4;\ + if(cbc <= 0)\ + cflush();\ + } + +#define CPUT(c)\ + {\ + cbp[0] = (c);\ + cbp++;\ + cbc--;\ + if(cbc <= 0)\ + cflush();\ + } + +u64int +entryvalue(void) +{ + char *a; + Sym *s; + + a = INITENTRY; + if(*a >= '0' && *a <= '9') + return (u64int)atovlwhex(a); + s = lookup(a, 0); + if(s->type == 0) + return INITTEXT; + if(s->type != STEXT && s->type != SLEAF) + diag("entry not text: %s", s->name); + return (u64int)s->value; +} + +void +asmb(void) +{ + Prog *p; + long t; + Optab *o; + + if(debug['v']) + Bprint(&bso, "%5.2f asm\n", cputime()); + Bflush(&bso); + OFFSET = HEADR; + seek(cout, OFFSET, 0); + pc = INITTEXT; + for(p = firstp; p != P; p = p->link) { + if(p->as == ATEXT) { + curtext = p; + autosize = p->to.offset + 8; + } + if(p->pc != pc) { + diag("phase error %lux sb %lux", + p->pc, pc); + if(!debug['a']) + prasm(curp); + pc = p->pc; + } + curp = p; + o = oplook(p); /* could probably avoid this call */ + if(asmout(p, o, 0)) { + p = p->link; + pc += 4; + } + pc += o->size; + } + if(debug['a']) + Bprint(&bso, "\n"); + Bflush(&bso); + cflush(); + + curtext = P; + switch(HEADTYPE) { + case 0: + case 4: + OFFSET = rnd(HEADR+textsize, 4096); + seek(cout, OFFSET, 0); + break; + case 1: + case 2: + case 3: + case 5: + case 6: + case 7: + OFFSET = HEADR+textsize; + seek(cout, OFFSET, 0); + break; + } + for(t = 0; t < datsize; t += sizeof(buf)-100) { + if(datsize-t > sizeof(buf)-100) + datblk(t, sizeof(buf)-100); + else + datblk(t, datsize-t); + } + + symsize = 0; + lcsize = 0; + if(!debug['s']) { + if(debug['v']) + Bprint(&bso, "%5.2f sym\n", cputime()); + Bflush(&bso); + switch(HEADTYPE) { + case 0: + case 4: + OFFSET = rnd(HEADR+textsize, 4096)+datsize; + seek(cout, OFFSET, 0); + break; + case 3: + case 2: + case 1: + case 5: + case 6: + case 7: + OFFSET = HEADR+textsize+datsize; + seek(cout, OFFSET, 0); + break; + } + if(!debug['s']) + asmsym(); + if(debug['v']) + Bprint(&bso, "%5.2f pc\n", cputime()); + Bflush(&bso); + if(!debug['s']) + asmlc(); + cflush(); + } + + if(debug['v']) + Bprint(&bso, "%5.2f header\n", cputime()); + Bflush(&bso); + OFFSET = 0; + seek(cout, OFFSET, 0); + switch(HEADTYPE) { + case 0: + lput(0x160L<<16); /* magic and sections */ + lput(0L); /* time and date */ + lput(rnd(HEADR+textsize, 4096)+datsize); + lput(symsize); /* nsyms */ + lput((0x38L<<16)|7L); /* size of optional hdr and flags */ + lput((0413<<16)|0437L); /* magic and version */ + lput(rnd(HEADR+textsize, 4096)); /* sizes */ + lput(datsize); + lput(bsssize); + lput(entryvalue()); /* va of entry */ + lput(INITTEXT-HEADR); /* va of base of text */ + lput(INITDAT); /* va of base of data */ + lput(INITDAT+datsize); /* va of base of bss */ + lput(~0L); /* gp reg mask */ + lput(0L); + lput(0L); + lput(0L); + lput(0L); + lput(~0L); /* gp value ?? */ + break; + case 1: + lput(0x160L<<16); /* magic and sections */ + lput(0L); /* time and date */ + lput(HEADR+textsize+datsize); + lput(symsize); /* nsyms */ + lput((0x38L<<16)|7L); /* size of optional hdr and flags */ + + lput((0407<<16)|0437L); /* magic and version */ + lput(textsize); /* sizes */ + lput(datsize); + lput(bsssize); + lput(entryvalue()); /* va of entry */ + lput(INITTEXT); /* va of base of text */ + lput(INITDAT); /* va of base of data */ + lput(INITDAT+datsize); /* va of base of bss */ + lput(~0L); /* gp reg mask */ + lput(lcsize); + lput(0L); + lput(0L); + lput(0L); + lput(~0L); /* gp value ?? */ + lput(0L); /* complete mystery */ + break; + case 2: + t = 18; + lput(((((4*t)+0)*t)+7)); /* magic */ + lput(textsize); /* sizes */ + lput(datsize); + lput(bsssize); + lput(symsize); /* nsyms */ + lput(entryvalue()); /* va of entry */ + lput(0L); + lput(lcsize); + break; + case 3: + lput((0x160L<<16)|3L); /* magic and sections */ + lput(time(0)); /* time and date */ + lput(HEADR+textsize+datsize); + lput(symsize); /* nsyms */ + lput((0x38L<<16)|7L); /* size of optional hdr and flags */ + + lput((0407<<16)|0437L); /* magic and version */ + lput(textsize); /* sizes */ + lput(datsize); + lput(bsssize); + lput(entryvalue()); /* va of entry */ + lput(INITTEXT); /* va of base of text */ + lput(INITDAT); /* va of base of data */ + lput(INITDAT+datsize); /* va of base of bss */ + lput(~0L); /* gp reg mask */ + lput(lcsize); + lput(0L); + lput(0L); + lput(0L); + lput(~0L); /* gp value ?? */ + + strnput(".text", 8); /* text segment */ + lput(INITTEXT); /* address */ + lput(INITTEXT); + lput(textsize); + lput(HEADR); + lput(0L); + lput(HEADR+textsize+datsize+symsize); + lput(lcsize); /* line number size */ + lput(0x20L); /* flags */ + + strnput(".data", 8); /* data segment */ + lput(INITDAT); /* address */ + lput(INITDAT); + lput(datsize); + lput(HEADR+textsize); + lput(0L); + lput(0L); + lput(0L); + lput(0x40L); /* flags */ + + strnput(".bss", 8); /* bss segment */ + lput(INITDAT+datsize); /* address */ + lput(INITDAT+datsize); + lput(bsssize); + lput(0L); + lput(0L); + lput(0L); + lput(0L); + lput(0x80L); /* flags */ + break; + case 4: + + lput((0x160L<<16)|3L); /* magic and sections */ + lput(time(0)); /* time and date */ + lput(rnd(HEADR+textsize, 4096)+datsize); + lput(symsize); /* nsyms */ + lput((0x38L<<16)|7L); /* size of optional hdr and flags */ + + lput((0413<<16)|01012L); /* magic and version */ + lput(textsize); /* sizes */ + lput(datsize); + lput(bsssize); + lput(entryvalue()); /* va of entry */ + lput(INITTEXT); /* va of base of text */ + lput(INITDAT); /* va of base of data */ + lput(INITDAT+datsize); /* va of base of bss */ + lput(~0L); /* gp reg mask */ + lput(lcsize); + lput(0L); + lput(0L); + lput(0L); + lput(~0L); /* gp value ?? */ + + strnput(".text", 8); /* text segment */ + lput(INITTEXT); /* address */ + lput(INITTEXT); + lput(textsize); + lput(HEADR); + lput(0L); + lput(HEADR+textsize+datsize+symsize); + lput(lcsize); /* line number size */ + lput(0x20L); /* flags */ + + strnput(".data", 8); /* data segment */ + lput(INITDAT); /* address */ + lput(INITDAT); + lput(datsize); + lput(rnd(HEADR+textsize, 4096)); /* sizes */ + lput(0L); + lput(0L); + lput(0L); + lput(0x40L); /* flags */ + + strnput(".bss", 8); /* bss segment */ + lput(INITDAT+datsize); /* address */ + lput(INITDAT+datsize); + lput(bsssize); + lput(0L); + lput(0L); + lput(0L); + lput(0L); + lput(0x80L); /* flags */ + break; + case 5: + strnput("\177ELF", 4); /* e_ident */ + CPUT(1); /* class = 32 bit */ + CPUT(2); /* data = MSB */ + CPUT(1); /* version = CURRENT */ + strnput("", 9); + lput((2L<<16)|8L); /* type = EXEC; machine = MIPS */ + lput(1L); /* version = CURRENT */ + lput(entryvalue()); /* entry vaddr */ + lput(52L); /* offset to first phdr */ + lput(0L); /* offset to first shdr */ + lput(0L); /* flags = MIPS */ + lput((52L<<16)|32L); /* Ehdr & Phdr sizes*/ + lput((3L<<16)|0L); /* # Phdrs & Shdr size */ + lput((0L<<16)|0L); /* # Shdrs & shdr string size */ + + lput(1L); /* text - type = PT_LOAD */ + lput(0L); /* file offset */ + lput(INITTEXT-HEADR); /* vaddr */ + lput(INITTEXT-HEADR); /* paddr */ + lput(HEADR+textsize); /* file size */ + lput(HEADR+textsize); /* memory size */ + lput(0x05L); /* protections = RX */ + lput(0x10000L); /* alignment code?? */ + + lput(1L); /* data - type = PT_LOAD */ + lput(HEADR+textsize); /* file offset */ + lput(INITDAT); /* vaddr */ + lput(INITDAT); /* paddr */ + lput(datsize); /* file size */ + lput(datsize+bsssize); /* memory size */ + lput(0x06L); /* protections = RW */ + lput(0x10000L); /* alignment code?? */ + + lput(0L); /* data - type = PT_NULL */ + lput(HEADR+textsize+datsize); /* file offset */ + lput(0L); + lput(0L); + lput(symsize); /* symbol table size */ + lput(lcsize); /* line number size */ + lput(0x04L); /* protections = R */ + lput(0x04L); /* alignment code?? */ + break; + case 6: + t = 16; + lput(((((4*t)+0)*t)+7)); /* magic */ + lput(textsize); /* sizes */ + lput(datsize); + lput(bsssize); + lput(symsize); /* nsyms */ + lput(entryvalue()); /* va of entry */ + lput(0L); + lput(lcsize); + break; + case 7: + strnput("\177ELF", 4); /* e_ident */ + CPUT(2); /* class = 64 bit */ + CPUT(2); /* data = MSB */ + CPUT(1); /* version = CURRENT */ + strnput("", 9); + lput((2L<<16)|8L); /* type = EXEC; machine = MIPS */ + lput(1L); /* version = CURRENT */ + VPUT(entryvalue()); /* entry vaddr */ + VPUT(64LL); /* offset to first phdr */ + VPUT(0LL); /* offset to first shdr */ + lput(0L); /* flags = MIPS */ + lput((64L<<16)|56L); /* Ehdr & Phdr sizes*/ + lput((2L<<16)|0L); /* # Phdrs & Shdr size */ + lput((0L<<16)|0L); /* # Shdrs & shdr string size */ + + lput(1L); /* text - type = PT_LOAD */ + lput(0x05L); /* protections = RX */ + VPUT(0x10000ULL); /* file offset */ + VPUT(INITTEXT); /* vaddr */ + VPUT(INITTEXT); /* paddr */ + VPUT((vlong)textsize); /* file size */ + VPUT((vlong)HEADR+textsize); /* memory size */ + VPUT(0x10000ULL); /* alignment code?? */ + + lput(1L); /* data - type = PT_LOAD */ + lput(0x06L); /* protections = RW */ + VPUT((vlong)HEADR+textsize); /* file offset */ + VPUT(INITDAT); /* vaddr */ + VPUT(INITDAT); /* paddr */ + VPUT((vlong)datsize); /* file size */ + VPUT((vlong)datsize+bsssize); /* memory size */ + VPUT(0x10000LL); /* alignment code?? */ + break; + } + cflush(); +} + +void +strnput(char *s, int n) +{ + for(; *s; s++){ + CPUT(*s); + n--; + } + for(; n > 0; n--) + CPUT(0); +} + +void +lput(long l) +{ + + LPUT(l); +} + +void +cflush(void) +{ + int n; + + n = sizeof(buf.cbuf) - cbc; + if(n) + write(cout, buf.cbuf, n); + cbp = buf.cbuf; + cbc = sizeof(buf.cbuf); +} + +void +nopstat(char *f, Count *c) +{ + if(c->outof) + Bprint(&bso, "%s delay %ld/%ld (%.2f)\n", f, + c->outof - c->count, c->outof, + (double)(c->outof - c->count)/c->outof); +} + +void +asmsym(void) +{ + Prog *p; + Auto *a; + Sym *s; + int h; + + s = lookup("etext", 0); + if(s->type == STEXT) + putsymb(s->name, 'T', s->value, s->version); + + for(h=0; hlink) + switch(s->type) { + case SCONST: + putsymb(s->name, 'D', s->value, s->version); + continue; + + case SDATA: + putsymb(s->name, 'D', s->value+INITDAT, s->version); + continue; + + case SBSS: + putsymb(s->name, 'B', s->value+INITDAT, s->version); + continue; + + case SFILE: + putsymb(s->name, 'f', s->value, s->version); + continue; + } + + for(p=textp; p!=P; p=p->cond) { + s = p->from.sym; + if(s->type != STEXT && s->type != SLEAF) + continue; + + /* filenames first */ + for(a=p->to.autom; a; a=a->link) + if(a->type == D_FILE) + putsymb(a->asym->name, 'z', a->aoffset, 0); + else + if(a->type == D_FILE1) + putsymb(a->asym->name, 'Z', a->aoffset, 0); + + if(s->type == STEXT) + putsymb(s->name, 'T', s->value, s->version); + else + putsymb(s->name, 'L', s->value, s->version); + + /* frame, auto and param after */ + putsymb(".frame", 'm', p->to.offset+8, 0); + for(a=p->to.autom; a; a=a->link) + if(a->type == D_AUTO) + putsymb(a->asym->name, 'a', -a->aoffset, 0); + else + if(a->type == D_PARAM) + putsymb(a->asym->name, 'p', a->aoffset, 0); + } + if(debug['v'] || debug['n']) + Bprint(&bso, "symsize = %lud\n", symsize); + Bflush(&bso); +} + +void +putsymb(char *s, int t, long v, int ver) +{ + int i, f; + + if(t == 'f') + s++; + LPUT(v); + if(ver) + t += 'a' - 'A'; + CPUT(t+0x80); /* 0x80 is variable length */ + + if(t == 'Z' || t == 'z') { + CPUT(s[0]); + for(i=1; s[i] != 0 || s[i+1] != 0; i += 2) { + CPUT(s[i]); + CPUT(s[i+1]); + } + CPUT(0); + CPUT(0); + i++; + } + else { + for(i=0; s[i]; i++) + CPUT(s[i]); + CPUT(0); + } + symsize += 4 + 1 + i + 1; + + if(debug['n']) { + if(t == 'z' || t == 'Z') { + Bprint(&bso, "%c %.8lux ", t, v); + for(i=1; s[i] != 0 || s[i+1] != 0; i+=2) { + f = ((s[i]&0xff) << 8) | (s[i+1]&0xff); + Bprint(&bso, "/%x", f); + } + Bprint(&bso, "\n"); + return; + } + if(ver) + Bprint(&bso, "%c %.8lux %s<%d>\n", t, v, s, ver); + else + Bprint(&bso, "%c %.8lux %s\n", t, v, s); + } +} + +#define MINLC 4 +void +asmlc(void) +{ + long oldpc, oldlc; + Prog *p; + long v, s; + + oldpc = INITTEXT; + oldlc = 0; + for(p = firstp; p != P; p = p->link) { + if(p->line == oldlc || p->as == ATEXT || p->as == ANOP) { + if(p->as == ATEXT) + curtext = p; + if(debug['L']) + Bprint(&bso, "%6llux %P\n", + p->pc, p); + continue; + } + if(debug['L']) + Bprint(&bso, "\t\t%6ld", lcsize); + v = (p->pc - oldpc) / MINLC; + while(v) { + s = 127; + if(v < 127) + s = v; + CPUT(s+128); /* 129-255 +pc */ + if(debug['L']) + Bprint(&bso, " pc+%ld*%d(%ld)", s, MINLC, s+128); + v -= s; + lcsize++; + } + s = p->line - oldlc; + oldlc = p->line; + oldpc = p->pc + MINLC; + if(s > 64 || s < -64) { + CPUT(0); /* 0 vv +lc */ + CPUT(s>>24); + CPUT(s>>16); + CPUT(s>>8); + CPUT(s); + if(debug['L']) { + if(s > 0) + Bprint(&bso, " lc+%ld(%d,%ld)\n", + s, 0, s); + else + Bprint(&bso, " lc%ld(%d,%ld)\n", + s, 0, s); + Bprint(&bso, "%6llux %P\n", + p->pc, p); + } + lcsize += 5; + continue; + } + if(s > 0) { + CPUT(0+s); /* 1-64 +lc */ + if(debug['L']) { + Bprint(&bso, " lc+%ld(%ld)\n", s, 0+s); + Bprint(&bso, "%6llux %P\n", + p->pc, p); + } + } else { + CPUT(64-s); /* 65-128 -lc */ + if(debug['L']) { + Bprint(&bso, " lc%ld(%ld)\n", s, 64-s); + Bprint(&bso, "%6llux %P\n", + p->pc, p); + } + } + lcsize++; + } + while(lcsize & 1) { + s = 129; + CPUT(s); + lcsize++; + } + if(debug['v'] || debug['L']) + Bprint(&bso, "lcsize = %ld\n", lcsize); + Bflush(&bso); +} + +void +datblk(long s, long n) +{ + Prog *p; + char *cast; + long l, fl, j, d; + int i, c; + + memset(buf.dbuf, 0, n+100); + for(p = datap; p != P; p = p->link) { + curp = p; + l = p->from.sym->value + p->from.offset - s; + c = p->reg; + i = 0; + if(l < 0) { + if(l+c <= 0) + continue; + while(l < 0) { + l++; + i++; + } + } + if(l >= n) + continue; + if(p->as != AINIT && p->as != ADYNT) { + for(j=l+(c-i)-1; j>=l; j--) + if(buf.dbuf[j]) { + print("%P\n", p); + diag("multiple initialization"); + break; + } + } + switch(p->to.type) { + default: + diag("unknown mode in initialization\n%P", p); + break; + + case D_VCONST: + cast = (char*)p->to.ieee; + for(; ito.ieee); + cast = (char*)&fl; + for(; ito.ieee; + for(; ito.sval[i]; + l++; + } + break; + + case D_CONST: + d = p->to.offset; + if(p->to.sym) { + if(p->to.sym->type == STEXT || + p->to.sym->type == SLEAF) + d += p->to.sym->value; + if(p->to.sym->type == SDATA) + d += p->to.sym->value + INITDAT; + if(p->to.sym->type == SBSS) + d += p->to.sym->value + INITDAT; + } + cast = (char*)&d; + switch(c) { + default: + diag("bad nuxi %d %d\n%P", c, i, curp); + break; + case 1: + for(; itype) { + default: + diag("unknown type %d", o->type); + if(!debug['a']) + prasm(p); + break; + + case 0: /* pseudo ops */ + if(aflag) { + if(p->link) { + if(p->as == ATEXT) { + ct = curtext; + o2 = autosize; + curtext = p; + autosize = p->to.offset + 8; + o1 = asmout(p->link, oplook(p->link), aflag); + curtext = ct; + autosize = o2; + } else + o1 = asmout(p->link, oplook(p->link), aflag); + } + return o1; + } + break; + + case 1: /* mov[v] r1,r2 ==> OR r1,r0,r2 */ + o1 = OP_RRR(oprrr(AOR), p->from.reg, REGZERO, p->to.reg); + break; + + case 2: /* add/sub r1,[r2],r3 */ + r = p->reg; + if(r == NREG) + r = p->to.reg; + o1 = OP_RRR(oprrr(p->as), p->from.reg, r, p->to.reg); + break; + + case 3: /* mov $soreg, r ==> or/add $i,o,r */ + v = regoff(&p->from); + r = p->from.reg; + if(r == NREG) + r = o->param; + a = AADDU; + if(o->a1 == C_ANDCON) + a = AOR; + o1 = OP_IRR(opirr(a), v, r, p->to.reg); + break; + + case 4: /* add $scon,[r1],r2 */ + v = regoff(&p->from); + r = p->reg; + if(r == NREG) + r = p->to.reg; + o1 = OP_IRR(opirr(p->as), v, r, p->to.reg); + break; + + case 5: /* syscall */ + if(aflag) + return 0; + o1 = oprrr(p->as); + break; + + case 6: /* beq r1,[r2],sbra */ + if(aflag) + return 0; + if(!debug['Y'] && p->link && p->cond && isnop(p->link)) { + nop.branch.count--; + nop.branch.outof--; + nop.jump.outof++; + o2 = asmout(p->cond, oplook(p->cond), 1); + if(o2) { + if(p->cond == P) + v = -4 >> 2; + else + v = (p->cond->pc+4 - pc-4) >> 2; + if(((v << 16) >> 16) != v) + diag("short branch too far: %d\n%P", v, p); + o1 = OP_IRR(opirr(p->as+ALAST), v, p->from.reg, p->reg); + if(debug['a']) + Bprint(&bso, " %.8llux: %.8lux %.8lux\t%P\n", + p->pc, o1, o2, p); + LPUT(o1); + LPUT(o2); + return 1; + } + } + if(p->cond == P) + v = -4 >> 2; + else + v = (p->cond->pc - pc-4) >> 2; + if(((v << 16) >> 16) != v) + diag("short branch too far: %d\n%P", v, p); + o1 = OP_IRR(opirr(p->as), v, p->from.reg, p->reg); + break; + + case 7: /* mov r, soreg ==> sw o(r) */ + r = p->to.reg; + if(r == NREG) + r = o->param; + v = regoff(&p->to); + o1 = OP_IRR(opirr(p->as), v, r, p->from.reg); + break; + + case 8: /* mov soreg, r ==> lw o(r) */ + r = p->from.reg; + if(r == NREG) + r = o->param; + v = regoff(&p->from); + o1 = OP_IRR(opirr(p->as+ALAST), v, r, p->to.reg); + break; + + case 9: /* asl r1,[r2],r3 */ + r = p->reg; + if(r == NREG) + r = p->to.reg; + o1 = OP_RRR(oprrr(p->as), r, p->from.reg, p->to.reg); + break; + + case 10: /* add $con,[r1],r2 ==> mov $con,t; add t,[r1],r2 */ + v = regoff(&p->from); + r = AOR; + if(v < 0) + r = AADDU; + o1 = OP_IRR(opirr(r), v, 0, REGTMP); + r = p->reg; + if(r == NREG) + r = p->to.reg; + o2 = OP_RRR(oprrr(p->as), REGTMP, r, p->to.reg); + break; + + case 11: /* jmp lbra */ + if(aflag) + return 0; + if(p->cond == P) + v = p->pc >> 2; + else + v = p->cond->pc >> 2; + o1 = OP_JMP(opirr(p->as), v); + if(!debug['Y'] && p->link && p->cond && isnop(p->link)) { + nop.branch.count--; + nop.branch.outof--; + nop.jump.outof++; + o2 = asmout(p->cond, oplook(p->cond), 1); + if(o2) { + o1 += 1; + if(debug['a']) + Bprint(&bso, " %.8llux: %.8lux %.8lux%P\n", + p->pc, o1, o2, p); + LPUT(o1); + LPUT(o2); + return 1; + } + } + break; + + case 12: /* movbs r,r */ + v = 16; + if(p->as == AMOVB) + v = 24; + o1 = OP_SRR(opirr(ASLL), v, p->from.reg, p->to.reg); + o2 = OP_SRR(opirr(ASRA), v, p->to.reg, p->to.reg); + break; + + case 13: /* movbu r,r */ + if(p->as == AMOVBU) + o1 = OP_IRR(opirr(AAND), 0xffL, p->from.reg, p->to.reg); + else + o1 = OP_IRR(opirr(AAND), 0xffffL, p->from.reg, p->to.reg); + break; + + case 14: /* movwu r,r */ + v = 32-32; + o1 = OP_SRR(opirr(ASLLV+ALAST), v, p->from.reg, p->to.reg); + o2 = OP_SRR(opirr(ASRLV+ALAST), v, p->to.reg, p->to.reg); + break; + + case 16: /* sll $c,[r1],r2 */ + v = regoff(&p->from); + r = p->reg; + if(r == NREG) + r = p->to.reg; + if(v >= 32) + o1 = OP_SRR(opirr(p->as+ALAST), v-32, r, p->to.reg); + else + o1 = OP_SRR(opirr(p->as), v, r, p->to.reg); + break; + + case 18: /* jmp [r1],0(r2) */ + if(aflag) + return 0; + r = p->reg; + if(r == NREG) + r = o->param; + o1 = OP_RRR(oprrr(p->as), 0, p->to.reg, r); + break; + + case 19: /* mov $lcon,r ==> lu+or */ + v = regoff(&p->from); + o1 = OP_IRR(opirr(ALAST), v>>16, REGZERO, p->to.reg); + o2 = OP_IRR(opirr(AOR), v, p->to.reg, p->to.reg); + break; + + case 20: /* mov lohi,r */ + r = OP(2,0); /* mfhi */ + if(p->from.type == D_LO) + r = OP(2,2); /* mflo */ + o1 = OP_RRR(r, REGZERO, REGZERO, p->to.reg); + break; + + case 21: /* mov r,lohi */ + r = OP(2,1); /* mthi */ + if(p->to.type == D_LO) + r = OP(2,3); /* mtlo */ + o1 = OP_RRR(r, REGZERO, p->from.reg, REGZERO); + break; + + case 22: /* mul r1,r2 */ + o1 = OP_RRR(oprrr(p->as), p->from.reg, p->reg, REGZERO); + break; + + case 23: /* add $lcon,r1,r2 ==> lu+or+add */ + v = regoff(&p->from); + if(p->to.reg == REGTMP || p->reg == REGTMP) + diag("cant synthesize large constant\n%P", p); + o1 = OP_IRR(opirr(ALAST), v>>16, REGZERO, REGTMP); + o2 = OP_IRR(opirr(AOR), v, REGTMP, REGTMP); + r = p->reg; + if(r == NREG) + r = p->to.reg; + o3 = OP_RRR(oprrr(p->as), REGTMP, r, p->to.reg); + break; + + case 24: /* mov $ucon,,r ==> lu r */ + v = regoff(&p->from); + o1 = OP_IRR(opirr(ALAST), v>>16, REGZERO, p->to.reg); + break; + + case 25: /* add/and $ucon,[r1],r2 ==> lu $con,t; add t,[r1],r2 */ + v = regoff(&p->from); + o1 = OP_IRR(opirr(ALAST), v>>16, REGZERO, REGTMP); + r = p->reg; + if(r == NREG) + r = p->to.reg; + o2 = OP_RRR(oprrr(p->as), REGTMP, r, p->to.reg); + break; + + case 26: /* mov $lsext/auto/oreg,,r2 ==> lu+or+add */ + v = regoff(&p->from); + if(p->to.reg == REGTMP) + diag("cant synthesize large constant\n%P", p); + o1 = OP_IRR(opirr(ALAST), v>>16, REGZERO, REGTMP); + o2 = OP_IRR(opirr(AOR), v, REGTMP, REGTMP); + r = p->from.reg; + if(r == NREG) + r = o->param; + o3 = OP_RRR(oprrr(AADDU), REGTMP, r, p->to.reg); + break; + + case 27: /* mov [sl]ext/auto/oreg,fr ==> lwc1 o(r) */ + r = p->from.reg; + if(r == NREG) + r = o->param; + v = regoff(&p->from); + if(p->as == AMOVD) + o4 = opirr(AMOVD+ALAST); + else + o4 = opirr(AMOVF+ALAST); + switch(o->size) { + case 16: + o1 = OP_IRR(opirr(ALAST), v>>16, REGZERO, REGTMP); + o2 = OP_IRR(opirr(AOR), v, REGTMP, REGTMP); + o3 = OP_RRR(oprrr(AADDU), r, REGTMP, REGTMP); + o4 = OP_IRR(o4, 0, REGTMP, p->to.reg); + break; + case 4: + o1 = OP_IRR(o4, v, r, p->to.reg); + break; + } + break; + + case 28: /* mov fr,[sl]ext/auto/oreg ==> swc1 o(r) */ + r = p->to.reg; + if(r == NREG) + r = o->param; + v = regoff(&p->to); + if(p->as == AMOVD) + o4 = opirr(AMOVD); + else + o4 = opirr(AMOVF); + switch(o->size) { + case 16: + if(r == REGTMP) + diag("cant synthesize large constant\n%P", p); + o1 = OP_IRR(opirr(ALAST), v>>16, REGZERO, REGTMP); + o2 = OP_IRR(opirr(AOR), v, REGTMP, REGTMP); + o3 = OP_RRR(oprrr(AADDU), r, REGTMP, REGTMP); + o4 = OP_IRR(o4, 0, REGTMP, p->from.reg); + break; + case 4: + o1 = OP_IRR(o4, v, r, p->from.reg); + break; + } + break; + + case 30: /* movw r,fr */ + r = SP(2,1)|(4<<21); /* mtc1 */ + o1 = OP_RRR(r, p->from.reg, 0, p->to.reg); + break; + + case 31: /* movw fr,r */ + r = SP(2,1)|(0<<21); /* mfc1 */ + o1 = OP_RRR(r, p->to.reg, 0, p->from.reg); + break; + + case 32: /* fadd fr1,[fr2],fr3 */ + r = p->reg; + if(r == NREG) + o1 = OP_FRRR(oprrr(p->as), p->from.reg, p->to.reg, p->to.reg); + else + o1 = OP_FRRR(oprrr(p->as), p->from.reg, r, p->to.reg); + break; + + case 33: /* fabs fr1,fr3 */ + o1 = OP_FRRR(oprrr(p->as), 0, p->from.reg, p->to.reg); + break; + + case 34: /* mov $con,fr ==> or/add $i,r,r2 */ + v = regoff(&p->from); + r = AADDU; + if(o->a1 == C_ANDCON) + r = AOR; + o1 = OP_IRR(opirr(r), v, 0, REGTMP); + o2 = OP_RRR(SP(2,1)|(4<<21), REGTMP, 0, p->to.reg); /* mtc1 */ + break; + + case 35: /* mov r,lext/luto/oreg ==> sw o(r) */ + /* + * the lowbits of the constant cannot + * be moved into the offset of the load + * because the mips 4000 in 64-bit mode + * does a 64-bit add and it will screw up. + */ + v = regoff(&p->to); + r = p->to.reg; + if(r == NREG) + r = o->param; + if(r == REGTMP) + diag("cant synthesize large constant\n%P", p); + o1 = OP_IRR(opirr(ALAST), v>>16, REGZERO, REGTMP); + o2 = OP_IRR(opirr(AOR), v, REGTMP, REGTMP); + o3 = OP_RRR(oprrr(AADDU), r, REGTMP, REGTMP); + o4 = OP_IRR(opirr(p->as), 0, REGTMP, p->from.reg); + break; + + case 36: /* mov lext/lauto/lreg,r ==> lw o(r30) */ + v = regoff(&p->from); + r = p->from.reg; + if(r == NREG) + r = o->param; + if(r == REGTMP) + diag("cant synthesize large constant\n%P", p); + o1 = OP_IRR(opirr(ALAST), v>>16, REGZERO, REGTMP); + o2 = OP_IRR(opirr(AOR), v, REGTMP, REGTMP); + o3 = OP_RRR(oprrr(AADDU), r, REGTMP, REGTMP); + o4 = OP_IRR(opirr(p->as+ALAST), 0, REGTMP, p->to.reg); + break; + + case 37: /* movw r,mr */ + r = SP(2,0)|(4<<21); /* mtc0 */ + if(p->as == AMOVV) + r = SP(2,0)|(5<<21); /* dmtc0 */ + o1 = OP_RRR(r, p->from.reg, 0, p->to.reg); + break; + + case 38: /* movw mr,r */ + r = SP(2,0)|(0<<21); /* mfc0 */ + if(p->as == AMOVV) + r = SP(2,0)|(1<<21); /* dmfc0 */ + o1 = OP_RRR(r, p->to.reg, 0, p->from.reg); + break; + + case 39: /* rfe ==> jmp+rfe */ + if(aflag) + return 0; + o1 = OP_RRR(oprrr(AJMP), 0, p->to.reg, REGZERO); + o2 = oprrr(p->as); + break; + + case 40: /* word */ + if(aflag) + return 0; + o1 = regoff(&p->to); + break; + + case 41: /* movw r,fcr */ + o1 = OP_RRR(SP(2,1)|(2<<21), REGZERO, 0, p->to.reg); /* mfcc1 */ + o2 = OP_RRR(SP(2,1)|(6<<21), p->from.reg, 0, p->to.reg);/* mtcc1 */ + break; + + case 42: /* movw fcr,r */ + o1 = OP_RRR(SP(2,1)|(2<<21), p->to.reg, 0, p->from.reg);/* mfcc1 */ + break; + + case 47: /* movv r,fr */ + r = SP(2,1)|(5<<21); /* dmtc1 */ + o1 = OP_RRR(r, p->from.reg, 0, p->to.reg); + break; + + case 48: /* movv fr,r */ + r = SP(2,1)|(1<<21); /* dmfc1 */ + o1 = OP_RRR(r, p->to.reg, 0, p->from.reg); + break; + } + if(aflag) + return o1; + v = p->pc; + switch(o->size) { + default: + if(debug['a']) + Bprint(&bso, " %.16llux:\t\t%P\n", v, p); + break; + case 4: + if(debug['a']) + Bprint(&bso, " %.16llux: %.8lux\t%P\n", v, o1, p); + LPUT(o1); + break; + case 8: + if(debug['a']) + Bprint(&bso, " %.16llux: %.8lux %.8lux\t%P\n", v, o1, o2, p); + LPUT(o1); + LPUT(o2); + break; + case 12: + if(debug['a']) + Bprint(&bso, " %.16llux: %.8lux %.8lux %.8lux\t%P\n", v, o1, o2, o3, p); + LPUT(o1); + LPUT(o2); + LPUT(o3); + break; + case 16: + if(debug['a']) + Bprint(&bso, " %.16llux: %.8lux %.8lux %.8lux %.8lux%P\n", + v, o1, o2, o3, o4, p); + LPUT(o1); + LPUT(o2); + LPUT(o3); + LPUT(o4); + break; + case 20: + if(debug['a']) + Bprint(&bso, " %.16llux: %.8lux %.8lux %.8lux %.8lux %.8lux%P\n", + v, o1, o2, o3, o4, o5, p); + LPUT(o1); + LPUT(o2); + LPUT(o3); + LPUT(o4); + LPUT(o5); + break; + + case 28: + if(debug['a']) + Bprint(&bso, " %.16llux: %.8lux %.8lux %.8lux %.8lux %.8lux %.8lux %.8lux%P\n", + v, o1, o2, o3, o4, o5, o6, o7, p); + LPUT(o1); + LPUT(o2); + LPUT(o3); + LPUT(o4); + LPUT(o5); + LPUT(o6); + LPUT(o7); + break; + } + return 0; +} + +int +isnop(Prog *p) +{ + if(p->as != ANOR) + return 0; + if(p->reg != REGZERO && p->reg != NREG) + return 0; + if(p->from.type != D_REG || p->from.reg != REGZERO) + return 0; + if(p->to.type != D_REG || p->to.reg != REGZERO) + return 0; + return 1; +} + +long +oprrr(int a) +{ + switch(a) { + case AADD: return OP(4,0); + case AADDU: return OP(4,1); + case ASGT: return OP(5,2); + case ASGTU: return OP(5,3); + case AAND: return OP(4,4); + case AOR: return OP(4,5); + case AXOR: return OP(4,6); + case ASUB: return OP(4,2); + case ASUBU: return OP(4,3); + case ANOR: return OP(4,7); + case ASLL: return OP(0,4); + case ASRL: return OP(0,6); + case ASRA: return OP(0,7); + + case ASLLV: return OP(2,4); + case ASRLV: return OP(2,6); + case ASRAV: return OP(2,7); + + case AADDV: return OP(5,4); + case AADDVU: return OP(5,5); + case ASUBV: return OP(5,6); + case ASUBVU: return OP(5,7); + case AREM: + case ADIV: return OP(3,2); + case AREMU: + case ADIVU: return OP(3,3); + case AMUL: return OP(3,0); + case AMULU: return OP(3,1); + + case AREMV: + case ADIVV: return OP(3,6); + case AREMVU: + case ADIVVU: return OP(3,7); + case AMULV: return OP(3,4); + case AMULVU: return OP(3,5); + + case AJMP: return OP(1,0); + case AJAL: return OP(1,1); + + case ABREAK: return OP(1,5); + case ASYSCALL: return OP(1,4); + case ATLBP: return MMU(1,0); + case ATLBR: return MMU(0,1); + case ATLBWI: return MMU(0,2); + case ATLBWR: return MMU(0,6); + case ARFE: return MMU(2,0); + + case ADIVF: return FPF(0,3); + case ADIVD: return FPD(0,3); + case AMULF: return FPF(0,2); + case AMULD: return FPD(0,2); + case ASUBF: return FPF(0,1); + case ASUBD: return FPD(0,1); + case AADDF: return FPF(0,0); + case AADDD: return FPD(0,0); + + case ATRUNCFV: return FPF(1,1); + case ATRUNCDV: return FPD(1,1); + case ATRUNCFW: return FPF(1,5); + case ATRUNCDW: return FPD(1,5); + case AMOVFV: return FPF(4,5); + case AMOVDV: return FPD(4,5); + case AMOVVF: return FPV(4,0); + case AMOVVD: return FPV(4,1); + + case AMOVFW: return FPF(4,4); + case AMOVDW: return FPD(4,4); + case AMOVWF: return FPW(4,0); + case AMOVDF: return FPD(4,0); + case AMOVWD: return FPW(4,1); + case AMOVFD: return FPF(4,1); + case AABSF: return FPF(0,5); + case AABSD: return FPD(0,5); + case AMOVF: return FPF(0,6); + case AMOVD: return FPD(0,6); + case ANEGF: return FPF(0,7); + case ANEGD: return FPD(0,7); + + case ACMPEQF: return FPF(6,2); + case ACMPEQD: return FPD(6,2); + case ACMPGTF: return FPF(7,4); + case ACMPGTD: return FPD(7,4); + case ACMPGEF: return FPF(7,6); + case ACMPGED: return FPD(7,6); + } + if(a >= ALAST) + diag("bad rrr %A+ALAST", a-ALAST); + else + diag("bad rrr %A", a); + return 0; +} + +long +opirr(int a) +{ + switch(a) { + case AADD: return SP(1,0); + case AADDU: return SP(1,1); + case ASGT: return SP(1,2); + case ASGTU: return SP(1,3); + case AAND: return SP(1,4); + case AOR: return SP(1,5); + case AXOR: return SP(1,6); + case ALAST: return SP(1,7); /* lui */ + case ASLL: return OP(0,0); + case ASRL: return OP(0,2); + case ASRA: return OP(0,3); + + case AADDV: return SP(3,0); + case AADDVU: return SP(3,1); + + case AJMP: return SP(0,2); + case AJAL: return SP(0,3); + case ABEQ: return SP(0,4); + case ABEQ+ALAST: return SP(2,4); /* likely */ + case ABNE: return SP(0,5); + case ABNE+ALAST: return SP(2,5); /* likely */ + + case ABGEZ: return SP(0,1)|BCOND(0,1); + case ABGEZ+ALAST: return SP(0,1)|BCOND(0,3); /* likely */ + case ABGEZAL: return SP(0,1)|BCOND(2,1); + case ABGEZAL+ALAST: return SP(0,1)|BCOND(2,3); /* likely */ + case ABGTZ: return SP(0,7); + case ABGTZ+ALAST: return SP(2,7); /* likely */ + case ABLEZ: return SP(0,6); + case ABLEZ+ALAST: return SP(2,6); /* likely */ + case ABLTZ: return SP(0,1)|BCOND(0,0); + case ABLTZ+ALAST: return SP(0,1)|BCOND(0,2); /* likely */ + case ABLTZAL: return SP(0,1)|BCOND(2,0); + case ABLTZAL+ALAST: return SP(0,1)|BCOND(2,2); /* likely */ + + case ABFPT: return SP(2,1)|(257<<16); + case ABFPT+ALAST: return SP(2,1)|(259<<16); /* likely */ + case ABFPF: return SP(2,1)|(256<<16); + case ABFPF+ALAST: return SP(2,1)|(258<<16); /* likely */ + + case AMOVB: + case AMOVBU: return SP(5,0); + case AMOVH: + case AMOVHU: return SP(5,1); + case AMOVW: return SP(5,3); + case AMOVV: return SP(7,7); + case AMOVF: return SP(7,1); + case AMOVD: return SP(7,5); + case AMOVWL: return SP(5,2); + case AMOVWR: return SP(5,6); + case AMOVVL: return SP(5,4); + case AMOVVR: return SP(5,5); + + case ABREAK: return SP(5,7); + + case AMOVWL+ALAST: return SP(4,2); + case AMOVWR+ALAST: return SP(4,6); + case AMOVVL+ALAST: return SP(3,2); + case AMOVVR+ALAST: return SP(3,3); + case AMOVB+ALAST: return SP(4,0); + case AMOVBU+ALAST: return SP(4,4); + case AMOVH+ALAST: return SP(4,1); + case AMOVHU+ALAST: return SP(4,5); + case AMOVW+ALAST: return SP(4,3); + case AMOVV+ALAST: return SP(6,7); + case AMOVF+ALAST: return SP(6,1); + case AMOVD+ALAST: return SP(6,5); + + case ASLLV: return OP(7,0); + case ASRLV: return OP(7,2); + case ASRAV: return OP(7,3); + case ASLLV+ALAST: return OP(7,4); + case ASRLV+ALAST: return OP(7,6); + case ASRAV+ALAST: return OP(7,7); + } + if(a >= ALAST) + diag("bad irr %A+ALAST", a-ALAST); + else + diag("bad irr %A", a); + return 0; +} diff --git a/sys/src/cmd/4l/compat.c b/sys/src/cmd/4l/compat.c new file mode 100644 index 0000000..3202039 --- /dev/null +++ b/sys/src/cmd/4l/compat.c @@ -0,0 +1,51 @@ +#include "l.h" + +#ifdef notdef +/* + * fake malloc + */ +void* +malloc(ulong n) +{ + void *p; + + while(n & 7) + n++; + while(nhunk < n) + gethunk(); + p = hunk; + nhunk -= n; + hunk += n; + return p; +} + +void +free(void *p) +{ + USED(p); +} + +void* +calloc(ulong m, ulong n) +{ + void *p; + + n *= m; + p = malloc(n); + memset(p, 0, n); + return p; +} + +void* +realloc(void *p, ulong n) +{ + fprint(2, "realloc(0x%p %ld) called\n", p, n); + abort(); + return 0; +} +#endif +void* +mysbrk(ulong size) +{ + return sbrk(size); +} diff --git a/sys/src/cmd/4l/enam.c b/sys/src/cmd/4l/enam.c new file mode 100644 index 0000000..0759132 --- /dev/null +++ b/sys/src/cmd/4l/enam.c @@ -0,0 +1,121 @@ +char* anames[] = +{ + "XXX", + "ABSD", + "ABSF", + "ABSW", + "ADD", + "ADDD", + "ADDF", + "ADDU", + "ADDW", + "AND", + "BEQ", + "BFPF", + "BFPT", + "BGEZ", + "BGEZAL", + "BGTZ", + "BLEZ", + "BLTZ", + "BLTZAL", + "BNE", + "BREAK", + "CMPEQD", + "CMPEQF", + "CMPGED", + "CMPGEF", + "CMPGTD", + "CMPGTF", + "DATA", + "DIV", + "DIVD", + "DIVF", + "DIVU", + "DIVW", + "GLOBL", + "GOK", + "HISTORY", + "JAL", + "JMP", + "MOVB", + "MOVBU", + "MOVD", + "MOVDF", + "MOVDW", + "MOVF", + "MOVFD", + "MOVFW", + "MOVH", + "MOVHU", + "MOVW", + "MOVWD", + "MOVWF", + "MOVWL", + "MOVWR", + "MUL", + "MULD", + "MULF", + "MULU", + "MULW", + "NAME", + "NEGD", + "NEGF", + "NEGW", + "NOP", + "NOR", + "OR", + "REM", + "REMU", + "RET", + "RFE", + "SGT", + "SGTU", + "SLL", + "SRA", + "SRL", + "SUB", + "SUBD", + "SUBF", + "SUBU", + "SUBW", + "SYSCALL", + "TEXT", + "TLBP", + "TLBR", + "TLBWI", + "TLBWR", + "WORD", + "XOR", + "END", + "MOVV", + "MOVVL", + "MOVVR", + "SLLV", + "SRAV", + "SRLV", + "DIVV", + "DIVVU", + "REMV", + "REMVU", + "MULV", + "MULVU", + "ADDV", + "ADDVU", + "SUBV", + "SUBVU", + "DYNT", + "INIT", + "BCASE", + "CASE", + "TRUNCFV", + "TRUNCDV", + "TRUNCFW", + "TRUNCDW", + "MOVWU", + "MOVFV", + "MOVDV", + "MOVVF", + "MOVVD", + "LAST", +}; diff --git a/sys/src/cmd/4l/l.h b/sys/src/cmd/4l/l.h new file mode 100644 index 0000000..d7bb462 --- /dev/null +++ b/sys/src/cmd/4l/l.h @@ -0,0 +1,331 @@ +#include +#include +#include +#include "../vc/v.out.h" + +#ifndef EXTERN +#define EXTERN extern +#endif + +typedef struct Adr Adr; +typedef struct Sym Sym; +typedef struct Autom Auto; +typedef struct Prog Prog; +typedef struct Optab Optab; +typedef struct Oprang Oprang; +typedef uchar Opcross[32][2][32]; +typedef struct Count Count; + +#define P ((Prog*)0) +#define S ((Sym*)0) +#define TNAME (curtext&&curtext->from.sym?curtext->from.sym->name:noname) + +struct Adr +{ + union + { + long u0offset; + char* u0sval; + vlong* u0vval; + Ieee* u0ieee; + } u0; + union + { + Auto* u1autom; + Sym* u1sym; + } u1; + char type; + char reg; + char name; + char class; +}; + +#define offset u0.u0offset +#define sval u0.u0sval +#define ieee u0.u0ieee +#define vval u0.u0vval + +#define autom u1.u1autom +#define sym u1.u1sym + +struct Prog +{ + Adr from; + Adr to; + union + { + long u0regused; + Prog* u0forwd; + } u0; + Prog* cond; + Prog* link; + vlong pc; + long line; + uchar mark; + uchar optab; + char as; + char reg; +}; +#define regused u0.u0regused +#define forwd u0.u0forwd + +struct Sym +{ + char *name; + short type; + short version; + short become; + short frame; + vlong value; + Sym* link; +}; +struct Autom +{ + Sym* asym; + Auto* link; + long aoffset; + short type; +}; +struct Optab +{ + char as; + char a1; + char a2; + char a3; + char type; + char size; + char param; +}; +struct Oprang +{ + Optab* start; + Optab* stop; +}; +struct Count +{ + long count; + long outof; +}; + +enum +{ + STEXT = 1, + SDATA, + SBSS, + SDATA1, + SXREF, + SLEAF, + SFILE, + SCONST, + + C_NONE = 0, + C_REG, + C_FREG, + C_FCREG, + C_MREG, + C_HI, + C_LO, + C_ZCON, + C_SCON, + C_ADD0CON, + C_AND0CON, + C_ADDCON, + C_ANDCON, + C_UCON, + C_LCON, + C_SACON, + C_SECON, + C_LACON, + C_LECON, + C_SBRA, + C_LBRA, + C_SAUTO, + C_SEXT, + C_LAUTO, + C_LEXT, + C_ZOREG, + C_SOREG, + C_LOREG, + C_GOK, + + NSCHED = 20, + +/* mark flags */ + FOLL = 1<<0, + LABEL = 1<<1, + LEAF = 1<<2, + SYNC = 1<<3, + BRANCH = 1<<4, + LOAD = 1<<5, + FCMP = 1<<6, + NOSCHED = 1<<7, + + BIG = 32766, + STRINGSZ = 200, + NHASH = 10007, + NHUNK = 100000, + MINSIZ = 64, + NENT = 100, + MAXIO = 8192, + MAXHIST = 20, /* limit of path elements for history symbols */ +}; + +EXTERN union +{ + struct + { + uchar obuf[MAXIO]; /* output buffer */ + uchar ibuf[MAXIO]; /* input buffer */ + } u; + char dbuf[1]; +} buf; + +#define cbuf u.obuf +#define xbuf u.ibuf + +#pragma varargck type "A" int +#pragma varargck type "D" Adr* +#pragma varargck type "N" Adr* +#pragma varargck type "P" Prog* +#pragma varargck type "S" char* + +EXTERN long HEADR; /* length of header */ +EXTERN int HEADTYPE; /* type of header */ +EXTERN vlong INITDAT; /* data location */ +EXTERN vlong INITRND; /* data round above text location */ +EXTERN vlong INITTEXT; /* text location */ +EXTERN char* INITENTRY; /* entry point */ +EXTERN int M64; +EXTERN long autosize; +EXTERN Biobuf bso; +EXTERN long bsssize; +EXTERN int cbc; +EXTERN uchar* cbp; +EXTERN int cout; +EXTERN Auto* curauto; +EXTERN Auto* curhist; +EXTERN Prog* curp; +EXTERN Prog* curtext; +EXTERN Prog* datap; +EXTERN long datsize; +EXTERN char debug[128]; +EXTERN Prog* etextp; +EXTERN Prog* firstp; +EXTERN char fnuxi8[8]; +EXTERN char* noname; +EXTERN Sym* hash[NHASH]; +EXTERN Sym* histfrog[MAXHIST]; +EXTERN int histfrogp; +EXTERN int histgen; +EXTERN char* library[50]; +EXTERN char* libraryobj[50]; +EXTERN int libraryp; +EXTERN char* hunk; +EXTERN char inuxi1[1]; +EXTERN char inuxi2[2]; +EXTERN char inuxi4[4]; +EXTERN Prog* lastp; +EXTERN long lcsize; +EXTERN char literal[32]; +EXTERN int nerrors; +EXTERN long nhunk; +EXTERN vlong instoffset; +EXTERN Opcross opcross[10]; +EXTERN Oprang oprange[ALAST]; +EXTERN char* outfile; +EXTERN vlong pc; +EXTERN uchar repop[ALAST]; +EXTERN long symsize; +EXTERN Prog* textp; +EXTERN long textsize; +EXTERN long thunk; +EXTERN int version; +EXTERN char xcmp[32][32]; +EXTERN Prog zprg; +EXTERN int dtype; + +EXTERN struct +{ + Count branch; + Count fcmp; + Count load; + Count mfrom; + Count page; + Count jump; +} nop; + +extern char* anames[]; +extern Optab optab[]; + +int Aconv(Fmt*); +int Dconv(Fmt*); +int Nconv(Fmt*); +int Pconv(Fmt*); +int Sconv(Fmt*); +int aclass(Adr*); +void addhist(long, int); +void addnop(Prog*); +void append(Prog*, Prog*); +void asmb(void); +void asmlc(void); +int asmout(Prog*, Optab*, int); +void asmsym(void); +long atolwhex(char*); +vlong atovlwhex(char*); +Prog* brloop(Prog*); +Biobuf bso; +void buildop(void); +void buildrep(int, int); +void cflush(void); +int cmp(int, int); +int compound(Prog*); +double cputime(void); +void datblk(long, long); +void diag(char*, ...); +void dodata(void); +void doprof1(void); +void doprof2(void); +u64int entryvalue(void); +void errorexit(void); +void exchange(Prog*); +int find1(long, int); +void follow(void); +void gethunk(void); +void histtoauto(void); +vlong ieeedtov(Ieee*); +double ieeedtod(Ieee*); +long ieeedtof(Ieee*); +int isnop(Prog*); +void ldobj(int, long, char*); +void loadlib(int, int); +void listinit(void); +Sym* lookup(char*, int); +void lput(long); +void mkfwd(void); +void* mysbrk(ulong); +void names(void); +void nocache(Prog*); +void noops(void); +void nuxiinit(void); +void objfile(char*); +int ocmp(const void*, const void*); +long opirr(int); +Optab* oplook(Prog*); +long oprrr(int); +void patch(void); +void prasm(Prog*); +void prepend(Prog*, Prog*); +Prog* prg(void); +int pseudo(Prog*); +void putsymb(char*, int, long, int); +long regoff(Adr*); +int relinv(int); +vlong rnd(vlong, vlong); +void sched(Prog*, Prog*); +void span(void); +void strnput(char*, int); +void undef(void); +void xdefine(char*, int, long); +void xfol(Prog*); +void xfol(Prog*); +void nopstat(char*, Count*); diff --git a/sys/src/cmd/4l/list.c b/sys/src/cmd/4l/list.c new file mode 100644 index 0000000..66939f9 --- /dev/null +++ b/sys/src/cmd/4l/list.c @@ -0,0 +1,281 @@ +#include "l.h" + +void +listinit(void) +{ + + fmtinstall('A', Aconv); + fmtinstall('D', Dconv); + fmtinstall('P', Pconv); + fmtinstall('S', Sconv); + fmtinstall('N', Nconv); +} + +void +prasm(Prog *p) +{ + print("%P\n", p); +} + +int +Pconv(Fmt *fp) +{ + char str[STRINGSZ], *s; + Prog *p; + int a; + + p = va_arg(fp->args, Prog*); + curp = p; + a = p->as; + if(a == ADATA || a == ADYNT || a == AINIT) + sprint(str, "(%ld) %A %D/%d,%D", + p->line, a, &p->from, p->reg, &p->to); + else{ + s = str; + s += sprint(s, "(%ld)", p->line); + if(p->mark & NOSCHED) + s += sprint(s, "*"); + if(p->reg == NREG) + sprint(s, " %A %D,%D", + a, &p->from, &p->to); + else + if(p->from.type != D_FREG) + sprint(s, " %A %D,R%d,%D", + a, &p->from, p->reg, &p->to); + else + sprint(s, " %A %D,F%d,%D", + a, &p->from, p->reg, &p->to); + } + return fmtstrcpy(fp, str); +} + +int +Aconv(Fmt *fp) +{ + char *s; + int a; + + a = va_arg(fp->args, int); + s = "???"; + if(a >= AXXX && a < ALAST) + s = anames[a]; + return fmtstrcpy(fp, s); +} + +int +Dconv(Fmt *fp) +{ + char str[STRINGSZ]; + Adr *a; + long v; + + a = va_arg(fp->args, Adr*); + switch(a->type) { + + default: + sprint(str, "GOK-type(%d)", a->type); + break; + + case D_NONE: + str[0] = 0; + if(a->name != D_NONE || a->reg != NREG || a->sym != S) + sprint(str, "%N(R%d)(NONE)", a, a->reg); + break; + + case D_CONST: + sprint(str, "$%N", a); + if(a->reg != NREG) + sprint(str, "%N(R%d)(CONST)", a, a->reg); + break; + + case D_OCONST: + sprint(str, "$*$%N", a); + if(a->reg != NREG) + sprint(str, "%N(R%d)(CONST)", a, a->reg); + break; + + case D_OREG: + if(a->reg != NREG) + sprint(str, "%N(R%d)", a, a->reg); + else + sprint(str, "%N", a); + break; + + case D_REG: + sprint(str, "R%d", a->reg); + if(a->name != D_NONE || a->sym != S) + sprint(str, "%N(R%d)(REG)", a, a->reg); + break; + + case D_MREG: + sprint(str, "M%d", a->reg); + if(a->name != D_NONE || a->sym != S) + sprint(str, "%N(R%d)(REG)", a, a->reg); + break; + + case D_FREG: + sprint(str, "F%d", a->reg); + if(a->name != D_NONE || a->sym != S) + sprint(str, "%N(R%d)(REG)", a, a->reg); + break; + + case D_FCREG: + sprint(str, "FC%d", a->reg); + if(a->name != D_NONE || a->sym != S) + sprint(str, "%N(R%d)(REG)", a, a->reg); + break; + + case D_LO: + sprint(str, "LO"); + if(a->name != D_NONE || a->sym != S) + sprint(str, "%N(LO)(REG)", a); + break; + + case D_HI: + sprint(str, "HI"); + if(a->name != D_NONE || a->sym != S) + sprint(str, "%N(HI)(REG)", a); + break; + + case D_BRANCH: /* botch */ /* XXX */ + if(curp->cond != P) { + v = curp->cond->pc; + if(v >= INITTEXT) + v -= INITTEXT-HEADR; + if(a->sym != S) + sprint(str, "%s+%.5lux(BRANCH)", a->sym->name, v); + else + sprint(str, "%.5lux(BRANCH)", v); + } else + if(a->sym != S) + sprint(str, "%s+%ld(APC)", a->sym->name, a->offset); + else + sprint(str, "%ld(APC)", a->offset); + break; + + case D_VCONST: + sprint(str, "$%lld", *a->vval); + break; + + case D_FCONST: + sprint(str, "$%e", ieeedtod(a->ieee)); + break; + + case D_SCONST: + sprint(str, "$\"%S\"", a->sval); + break; + } + return fmtstrcpy(fp, str); +} + +int +Nconv(Fmt *fp) +{ + char str[STRINGSZ]; + Adr *a; + Sym *s; + + a = va_arg(fp->args, Adr*); + s = a->sym; + switch(a->name) { + default: + sprint(str, "GOK-name(%d)", a->name); + break; + + case D_NONE: + sprint(str, "%ld", a->offset); + break; + + case D_EXTERN: + if(s == S) + sprint(str, "%ld(SB)", a->offset); + else + sprint(str, "%s+%ld(SB)", s->name, a->offset); + break; + + case D_STATIC: + if(s == S) + sprint(str, "<>+%ld(SB)", a->offset); + else + sprint(str, "%s<>+%ld(SB)", s->name, a->offset); + break; + + case D_AUTO: + if(s == S) + sprint(str, "%ld(SP)", a->offset); + else + sprint(str, "%s-%ld(SP)", s->name, -a->offset); + break; + + case D_PARAM: + if(s == S) + sprint(str, "%ld(FP)", a->offset); + else + sprint(str, "%s+%ld(FP)", s->name, a->offset); + break; + } + + return fmtstrcpy(fp, str); +} + +int +Sconv(Fmt *fp) +{ + int i, c; + char str[STRINGSZ], *p, *a; + + a = va_arg(fp->args, char*); + p = str; + for(i=0; i= 'a' && c <= 'z' || + c >= 'A' && c <= 'Z' || + c >= '0' && c <= '9' || + c == ' ' || c == '%') { + *p++ = c; + continue; + } + *p++ = '\\'; + switch(c) { + case 0: + *p++ = 'z'; + continue; + case '\\': + case '"': + *p++ = c; + continue; + case '\n': + *p++ = 'n'; + continue; + case '\t': + *p++ = 't'; + continue; + } + *p++ = (c>>6) + '0'; + *p++ = ((c>>3) & 7) + '0'; + *p++ = (c & 7) + '0'; + } + *p = 0; + return fmtstrcpy(fp, str); +} + +void +diag(char *fmt, ...) +{ + char buf[STRINGSZ], *tn; + va_list arg; + + tn = "??none??"; + if(curtext != P && curtext->from.sym != S) + tn = curtext->from.sym->name; + va_start(arg, fmt); + vseprint(buf, buf+sizeof(buf), fmt, arg); + va_end(arg); + print("%s: %s\n", tn, buf); + + nerrors++; + if(nerrors > 10) { + print("too many errors\n"); + errorexit(); + } +} diff --git a/sys/src/cmd/4l/mkfile b/sys/src/cmd/4l/mkfile new file mode 100644 index 0000000..5ba9cf8 --- /dev/null +++ b/sys/src/cmd/4l/mkfile @@ -0,0 +1,38 @@ +link) { + + /* find out how much arg space is used in this TEXT */ + if(p->to.type == D_OREG && p->to.reg == REGSP) + if(p->to.offset > curframe) + curframe = p->to.offset; + + switch(p->as) { + case ATEXT: + if(curtext && curtext->from.sym) { + curtext->from.sym->frame = curframe; + curtext->from.sym->become = curbecome; + if(curbecome > maxbecome) + maxbecome = curbecome; + } + curframe = 0; + curbecome = 0; + + p->mark |= LABEL|LEAF|SYNC; + if(p->link) + p->link->mark |= LABEL; + curtext = p; + break; + + /* too hard, just leave alone */ + case AMOVW: + case AMOVV: + if(p->to.type == D_FCREG || + p->to.type == D_MREG) { + p->mark |= LABEL|SYNC; + break; + } + if(p->from.type == D_FCREG || + p->from.type == D_MREG) { + p->mark |= LABEL|SYNC; + addnop(p); + addnop(p); + nop.mfrom.count += 2; + nop.mfrom.outof += 2; + break; + } + break; + + /* too hard, just leave alone */ + case ACASE: + case ASYSCALL: + case AWORD: + case ATLBWR: + case ATLBWI: + case ATLBP: + case ATLBR: + p->mark |= LABEL|SYNC; + break; + + case ANOR: + if(p->to.type == D_REG && p->to.reg == REGZERO) + p->mark |= LABEL|SYNC; + break; + + case ARET: + /* special form of RET is BECOME */ + if(p->from.type == D_CONST) + if(p->from.offset > curbecome) + curbecome = p->from.offset; + + if(p->link != P) + p->link->mark |= LABEL; + break; + + case ANOP: + q1 = p->link; + q->link = q1; /* q is non-nop */ + q1->mark |= p->mark; + continue; + + case ABCASE: + p->mark |= LABEL|SYNC; + goto dstlab; + + case ABGEZAL: + case ABLTZAL: + case AJAL: + if(curtext != P) + curtext->mark &= ~LEAF; + + case AJMP: + case ABEQ: + case ABGEZ: + case ABGTZ: + case ABLEZ: + case ABLTZ: + case ABNE: + case ABFPT: + case ABFPF: + p->mark |= BRANCH; + + dstlab: + q1 = p->cond; + if(q1 != P) { + while(q1->as == ANOP) { + q1 = q1->link; + p->cond = q1; + } + if(!(q1->mark & LEAF)) + q1->mark |= LABEL; + } else + p->mark |= LABEL; + q1 = p->link; + if(q1 != P) + q1->mark |= LABEL; + break; + } + q = p; + } + + if(curtext && curtext->from.sym) { + curtext->from.sym->frame = curframe; + curtext->from.sym->become = curbecome; + if(curbecome > maxbecome) + maxbecome = curbecome; + } + + if(debug['b']) + print("max become = %d\n", maxbecome); + xdefine("ALEFbecome", STEXT, maxbecome); + + curtext = 0; + for(p = firstp; p != P; p = p->link) { + switch(p->as) { + case ATEXT: + curtext = p; + break; + case AJAL: + if(curtext != P && curtext->from.sym != S && curtext->to.offset >= 0) { + o = maxbecome - curtext->from.sym->frame; + if(o <= 0) + break; + /* calling a become or calling a variable */ + if(p->to.sym == S || p->to.sym->become) { + curtext->to.offset += o; + if(debug['b']) { + curp = p; + print("%D calling %D increase %d\n", + &curtext->from, &p->to, o); + } + } + } + break; + } + } + + for(p = firstp; p != P; p = p->link) { + o = p->as; + switch(o) { + case ATEXT: + curtext = p; + autosize = p->to.offset + 8; + if(autosize <= 8) + if(curtext->mark & LEAF) { + p->to.offset = -8; + autosize = 0; + } + + q = p; + if(autosize) { + if(autosize & 7) + Bprint(&bso, "odd stack in: %s\n", + curtext->from.sym->name); + q = prg(); + q->as = AADD; + q->line = p->line; + q->from.type = D_CONST; + q->from.offset = -autosize; + q->to.type = D_REG; + q->to.reg = REGSP; + + q->link = p->link; + p->link = q; + } else + if(!(curtext->mark & LEAF)) { + if(debug['v']) + Bprint(&bso, "save suppressed in: %s\n", + curtext->from.sym->name); + Bflush(&bso); + curtext->mark |= LEAF; + } + + if(curtext->mark & LEAF) { + if(curtext->from.sym) + curtext->from.sym->type = SLEAF; + break; + } + + q1 = prg(); + q1->as = AMOVW; + q1->line = p->line; + q1->from.type = D_REG; + q1->from.reg = REGLINK; + q1->to.type = D_OREG; + q1->from.offset = 0; + q1->to.reg = REGSP; + + q1->link = q->link; + q->link = q1; + break; + + case ARET: + nocache(p); + if(p->from.type == D_CONST) + goto become; + if(curtext->mark & LEAF) { + if(!autosize) { + p->as = AJMP; + p->from = zprg.from; + p->to.type = D_OREG; + p->to.offset = 0; + p->to.reg = REGLINK; + p->mark |= BRANCH; + break; + } + + p->as = AADD; + p->from.type = D_CONST; + p->from.offset = autosize; + p->to.type = D_REG; + p->to.reg = REGSP; + + q = prg(); + q->as = AJMP; + q->line = p->line; + q->to.type = D_OREG; + q->to.offset = 0; + q->to.reg = REGLINK; + q->mark |= BRANCH; + + q->link = p->link; + p->link = q; + break; + } + p->as = AMOVW; + p->from.type = D_OREG; + p->from.offset = 0; + p->from.reg = REGSP; + p->to.type = D_REG; + p->to.reg = 2; + + q = p; + if(autosize) { + q = prg(); + q->as = AADD; + q->line = p->line; + q->from.type = D_CONST; + q->from.offset = autosize; + q->to.type = D_REG; + q->to.reg = REGSP; + + q->link = p->link; + p->link = q; + } + + q1 = prg(); + q1->as = AJMP; + q1->line = p->line; + q1->to.type = D_OREG; + q1->to.offset = 0; + q1->to.reg = 2; + q1->mark |= BRANCH; + + q1->link = q->link; + q->link = q1; + break; + + become: + if(curtext->mark & LEAF) { + + q = prg(); + q->line = p->line; + q->as = AJMP; + q->from = zprg.from; + q->to = p->to; + q->cond = p->cond; + q->link = p->link; + q->mark |= BRANCH; + p->link = q; + + p->as = AADD; + p->from = zprg.from; + p->from.type = D_CONST; + p->from.offset = autosize; + p->to = zprg.to; + p->to.type = D_REG; + p->to.reg = REGSP; + + break; + } + q = prg(); + q->line = p->line; + q->as = AJMP; + q->from = zprg.from; + q->to = p->to; + q->cond = p->cond; + q->link = p->link; + q->mark |= BRANCH; + p->link = q; + + q = prg(); + q->line = p->line; + q->as = AADD; + q->from.type = D_CONST; + q->from.offset = autosize; + q->to.type = D_REG; + q->to.reg = REGSP; + q->link = p->link; + p->link = q; + + p->as = AMOVW; + p->from = zprg.from; + p->from.type = D_OREG; + p->from.offset = 0; + p->from.reg = REGSP; + p->to = zprg.to; + p->to.type = D_REG; + p->to.reg = REGLINK; + + break; + } + } + + curtext = P; + q = P; /* p - 1 */ + q1 = firstp; /* top of block */ + o = 0; /* count of instructions */ + for(p = firstp; p != P; p = p1) { + p1 = p->link; + o++; + if(p->mark & NOSCHED){ + if(q1 != p){ + sched(q1, q); + } + for(; p != P; p = p->link){ + if(!(p->mark & NOSCHED)) + break; + q = p; + } + p1 = p; + q1 = p; + o = 0; + continue; + } + if(p->mark & (LABEL|SYNC)) { + if(q1 != p) + sched(q1, q); + q1 = p; + o = 1; + } + if(p->mark & (BRANCH|SYNC)) { + sched(q1, p); + q1 = p1; + o = 0; + } + if(o >= NSCHED) { + sched(q1, p); + q1 = p1; + o = 0; + } + q = p; + } +} + +void +addnop(Prog *p) +{ + Prog *q; + + q = prg(); + q->as = ANOR; + q->line = p->line; + q->from.type = D_REG; + q->from.reg = REGZERO; + q->to.type = D_REG; + q->to.reg = REGZERO; + + q->link = p->link; + p->link = q; +} + +void +nocache(Prog *p) +{ + p->optab = 0; + p->from.class = 0; + p->to.class = 0; +} diff --git a/sys/src/cmd/4l/obj.c b/sys/src/cmd/4l/obj.c new file mode 100644 index 0000000..3a1aaef --- /dev/null +++ b/sys/src/cmd/4l/obj.c @@ -0,0 +1,1395 @@ +#define EXTERN +#include "l.h" +#include + +#ifndef DEFAULT +#define DEFAULT '9' +#endif + +char *noname = ""; +char symname[] = SYMDEF; +char thechar = '4'; +char *thestring = "mips64"; + +/* + * -H0 -T0x40004C -D0x10000000 is abbrev unix + * -H1 -T0x80020000 -R4 is bootp() format for 3k + * -H2 -T4128 -R4096 is plan9 mips2 format + * -H3 -T0x80020000 -R8 is bootp() format for 4k + * -H4 -T0x400000 -R4 is sgi unix coff executable + * -H5 -T0x4000A0 -R4 is sgi unix elf executable + * -H6 -T0x4000A0 -R4 is plan9 mips format + */ + +void +main(int argc, char *argv[]) +{ + int c; + char *a; + + Binit(&bso, 1, OWRITE); + cout = -1; + listinit(); + outfile = 0; + nerrors = 0; + curtext = P; + HEADTYPE = -1; + INITTEXT = -1; + INITDAT = -1; + INITRND = -1; + INITENTRY = 0; + M64 = 1; + + ARGBEGIN { + default: + c = ARGC(); + if(c >= 0 && c < sizeof(debug)) + debug[c]++; + break; + case 'o': + outfile = ARGF(); + break; + case 'E': + a = ARGF(); + if(a) + INITENTRY = a; + break; + case 'T': + a = ARGF(); + if(a) + INITTEXT = atolwhex(a); + break; + case 'D': + a = ARGF(); + if(a) + INITDAT = atolwhex(a); + break; + case 'R': + a = ARGF(); + if(a) + INITRND = atolwhex(a); + break; + case 'H': + a = ARGF(); + if(a) + HEADTYPE = atolwhex(a); + /* do something about setting INITTEXT */ + break; + } ARGEND + + USED(argc); + + if(*argv == 0) { + diag("usage: vl [-options] objects"); + errorexit(); + } + if(!debug['9'] && !debug['U'] && !debug['B']) + debug[DEFAULT] = 1; + if(HEADTYPE == -1) { + if(debug['U']) + HEADTYPE = 0; + if(debug['B']) + HEADTYPE = 1; + if(debug['9']) + HEADTYPE = 2; + } + switch(HEADTYPE) { + default: + diag("unknown -H option"); + errorexit(); + + case 0: /* unix simple */ + HEADR = 20L+56L; + if(INITTEXT == -1) + INITTEXT = 0x40004CL; + if(INITDAT == -1) + INITDAT = 0x10000000L; + if(INITRND == -1) + INITRND = 0; + break; + case 1: /* boot for 3k */ + HEADR = 20L+60L; + if(INITTEXT == -1) + INITTEXT = 0x80020000L; + if(INITDAT == -1) + INITDAT = 0; + if(INITRND == -1) + INITRND = 4; + break; + case 2: /* plan 9 mips2 */ + HEADR = 32L; + if(INITTEXT == -1) + INITTEXT = 4128; + if(INITDAT == -1) + INITDAT = 0; + if(INITRND == -1) + INITRND = 4096; + break; + case 3: /* boot for 4k */ + HEADR = 20L+56L+3*40L; + if(INITTEXT == -1) + INITTEXT = 0x80020000L; + if(INITDAT == -1) + INITDAT = 0; + if(INITRND == -1) + INITRND = 8; + break; + case 4: /* sgi unix coff executable */ + HEADR = 20L+56L+3*40L; + if(INITTEXT == -1) + INITTEXT = 0x00400000L+HEADR; + if(INITDAT == -1) + INITDAT = 0x10000000; + if(INITRND == -1) + INITRND = 0; + break; + case 5: /* sgi unix elf executable */ + HEADR = rnd(52L+3*32L, 16); + if(INITTEXT == -1) + INITTEXT = 0x00400000L+HEADR; + if(INITDAT == -1) + INITDAT = 0x10000000; + if(INITRND == -1) + INITRND = 0; + break; + case 6: /* plan 9 mips */ + HEADR = 32L; + if(INITTEXT == -1) + INITTEXT = 4128; + if(INITDAT == -1) + INITDAT = 0; + if(INITRND == -1) + INITRND = 4096; + break; + case 7: /* elf64 boot */ + HEADR = rnd(64L+3*56L, 16); + if(INITTEXT == -1) + INITTEXT = 0xa800000020040000ULL; + if(INITDAT == -1) + INITDAT = 0; + if(INITRND == -1) + INITRND = 8; + break; + } + + if(INITDAT != 0 && INITRND != 0) + print("warning: -D0x%llux is ignored because of -R0x%llux\n", + INITDAT, INITRND); + if(debug['v']) + Bprint(&bso, "HEADER = -H0x%d -T0x%llux -D0x%llux -R0x%llux\n", + HEADTYPE, INITTEXT, INITDAT, INITRND); + Bflush(&bso); + zprg.as = AGOK; + zprg.reg = NREG; + zprg.from.name = D_NONE; + zprg.from.type = D_NONE; + zprg.from.reg = NREG; + zprg.to = zprg.from; + buildop(); + histgen = 0; + textp = P; + datap = P; + pc = 0; + dtype = 4; + if(outfile == 0) + outfile = "4.out"; + cout = create(outfile, 1, 0775); + if(cout < 0) { + diag("%s: cannot create", outfile); + errorexit(); + } + nuxiinit(); + + version = 0; + cbp = buf.cbuf; + cbc = sizeof(buf.cbuf); + firstp = prg(); + lastp = firstp; + + if(INITENTRY == 0) { + INITENTRY = "_main"; + if(debug['p']) + INITENTRY = "_mainp"; + if(!debug['l']) + lookup(INITENTRY, 0)->type = SXREF; + } else + lookup(INITENTRY, 0)->type = SXREF; + + while(*argv) + objfile(*argv++); + if(!debug['l']) + loadlib(0, libraryp); + firstp = firstp->link; + if(firstp == P) + goto out; + patch(); + if(debug['p']) + if(debug['1']) + doprof1(); + else + doprof2(); + dodata(); + follow(); + if(firstp == P) + goto out; + noops(); + span(); + asmb(); + undef(); + +out: + if(debug['v']) { + Bprint(&bso, "%5.2f cpu time\n", cputime()); + Bprint(&bso, "%ld memory used\n", thunk); + Bprint(&bso, "%d sizeof adr\n", sizeof(Adr)); + Bprint(&bso, "%d sizeof prog\n", sizeof(Prog)); + } + Bflush(&bso); + errorexit(); +} + +void +loadlib(int beg, int end) +{ + int i, t; + + for(i=end-1; i>=beg; i--) { + t = libraryp; + if(debug['v']) + Bprint(&bso, "%5.2f autolib: %s\n", cputime(), library[i]); + objfile(library[i]); + if(t != libraryp) + loadlib(t, libraryp); + } +} + +void +errorexit(void) +{ + + if(nerrors) { + if(cout >= 0) + remove(outfile); + exits("error"); + } + exits(0); +} + +void +objfile(char *file) +{ + vlong off, esym, cnt, l; + int f, work; + Sym *s; + char magbuf[SARMAG]; + char name[100], pname[150]; + struct ar_hdr arhdr; + char *e, *start, *stop; + + if(file[0] == '-' && file[1] == 'l') { + if(debug['9']) + sprint(name, "/%s/lib/lib", thestring); + else + sprint(name, "/usr/%clib/lib", thechar); + strcat(name, file+2); + strcat(name, ".a"); + file = name; + } + if(debug['v']) + Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file); + Bflush(&bso); + f = open(file, 0); + if(f < 0) { + diag("cannot open file: %s", file); + errorexit(); + } + l = read(f, magbuf, SARMAG); + if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){ + /* load it as a regular file */ + l = seek(f, 0L, 2); + seek(f, 0L, 0); + ldobj(f, l, file); + close(f); + return; + } + + if(debug['v']) + Bprint(&bso, "%5.2f ldlib: %s\n", cputime(), file); + l = read(f, &arhdr, SAR_HDR); + if(l != SAR_HDR) { + diag("%s: short read on archive file symbol header", file); + goto out; + } + if(strncmp(arhdr.name, symname, strlen(symname))) { + diag("%s: first entry not symbol header", file); + goto out; + } + + esym = SARMAG + SAR_HDR + atolwhex(arhdr.size); + off = SARMAG + SAR_HDR; + + /* + * just bang the whole symbol file into memory + */ + seek(f, off, 0); + cnt = esym - off; + start = malloc(cnt + 10); + cnt = read(f, start, cnt); + if(cnt <= 0){ + close(f); + return; + } + stop = &start[cnt]; + memset(stop, 0, 10); + + work = 1; + while(work){ + if(debug['v']) + Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file); + Bflush(&bso); + work = 0; + for(e = start; e < stop; e = strchr(e+5, 0) + 1) { + s = lookup(e+5, 0); + if(s->type != SXREF) + continue; + sprint(pname, "%s(%s)", file, s->name); + if(debug['v']) + Bprint(&bso, "%5.2f library: %s\n", cputime(), pname); + Bflush(&bso); + l = e[1] & 0xff; + l |= (e[2] & 0xff) << 8; + l |= (e[3] & 0xff) << 16; + l |= (e[4] & 0xff) << 24; + seek(f, l, 0); + l = read(f, &arhdr, SAR_HDR); + if(l != SAR_HDR) + goto bad; + if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag))) + goto bad; + l = atolwhex(arhdr.size); + ldobj(f, l, pname); + if(s->type == SXREF) { + diag("%s: failed to load: %s", file, s->name); + errorexit(); + } + work = 1; + } + } + return; + +bad: + diag("%s: bad or out of date archive", file); +out: + close(f); +} + +int +zaddr(uchar *p, Adr *a, Sym *h[]) +{ + int i, c; + long l; + Sym *s; + Auto *u; + + char *vc; + + c = p[2]; + if(c < 0 || c > NSYM){ + print("sym out of range: %d\n", c); + p[0] = ALAST+1; + return 0; + } + a->type = p[0]; + a->reg = p[1]; + a->sym = h[c]; + a->name = p[3]; + c = 4; + + if(a->reg < 0 || a->reg > NREG) { + print("register out of range %d\n", a->reg); + p[0] = ALAST+1; + return 0; /* force real diagnostic */ + } + + switch(a->type) { + default: + print("unknown type %d\n", a->type); + p[0] = ALAST+1; + return 0; /* force real diagnostic */ + + case D_NONE: + case D_REG: + case D_FREG: + case D_MREG: + case D_FCREG: + case D_LO: + case D_HI: + break; + + case D_BRANCH: + case D_OREG: + case D_CONST: + case D_OCONST: + a->offset = p[4] | (p[5]<<8) | + (p[6]<<16) | (p[7]<<24); + c += 4; + break; + + case D_SCONST: + a->sval = malloc(NSNAME); + memmove(a->sval, p+4, NSNAME); + c += NSNAME; + break; + + case D_FCONST: + a->ieee = malloc(sizeof(Ieee)); + a->ieee->l = p[4] | (p[5]<<8) | + (p[6]<<16) | (p[7]<<24); + a->ieee->h = p[8] | (p[9]<<8) | + (p[10]<<16) | (p[11]<<24); + c += 8; + break; + + case D_VCONST: + a->vval = malloc(sizeof(vlong)); + /* *a->vval = p[4] | (p[5]<<8) | (p[6]<<16) | + (p[7]<<24) | ((vlong)p[8]<<32) | ((vlong)p[9]<<40) | + ((vlong)p[10]<<48) | ((vlong)p[11]<<56); */ + vc = (char*)a->vval; + vc[0] = p[4]; + vc[1] = p[5]; + vc[2] = p[6]; + vc[3] = p[7]; + vc[4] = p[8]; + vc[5] = p[9]; + vc[6] = p[10]; + vc[7] = p[11]; + c += 8; + break; + } + s = a->sym; + if(s == S) + return c; + i = a->name; + if(i != D_AUTO && i != D_PARAM) + return c; + + l = a->offset; + for(u=curauto; u; u=u->link) + if(u->asym == s) + if(u->type == i) { + if(u->aoffset > l) + u->aoffset = l; + return c; + } + + while(nhunk < sizeof(Auto)) + gethunk(); + u = (Auto*)hunk; + nhunk -= sizeof(Auto); + hunk += sizeof(Auto); + + u->link = curauto; + curauto = u; + u->asym = s; + u->aoffset = l; + u->type = i; + return c; +} + +void +addlib(char *obj) +{ + char name[1024], comp[256], *p; + int i; + + if(histfrogp <= 0) + return; + + if(histfrog[0]->name[1] == '/') { + sprint(name, ""); + i = 1; + } else + if(histfrog[0]->name[1] == '.') { + sprint(name, "."); + i = 0; + } else { + if(debug['9']) + sprint(name, "/%s/lib", thestring); + else + sprint(name, "/usr/%clib", thechar); + i = 0; + } + + for(; iname+1); + for(;;) { + p = strstr(comp, "$O"); + if(p == 0) + break; + memmove(p+1, p+2, strlen(p+2)+1); + p[0] = thechar; + } + for(;;) { + p = strstr(comp, "$M"); + if(p == 0) + break; + if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) { + diag("library component too long"); + return; + } + memmove(p+strlen(thestring), p+2, strlen(p+2)+1); + memmove(p, thestring, strlen(thestring)); + } + if(strlen(name) + strlen(comp) + 3 >= sizeof(name)) { + diag("library component too long"); + return; + } + strcat(name, "/"); + strcat(name, comp); + } + for(i=0; iname = malloc(2*(histfrogp+1) + 1); + + u->asym = s; + u->type = type; + u->aoffset = line; + u->link = curhist; + curhist = u; + + j = 1; + for(i=0; ivalue; + s->name[j+0] = k>>8; + s->name[j+1] = k; + j += 2; + } +} + +void +histtoauto(void) +{ + Auto *l; + + while(l = curhist) { + curhist = l->link; + l->link = curauto; + curauto = l; + } +} + +void +collapsefrog(Sym *s) +{ + int i; + + /* + * bad encoding of path components only allows + * MAXHIST components. if there is an overflow, + * first try to collapse xxx/.. + */ + for(i=1; iname+1, "..") == 0) { + memmove(histfrog+i-1, histfrog+i+1, + (histfrogp-i-1)*sizeof(histfrog[0])); + histfrogp--; + goto out; + } + + /* + * next try to collapse . + */ + for(i=0; iname+1, ".") == 0) { + memmove(histfrog+i, histfrog+i+1, + (histfrogp-i-1)*sizeof(histfrog[0])); + goto out; + } + + /* + * last chance, just truncate from front + */ + memmove(histfrog+0, histfrog+1, + (histfrogp-1)*sizeof(histfrog[0])); + +out: + histfrog[histfrogp-1] = s; +} + +void +nopout(Prog *p) +{ + p->as = ANOP; + p->from.type = D_NONE; + p->to.type = D_NONE; +} + +uchar* +readsome(int f, uchar *buf, uchar *good, uchar *stop, int max) +{ + int n; + + n = stop - good; + memmove(buf, good, stop - good); + stop = buf + n; + n = MAXIO - n; + if(n > max) + n = max; + n = read(f, stop, n); + if(n <= 0) + return 0; + return stop + n; +} + +void +ldobj(int f, long c, char *pn) +{ + vlong ipc; + Prog *p, *t; + uchar *bloc, *bsize, *stop; + Sym *h[NSYM], *s, *di; + int v, o, r, skip; + + bsize = buf.xbuf; + bloc = buf.xbuf; + di = S; + +newloop: + memset(h, 0, sizeof(h)); + version++; + histfrogp = 0; + ipc = pc; + skip = 0; + +loop: + if(c <= 0) + goto eof; + r = bsize - bloc; + if(r < 100 && r < c) { /* enough for largest prog */ + bsize = readsome(f, buf.xbuf, bloc, bsize, c); + if(bsize == 0) + goto eof; + bloc = buf.xbuf; + goto loop; + } + o = bloc[0]; /* as */ + if(o <= AXXX || o >= ALAST) { + diag("%s: line %ld: opcode out of range %d", pn, pc-ipc, o); + print(" probably not a .v file\n"); + errorexit(); + } + if(o == ANAME) { + stop = memchr(&bloc[3], 0, bsize-&bloc[3]); + if(stop == 0){ + bsize = readsome(f, buf.xbuf, bloc, bsize, c); + if(bsize == 0) + goto eof; + bloc = buf.xbuf; + stop = memchr(&bloc[3], 0, bsize-&bloc[3]); + if(stop == 0){ + fprint(2, "%s: name too long\n", pn); + errorexit(); + } + } + v = bloc[1]; /* type */ + o = bloc[2]; /* sym */ + bloc += 3; + c -= 3; + + r = 0; + if(v == D_STATIC) + r = version; + s = lookup((char*)bloc, r); + c -= &stop[1] - bloc; + bloc = stop + 1; + + if(debug['W']) + print(" ANAME %s\n", s->name); + h[o] = s; + if((v == D_EXTERN || v == D_STATIC) && s->type == 0) + s->type = SXREF; + if(v == D_FILE) { + if(s->type != SFILE) { + histgen++; + s->type = SFILE; + s->value = histgen; + } + if(histfrogp < MAXHIST) { + histfrog[histfrogp] = s; + histfrogp++; + } else + collapsefrog(s); + } + goto loop; + } + + if(nhunk < sizeof(Prog)) + gethunk(); + p = (Prog*)hunk; + nhunk -= sizeof(Prog); + hunk += sizeof(Prog); + + p->as = o; + p->reg = bloc[1] & 0x7f; + if(bloc[1] & 0x80) + p->mark = NOSCHED; + p->line = bloc[2] | (bloc[3]<<8) | (bloc[4]<<16) | (bloc[5]<<24); + + r = zaddr(bloc+6, &p->from, h) + 6; + r += zaddr(bloc+r, &p->to, h); + bloc += r; + c -= r; + + if(p->reg < 0 || p->reg > NREG) + diag("register out of range %d", p->reg); + + p->link = P; + p->cond = P; + + if(debug['W']) + print("%P\n", p); + + switch(o) { + case AHISTORY: + if(p->to.offset == -1) { + addlib(pn); + histfrogp = 0; + goto loop; + } + addhist(p->line, D_FILE); /* 'z' */ + if(p->to.offset) + addhist(p->to.offset, D_FILE1); /* 'Z' */ + histfrogp = 0; + goto loop; + + case AEND: + histtoauto(); + if(curtext != P) + curtext->to.autom = curauto; + curauto = 0; + curtext = P; + if(c) + goto newloop; + return; + + case AGLOBL: + s = p->from.sym; + if(s == S) { + diag("GLOBL must have a name\n%P", p); + errorexit(); + } + if(s->type == 0 || s->type == SXREF) { + s->type = SBSS; + s->value = 0; + } + if(s->type != SBSS) { + diag("redefinition: %s\n%P", s->name, p); + s->type = SBSS; + s->value = 0; + } + if(p->to.offset > s->value) + s->value = p->to.offset; + break; + + case ADYNT: + if(p->to.sym == S) { + diag("DYNT without a sym\n%P", p); + break; + } + di = p->to.sym; + p->reg = 4; + if(di->type == SXREF) { + if(debug['z']) + Bprint(&bso, "%P set to %d\n", p, dtype); + di->type = SCONST; + di->value = dtype; + dtype += 4; + } + if(p->from.sym == S) + break; + + p->from.offset = di->value; + p->from.sym->type = SDATA; + if(curtext == P) { + diag("DYNT not in text: %P", p); + break; + } + p->to.sym = curtext->from.sym; + p->to.type = D_CONST; + p->link = datap; + datap = p; + break; + + case AINIT: + if(p->from.sym == S) { + diag("INIT without a sym\n%P", p); + break; + } + if(di == S) { + diag("INIT without previous DYNT\n%P", p); + break; + } + p->from.offset = di->value; + p->from.sym->type = SDATA; + p->link = datap; + datap = p; + break; + + case ADATA: + if(p->from.sym == S) { + diag("DATA without a sym\n%P", p); + break; + } + p->link = datap; + datap = p; + break; + + case AGOK: + diag("unknown opcode\n%P", p); + p->pc = pc; + pc++; + break; + + case ATEXT: + if(curtext != P) { + histtoauto(); + curtext->to.autom = curauto; + curauto = 0; + } + skip = 0; + curtext = p; + s = p->from.sym; + if(s == S) { + diag("TEXT must have a name\n%P", p); + errorexit(); + } + autosize = p->to.offset; + if(autosize & 7) { + diag("stack frame not 8 multiple: %s\n%P", s->name, p); + autosize = autosize + 7 & ~7; + p->to.offset = autosize; + } + autosize += 8; + if(s->type != 0 && s->type != SXREF) { + if(p->reg & DUPOK) { + skip = 1; + goto casedef; + } + diag("redefinition: %s\n%P", s->name, p); + } + s->type = STEXT; + s->value = pc; + lastp->link = p; + lastp = p; + p->pc = pc; + pc++; + if(textp == P) { + textp = p; + etextp = p; + goto loop; + } + etextp->cond = p; + etextp = p; + break; + + case ASUB: + case ASUBU: + if(p->from.type == D_CONST) + if(p->from.name == D_NONE) { + p->from.offset = -p->from.offset; + if(p->as == ASUB) + p->as = AADD; + else + p->as = AADDU; + } + goto casedef; + + case AMOVF: + if(skip) + goto casedef; + + if(p->from.type == D_FCONST) { + /* size sb 9 max */ + sprint(literal, "$%lux", ieeedtof(p->from.ieee)); + s = lookup(literal, 0); + if(s->type == 0) { + s->type = SBSS; + s->value = 4; + t = prg(); + t->as = ADATA; + t->line = p->line; + t->from.type = D_OREG; + t->from.sym = s; + t->from.name = D_EXTERN; + t->reg = 4; + t->to = p->from; + t->link = datap; + datap = t; + } + p->from.type = D_OREG; + p->from.sym = s; + p->from.name = D_EXTERN; + p->from.offset = 0; + } + goto casedef; + + case AMOVD: + if(skip) + goto casedef; + + if(p->from.type == D_FCONST) { + /* size sb 18 max */ + sprint(literal, "$%lux.%lux", + p->from.ieee->h, p->from.ieee->l); + s = lookup(literal, 0); + if(s->type == 0) { + s->type = SBSS; + s->value = 8; + t = prg(); + t->as = ADATA; + t->line = p->line; + t->from.type = D_OREG; + t->from.sym = s; + t->from.name = D_EXTERN; + t->reg = 8; + t->to = p->from; + t->link = datap; + datap = t; + } + p->from.type = D_OREG; + p->from.sym = s; + p->from.name = D_EXTERN; + p->from.offset = 0; + } + goto casedef; + + case AMOVV: + if(skip) + goto casedef; + + if(p->from.type == D_VCONST) { + /* size sb 18 max */ + sprint(literal, "$%llux", *p->from.vval); + s = lookup(literal, 0); + if(s->type == 0) { + s->type = SBSS; + s->value = 8; + t = prg(); + t->as = ADATA; + t->line = p->line; + t->from.type = D_OREG; + t->from.sym = s; + t->from.name = D_EXTERN; + t->reg = 8; + t->to = p->from; + t->link = datap; + datap = t; + } + p->from.type = D_OREG; + p->from.sym = s; + p->from.name = D_EXTERN; + p->from.offset = 0; + } + goto casedef; + + default: + casedef: + if(skip) + nopout(p); + + if(p->to.type == D_BRANCH) + p->to.offset += ipc; + lastp->link = p; + lastp = p; + p->pc = pc; + pc++; + break; + } + goto loop; + +eof: + diag("truncated object file: %s", pn); +} + +Sym* +lookup(char *symb, int v) +{ + Sym *s; + char *p; + long h; + int c, l; + + h = v; + for(p=symb; c = *p; p++) + h = h+h+h + c; + l = (p - symb) + 1; + if(h < 0) + h = ~h; + h %= NHASH; + for(s = hash[h]; s != S; s = s->link) + if(s->version == v) + if(memcmp(s->name, symb, l) == 0) + return s; + + while(nhunk < sizeof(Sym)) + gethunk(); + s = (Sym*)hunk; + nhunk -= sizeof(Sym); + hunk += sizeof(Sym); + + s->name = malloc(l); + memmove(s->name, symb, l); + + s->link = hash[h]; + s->type = 0; + s->version = v; + s->value = 0; + hash[h] = s; + return s; +} + +Prog* +prg(void) +{ + Prog *p; + + while(nhunk < sizeof(Prog)) + gethunk(); + p = (Prog*)hunk; + nhunk -= sizeof(Prog); + hunk += sizeof(Prog); + + *p = zprg; + return p; +} + +void +gethunk(void) +{ + char *h; + long nh; + + nh = NHUNK; + if(thunk >= 5L*NHUNK) { + nh = 5L*NHUNK; + if(thunk >= 25L*NHUNK) + nh = 25L*NHUNK; + } + h = mysbrk(nh); + if(h == (char*)-1) { + diag("out of memory"); + errorexit(); + } + hunk = h; + nhunk = nh; + thunk += nh; +} + +void +doprof1(void) +{ + Sym *s; + long n; + Prog *p, *q; + + if(debug['v']) + Bprint(&bso, "%5.2f profile 1\n", cputime()); + Bflush(&bso); + s = lookup("__mcount", 0); + n = 1; + for(p = firstp->link; p != P; p = p->link) { + if(p->as == ATEXT) { + q = prg(); + q->line = p->line; + q->link = datap; + datap = q; + q->as = ADATA; + q->from.type = D_OREG; + q->from.name = D_EXTERN; + q->from.offset = n*4; + q->from.sym = s; + q->reg = 4; + q->to = p->from; + q->to.type = D_CONST; + + q = prg(); + q->line = p->line; + q->pc = p->pc; + q->link = p->link; + p->link = q; + p = q; + p->as = AMOVW; + p->from.type = D_OREG; + p->from.name = D_EXTERN; + p->from.sym = s; + p->from.offset = n*4 + 4; + p->to.type = D_REG; + p->to.reg = REGTMP; + + q = prg(); + q->line = p->line; + q->pc = p->pc; + q->link = p->link; + p->link = q; + p = q; + p->as = AADDU; + p->from.type = D_CONST; + p->from.offset = 1; + p->to.type = D_REG; + p->to.reg = REGTMP; + + q = prg(); + q->line = p->line; + q->pc = p->pc; + q->link = p->link; + p->link = q; + p = q; + p->as = AMOVW; + p->from.type = D_REG; + p->from.reg = REGTMP; + p->to.type = D_OREG; + p->to.name = D_EXTERN; + p->to.sym = s; + p->to.offset = n*4 + 4; + + n += 2; + continue; + } + } + q = prg(); + q->line = 0; + q->link = datap; + datap = q; + + q->as = ADATA; + q->from.type = D_OREG; + q->from.name = D_EXTERN; + q->from.sym = s; + q->reg = 4; + q->to.type = D_CONST; + q->to.offset = n; + + s->type = SBSS; + s->value = n*4; +} + +void +doprof2(void) +{ + Sym *s2, *s4; + Prog *p, *q, *ps2, *ps4; + + if(debug['v']) + Bprint(&bso, "%5.2f profile 2\n", cputime()); + Bflush(&bso); + s2 = lookup("_profin", 0); + s4 = lookup("_profout", 0); + if(s2->type != STEXT || s4->type != STEXT) { + diag("_profin/_profout not defined"); + return; + } + + ps2 = P; + ps4 = P; + for(p = firstp; p != P; p = p->link) { + if(p->as == ATEXT) { + if(p->from.sym == s2) { + ps2 = p; + p->reg = 1; + } + if(p->from.sym == s4) { + ps4 = p; + p->reg = 1; + } + } + } + for(p = firstp; p != P; p = p->link) { + if(p->as == ATEXT) { + if(p->reg & NOPROF) { + for(;;) { + q = p->link; + if(q == P) + break; + if(q->as == ATEXT) + break; + p = q; + } + continue; + } + + /* + * JAL profin, R2 + */ + q = prg(); + q->line = p->line; + q->pc = p->pc; + q->link = p->link; + p->link = q; + p = q; + p->as = AJAL; + p->to.type = D_BRANCH; + p->cond = ps2; + p->to.sym = s2; + + continue; + } + if(p->as == ARET) { + /* + * RET + */ + q = prg(); + q->as = ARET; + q->from = p->from; + q->to = p->to; + q->link = p->link; + p->link = q; + + /* + * JAL profout + */ + p->as = AJAL; + p->from = zprg.from; + p->to = zprg.to; + p->to.type = D_BRANCH; + p->cond = ps4; + p->to.sym = s4; + + p = q; + + continue; + } + } +} + +void +nuxiinit(void) +{ + int i, c; + + for(i=0; i<4; i++) { + c = find1(0x01020304L, i+1); + if(i >= 2) + inuxi2[i-2] = c; + if(i >= 3) + inuxi1[i-3] = c; + inuxi4[i] = c; + + fnuxi8[i] = c+4; + fnuxi8[i+4] = c; + } + if(debug['v']) { + Bprint(&bso, "inuxi = "); + for(i=0; i<1; i++) + Bprint(&bso, "%d", inuxi1[i]); + Bprint(&bso, " "); + for(i=0; i<2; i++) + Bprint(&bso, "%d", inuxi2[i]); + Bprint(&bso, " "); + for(i=0; i<4; i++) + Bprint(&bso, "%d", inuxi4[i]); + Bprint(&bso, "\nfnuxi = "); + for(i=0; i<8; i++) + Bprint(&bso, "%d", fnuxi8[i]); + Bprint(&bso, "\n"); + } + Bflush(&bso); +} + +int +find1(long l, int c) +{ + char *p; + int i; + + p = (char*)&l; + for(i=0; i<4; i++) + if(*p++ == c) + return i; + return 0; +} + +vlong +ieeedtov(Ieee *ieeep) +{ + vlong v; + + v = (vlong)ieeep->l & 0xffffffffLL; + v |= (vlong)ieeep->h << 32; + return v; +} + +long +ieeedtof(Ieee *ieeep) +{ + int exp; + long v; + + if(ieeep->h == 0) + return 0; + exp = (ieeep->h>>20) & ((1L<<11)-1L); + exp -= (1L<<10) - 2L; + v = (ieeep->h & 0xfffffL) << 3; + v |= (ieeep->l >> 29) & 0x7L; + if((ieeep->l >> 28) & 1) { + v++; + if(v & 0x800000L) { + v = (v & 0x7fffffL) >> 1; + exp++; + } + } + if(exp <= -126 || exp >= 130) + diag("double fp to single fp overflow"); + v |= ((exp + 126) & 0xffL) << 23; + v |= ieeep->h & 0x80000000L; + return v; +} + +double +ieeedtod(Ieee *ieeep) +{ + Ieee e; + double fr; + int exp; + + if(ieeep->h & (1L<<31)) { + e.h = ieeep->h & ~(1L<<31); + e.l = ieeep->l; + return -ieeedtod(&e); + } + if(ieeep->l == 0 && ieeep->h == 0) + return 0; + fr = ieeep->l & ((1L<<16)-1L); + fr /= 1L<<16; + fr += (ieeep->l>>16) & ((1L<<16)-1L); + fr /= 1L<<16; + fr += (ieeep->h & (1L<<20)-1L) | (1L<<20); + fr /= 1L<<21; + exp = (ieeep->h>>20) & ((1L<<11)-1L); + exp -= (1L<<10) - 2L; + return ldexp(fr, exp); +} diff --git a/sys/src/cmd/4l/optab.c b/sys/src/cmd/4l/optab.c new file mode 100644 index 0000000..d788c01 --- /dev/null +++ b/sys/src/cmd/4l/optab.c @@ -0,0 +1,221 @@ +#include "l.h" + +#define X 99 + +Optab optab[] = +{ + { ATEXT, C_LEXT, C_NONE, C_LCON, 0, 0, 0 }, + { ATEXT, C_LEXT, C_REG, C_LCON, 0, 0, 0 }, + + { AMOVW, C_REG, C_NONE, C_REG, 1, 4, 0 }, + { AMOVV, C_REG, C_NONE, C_REG, 1, 4, 0 }, + { AMOVB, C_REG, C_NONE, C_REG, 12, 8, 0 }, + { AMOVBU, C_REG, C_NONE, C_REG, 13, 4, 0 }, + { AMOVWU, C_REG, C_NONE, C_REG, 14, 8, 0 }, + + { ASUB, C_REG, C_REG, C_REG, 2, 4, 0 }, + { AADD, C_REG, C_REG, C_REG, 2, 4, 0 }, + { AAND, C_REG, C_REG, C_REG, 2, 4, 0 }, + { ASUB, C_REG, C_NONE, C_REG, 2, 4, 0 }, + { AADD, C_REG, C_NONE, C_REG, 2, 4, 0 }, + { AAND, C_REG, C_NONE, C_REG, 2, 4, 0 }, + + { ASLL, C_REG, C_NONE, C_REG, 9, 4, 0 }, + { ASLL, C_REG, C_REG, C_REG, 9, 4, 0 }, + + { AADDF, C_FREG, C_NONE, C_FREG, 32, 4, 0 }, + { AADDF, C_FREG, C_REG, C_FREG, 32, 4, 0 }, + { ACMPEQF, C_FREG, C_REG, C_NONE, 32, 4, 0 }, + { AABSF, C_FREG, C_NONE, C_FREG, 33, 4, 0 }, + { AMOVF, C_FREG, C_NONE, C_FREG, 33, 4, 0 }, + { AMOVD, C_FREG, C_NONE, C_FREG, 33, 4, 0 }, + + { AMOVW, C_REG, C_NONE, C_SEXT, 7, 4, REGSB }, + { AMOVV, C_REG, C_NONE, C_SEXT, 7, 4, REGSB }, + { AMOVB, C_REG, C_NONE, C_SEXT, 7, 4, REGSB }, + { AMOVBU, C_REG, C_NONE, C_SEXT, 7, 4, REGSB }, + { AMOVWL, C_REG, C_NONE, C_SEXT, 7, 4, REGSB }, + { AMOVW, C_REG, C_NONE, C_SAUTO, 7, 4, REGSP }, + { AMOVV, C_REG, C_NONE, C_SAUTO, 7, 4, REGSP }, + { AMOVB, C_REG, C_NONE, C_SAUTO, 7, 4, REGSP }, + { AMOVBU, C_REG, C_NONE, C_SAUTO, 7, 4, REGSP }, + { AMOVWL, C_REG, C_NONE, C_SAUTO, 7, 4, REGSP }, + { AMOVW, C_REG, C_NONE, C_SOREG, 7, 4, REGZERO }, + { AMOVV, C_REG, C_NONE, C_SOREG, 7, 4, REGZERO }, + { AMOVB, C_REG, C_NONE, C_SOREG, 7, 4, REGZERO }, + { AMOVBU, C_REG, C_NONE, C_SOREG, 7, 4, REGZERO }, + { AMOVWL, C_REG, C_NONE, C_SOREG, 7, 4, REGZERO }, + + { AMOVW, C_SEXT, C_NONE, C_REG, 8, 4, REGSB }, + { AMOVV, C_SEXT, C_NONE, C_REG, 8, 4, REGSB }, + { AMOVB, C_SEXT, C_NONE, C_REG, 8, 4, REGSB }, + { AMOVBU, C_SEXT, C_NONE, C_REG, 8, 4, REGSB }, + { AMOVWL, C_SEXT, C_NONE, C_REG, 8, 4, REGSB }, + { AMOVW, C_SAUTO,C_NONE, C_REG, 8, 4, REGSP }, + { AMOVV, C_SAUTO,C_NONE, C_REG, 8, 4, REGSP }, + { AMOVB, C_SAUTO,C_NONE, C_REG, 8, 4, REGSP }, + { AMOVBU, C_SAUTO,C_NONE, C_REG, 8, 4, REGSP }, + { AMOVWL, C_SAUTO,C_NONE, C_REG, 8, 4, REGSP }, + { AMOVW, C_SOREG,C_NONE, C_REG, 8, 4, REGZERO }, + { AMOVV, C_SOREG,C_NONE, C_REG, 8, 4, REGZERO }, + { AMOVB, C_SOREG,C_NONE, C_REG, 8, 4, REGZERO }, + { AMOVBU, C_SOREG,C_NONE, C_REG, 8, 4, REGZERO }, + { AMOVWL, C_SOREG,C_NONE, C_REG, 8, 4, REGZERO }, + + { AMOVW, C_REG, C_NONE, C_LEXT, 35, 16, REGSB }, + { AMOVV, C_REG, C_NONE, C_LEXT, 35, 16, REGSB }, + { AMOVB, C_REG, C_NONE, C_LEXT, 35, 16, REGSB }, + { AMOVBU, C_REG, C_NONE, C_LEXT, 35, 16, REGSB }, + { AMOVW, C_REG, C_NONE, C_LAUTO, 35, 16, REGSP }, + { AMOVV, C_REG, C_NONE, C_LAUTO, 35, 16, REGSP }, + { AMOVB, C_REG, C_NONE, C_LAUTO, 35, 16, REGSP }, + { AMOVBU, C_REG, C_NONE, C_LAUTO, 35, 16, REGSP }, + { AMOVW, C_REG, C_NONE, C_LOREG, 35, 16, REGZERO }, + { AMOVV, C_REG, C_NONE, C_LOREG, 35, 16, REGZERO }, + { AMOVB, C_REG, C_NONE, C_LOREG, 35, 16, REGZERO }, + { AMOVBU, C_REG, C_NONE, C_LOREG, 35, 16, REGZERO }, + + { AMOVW, C_LEXT, C_NONE, C_REG, 36, 16, REGSB }, + { AMOVV, C_LEXT, C_NONE, C_REG, 36, 16, REGSB }, + { AMOVB, C_LEXT, C_NONE, C_REG, 36, 16, REGSB }, + { AMOVBU, C_LEXT, C_NONE, C_REG, 36, 16, REGSB }, + { AMOVW, C_LAUTO,C_NONE, C_REG, 36, 16, REGSP }, + { AMOVV, C_LAUTO,C_NONE, C_REG, 36, 16, REGSP }, + { AMOVB, C_LAUTO,C_NONE, C_REG, 36, 16, REGSP }, + { AMOVBU, C_LAUTO,C_NONE, C_REG, 36, 16, REGSP }, + { AMOVW, C_LOREG,C_NONE, C_REG, 36, 16, REGZERO }, + { AMOVV, C_LOREG,C_NONE, C_REG, 36, 16, REGZERO }, + { AMOVB, C_LOREG,C_NONE, C_REG, 36, 16, REGZERO }, + { AMOVBU, C_LOREG,C_NONE, C_REG, 36, 16, REGZERO }, + + { AMOVW, C_SECON,C_NONE, C_REG, 3, 4, REGSB }, + { AMOVW, C_SACON,C_NONE, C_REG, 3, 4, REGSP }, + { AMOVW, C_LECON,C_NONE, C_REG, 26, 12, REGSB }, + { AMOVW, C_LACON,C_NONE, C_REG, 26, 12, REGSP }, + { AMOVW, C_ADDCON,C_NONE,C_REG, 3, 4, REGZERO }, + { AMOVV, C_ADDCON,C_NONE,C_REG, 3, 4, REGZERO }, + { AMOVW, C_ANDCON,C_NONE,C_REG, 3, 4, REGZERO }, + { AMOVV, C_ANDCON,C_NONE,C_REG, 3, 4, REGZERO }, + + { AMOVW, C_UCON, C_NONE, C_REG, 24, 4, 0 }, + { AMOVV, C_UCON, C_NONE, C_REG, 24, 4, 0 }, + { AMOVW, C_LCON, C_NONE, C_REG, 19, 8, 0 }, + { AMOVV, C_LCON, C_NONE, C_REG, 19, 8, 0 }, + + { AMOVW, C_HI, C_NONE, C_REG, 20, 4, 0 }, + { AMOVV, C_HI, C_NONE, C_REG, 20, 4, 0 }, + { AMOVW, C_LO, C_NONE, C_REG, 20, 4, 0 }, + { AMOVV, C_LO, C_NONE, C_REG, 20, 4, 0 }, + { AMOVW, C_REG, C_NONE, C_HI, 21, 4, 0 }, + { AMOVV, C_REG, C_NONE, C_HI, 21, 4, 0 }, + { AMOVW, C_REG, C_NONE, C_LO, 21, 4, 0 }, + { AMOVV, C_REG, C_NONE, C_LO, 21, 4, 0 }, + + { AMUL, C_REG, C_REG, C_NONE, 22, 4, 0 }, + + { AADD, C_ADD0CON,C_REG,C_REG, 4, 4, 0 }, + { AADD, C_ADD0CON,C_NONE,C_REG, 4, 4, 0 }, + { AADD, C_ANDCON,C_REG, C_REG, 10, 8, 0 }, + { AADD, C_ANDCON,C_NONE,C_REG, 10, 8, 0 }, + + { AAND, C_AND0CON,C_REG,C_REG, 4, 4, 0 }, + { AAND, C_AND0CON,C_NONE,C_REG, 4, 4, 0 }, + { AAND, C_ADDCON,C_REG, C_REG, 10, 8, 0 }, + { AAND, C_ADDCON,C_NONE,C_REG, 10, 8, 0 }, + + { AADD, C_UCON, C_REG, C_REG, 25, 8, 0 }, + { AADD, C_UCON, C_NONE, C_REG, 25, 8, 0 }, + { AAND, C_UCON, C_REG, C_REG, 25, 8, 0 }, + { AAND, C_UCON, C_NONE, C_REG, 25, 8, 0 }, + + { AADD, C_LCON, C_NONE, C_REG, 23, 12, 0 }, + { AAND, C_LCON, C_NONE, C_REG, 23, 12, 0 }, + { AADD, C_LCON, C_REG, C_REG, 23, 12, 0 }, + { AAND, C_LCON, C_REG, C_REG, 23, 12, 0 }, + + { ASLL, C_SCON, C_REG, C_REG, 16, 4, 0 }, + { ASLL, C_SCON, C_NONE, C_REG, 16, 4, 0 }, + + { ASYSCALL, C_NONE, C_NONE, C_NONE, 5, 4, 0 }, + + { ABEQ, C_REG, C_REG, C_SBRA, 6, 4, 0 }, + { ABEQ, C_REG, C_NONE, C_SBRA, 6, 4, 0 }, + { ABLEZ, C_REG, C_NONE, C_SBRA, 6, 4, 0 }, + { ABFPT, C_NONE, C_NONE, C_SBRA, 6, 4, 0 }, + + { AJMP, C_NONE, C_NONE, C_LBRA, 11, 4, 0 }, + { AJAL, C_NONE, C_NONE, C_LBRA, 11, 4, 0 }, + + { AJMP, C_NONE, C_NONE, C_ZOREG, 18, 4, REGZERO }, + { AJAL, C_NONE, C_NONE, C_ZOREG, 18, 4, REGLINK }, + + { AMOVW, C_SEXT, C_NONE, C_FREG, 27, 4, REGSB }, + { AMOVF, C_SEXT, C_NONE, C_FREG, 27, 4, REGSB }, + { AMOVD, C_SEXT, C_NONE, C_FREG, 27, 4, REGSB }, + { AMOVW, C_SAUTO,C_NONE, C_FREG, 27, 4, REGSP }, + { AMOVF, C_SAUTO,C_NONE, C_FREG, 27, 4, REGSP }, + { AMOVD, C_SAUTO,C_NONE, C_FREG, 27, 4, REGSP }, + { AMOVW, C_SOREG,C_NONE, C_FREG, 27, 4, REGZERO }, + { AMOVF, C_SOREG,C_NONE, C_FREG, 27, 4, REGZERO }, + { AMOVD, C_SOREG,C_NONE, C_FREG, 27, 4, REGZERO }, + + { AMOVW, C_LEXT, C_NONE, C_FREG, 27, 16, REGSB }, + { AMOVF, C_LEXT, C_NONE, C_FREG, 27, 16, REGSB }, + { AMOVD, C_LEXT, C_NONE, C_FREG, 27, 16, REGSB }, + { AMOVW, C_LAUTO,C_NONE, C_FREG, 27, 16, REGSP }, + { AMOVF, C_LAUTO,C_NONE, C_FREG, 27, 16, REGSP }, + { AMOVD, C_LAUTO,C_NONE, C_FREG, 27, 16, REGSP }, + { AMOVW, C_LOREG,C_NONE, C_FREG, 27, 16, REGZERO }, + { AMOVF, C_LOREG,C_NONE, C_FREG, 27, 16, REGZERO }, + { AMOVD, C_LOREG,C_NONE, C_FREG, 27, 16, REGZERO }, + + { AMOVW, C_FREG, C_NONE, C_SEXT, 28, 4, REGSB }, + { AMOVF, C_FREG, C_NONE, C_SEXT, 28, 4, REGSB }, + { AMOVD, C_FREG, C_NONE, C_SEXT, 28, 4, REGSB }, + { AMOVW, C_FREG, C_NONE, C_SAUTO, 28, 4, REGSP }, + { AMOVF, C_FREG, C_NONE, C_SAUTO, 28, 4, REGSP }, + { AMOVD, C_FREG, C_NONE, C_SAUTO, 28, 4, REGSP }, + { AMOVW, C_FREG, C_NONE, C_SOREG, 28, 4, REGZERO }, + { AMOVF, C_FREG, C_NONE, C_SOREG, 28, 4, REGZERO }, + { AMOVD, C_FREG, C_NONE, C_SOREG, 28, 4, REGZERO }, + + { AMOVW, C_FREG, C_NONE, C_LEXT, 28, 16, REGSB }, + { AMOVF, C_FREG, C_NONE, C_LEXT, 28, 16, REGSB }, + { AMOVD, C_FREG, C_NONE, C_LEXT, 28, 16, REGSB }, + { AMOVW, C_FREG, C_NONE, C_LAUTO, 28, 16, REGSP }, + { AMOVF, C_FREG, C_NONE, C_LAUTO, 28, 16, REGSP }, + { AMOVD, C_FREG, C_NONE, C_LAUTO, 28, 16, REGSP }, + { AMOVW, C_FREG, C_NONE, C_LOREG, 28, 16, REGZERO }, + { AMOVF, C_FREG, C_NONE, C_LOREG, 28, 16, REGZERO }, + { AMOVD, C_FREG, C_NONE, C_LOREG, 28, 16, REGZERO }, + + { AMOVW, C_REG, C_NONE, C_FREG, 30, 4, 0 }, + { AMOVW, C_FREG, C_NONE, C_REG, 31, 4, 0 }, + { AMOVV, C_REG, C_NONE, C_FREG, 47, 4, 0 }, + { AMOVV, C_FREG, C_NONE, C_REG, 48, 4, 0 }, + + { AMOVW, C_ADDCON,C_NONE,C_FREG, 34, 8, 0 }, + { AMOVW, C_ANDCON,C_NONE,C_FREG, 34, 8, 0 }, + { AMOVW, C_UCON, C_NONE, C_FREG, 35, 8, 0 }, + { AMOVW, C_LCON, C_NONE, C_FREG, 36, 12, 0 }, + + { AMOVW, C_REG, C_NONE, C_MREG, 37, 4, 0 }, + { AMOVV, C_REG, C_NONE, C_MREG, 37, 4, 0 }, + { AMOVW, C_MREG, C_NONE, C_REG, 38, 4, 0 }, + { AMOVV, C_MREG, C_NONE, C_REG, 38, 4, 0 }, + + { ARFE, C_NONE, C_NONE, C_ZOREG, 39, 8, 0 }, + { AWORD, C_NONE, C_NONE, C_LCON, 40, 4, 0 }, + + { AMOVW, C_REG, C_NONE, C_FCREG, 41, 8, 0 }, + { AMOVV, C_REG, C_NONE, C_FCREG, 41, 8, 0 }, + { AMOVW, C_FCREG,C_NONE, C_REG, 42, 4, 0 }, + { AMOVV, C_FCREG,C_NONE, C_REG, 42, 4, 0 }, + + { ABREAK, C_REG, C_NONE, C_SEXT, 7, 4, REGSB }, /* really CACHE instruction */ + { ABREAK, C_REG, C_NONE, C_SAUTO, 7, 4, REGSP }, + { ABREAK, C_REG, C_NONE, C_SOREG, 7, 4, REGZERO }, + { ABREAK, C_NONE, C_NONE, C_NONE, 5, 4, 0 }, + + { AXXX, C_NONE, C_NONE, C_NONE, 0, 4, 0 }, +}; diff --git a/sys/src/cmd/4l/pass.c b/sys/src/cmd/4l/pass.c new file mode 100644 index 0000000..1a927c2 --- /dev/null +++ b/sys/src/cmd/4l/pass.c @@ -0,0 +1,538 @@ +#include "l.h" + +void +dodata(void) +{ + int i, t; + Sym *s; + Prog *p, *p1; + vlong orig, orig1, v; + + if(debug['v']) + Bprint(&bso, "%5.2f dodata\n", cputime()); + Bflush(&bso); + for(p = datap; p != P; p = p->link) { + s = p->from.sym; + if(p->as == ADYNT || p->as == AINIT) + s->value = dtype; + if(s->type == SBSS) + s->type = SDATA; + if(s->type != SDATA) + diag("initialize non-data (%d): %s\n%P", + s->type, s->name, p); + v = p->from.offset + p->reg; + if(v > s->value) + diag("initialize bounds (%lld): %s\n%P", + s->value, s->name, p); + } + + /* + * pass 1 + * assign 'small' variables to data segment + * (rational is that data segment is more easily + * addressed through offset on R30) + */ + orig = 0; + for(i=0; ilink) { + t = s->type; + if(t != SDATA && t != SBSS) + continue; + v = s->value; + if(v == 0) { + diag("%s: no size", s->name); + v = 1; + } + while(v & 7) + v++; + s->value = v; + if(v > MINSIZ) + continue; + s->value = orig; + orig += v; + s->type = SDATA1; + } + orig1 = orig; + + /* + * pass 2 + * assign 'data' variables to data segment + */ + for(i=0; ilink) { + t = s->type; + if(t != SDATA) { + if(t == SDATA1) + s->type = SDATA; + continue; + } + v = s->value; + while(v & 7) + v++; + s->value = orig; + orig += v; + s->type = SDATA1; + } + + while(orig & 7) + orig++; + datsize = orig; + + /* + * pass 3 + * everything else to bss segment + */ + for(i=0; ilink) { + if(s->type != SBSS) + continue; + v = s->value; + while(v & 7) + v++; + s->value = orig; + orig += v; + } + while(orig & 7) + orig++; + bsssize = orig-datsize; + + /* + * pass 4 + * add literals to all large values. + * at this time: + * small data is allocated DATA + * large data is allocated DATA1 + * large bss is allocated BSS + * the new literals are loaded between + * small data and large data. + */ + orig = 0; + for(p = firstp; p != P; p = p->link) { + if(p->as != AMOVW) + continue; + if(p->from.type != D_CONST) + continue; + if(s = p->from.sym) { + t = s->type; + if(t != SDATA && t != SDATA1 && t != SBSS) + continue; + t = p->from.name; + if(t != D_EXTERN && t != D_STATIC) + continue; + v = s->value + p->from.offset; + if(v >= 0 && v <= 0xffff) + continue; + if(!strcmp(s->name, "setR30")) + continue; + /* size should be 19 max */ + if(strlen(s->name) >= 10) /* has loader address */ + sprint(literal, "$%lux.%lux", (long)s, p->from.offset); + else + sprint(literal, "$%s.%d.%lux", s->name, s->version, p->from.offset); + } else { + if(p->from.name != D_NONE) + continue; + if(p->from.reg != NREG) + continue; + v = p->from.offset; + if(v >= -0x7fff && v <= 0xffff) + continue; + if(!(v & 0xffff)) + continue; + /* size should be 9 max */ + sprint(literal, "$%llux", v); + } + s = lookup(literal, 0); + if(s->type == 0) { + s->type = SDATA; + s->value = orig1+orig; + orig += 8; + p1 = prg(); + p1->line = p->line; + p1->as = ADATA; + p1->from.type = D_OREG; + p1->from.sym = s; + p1->from.name = D_EXTERN; + p1->reg = 4; + p1->to = p->from; + p1->link = datap; + datap = p1; + } + if(s->type != SDATA) + diag("literal not data: %s", s->name); + p->from.type = D_OREG; + p->from.sym = s; + p->from.name = D_EXTERN; + p->from.offset = 0; + nocache(p); + continue; + } + while(orig & 7) + orig++; + /* + * pass 5 + * re-adjust offsets + */ + for(i=0; ilink) { + t = s->type; + if(t == SBSS) { + s->value += orig; + continue; + } + if(t == SDATA1) { + s->type = SDATA; + s->value += orig; + continue; + } + } + datsize += orig; + xdefine("setR30", SDATA, 0L+BIG); + xdefine("bdata", SDATA, 0L); + xdefine("edata", SDATA, datsize); + xdefine("end", SBSS, datsize+bsssize); + xdefine("etext", STEXT, 0L); +} + +void +undef(void) +{ + int i; + Sym *s; + + for(i=0; ilink) + if(s->type == SXREF) + diag("%s: not defined", s->name); +} + +void +follow(void) +{ + if(debug['v']) + Bprint(&bso, "%5.2f follow\n", cputime()); + Bflush(&bso); + + firstp = prg(); + lastp = firstp; + xfol(textp); + + firstp = firstp->link; + lastp->link = P; +} + +void +xfol(Prog *p) +{ + Prog *q, *r; + int a, i; + +loop: + if(p == P) + return; + a = p->as; + if(a == ATEXT) + curtext = p; + if(a == AJMP) { + q = p->cond; + if((p->mark&NOSCHED) || q && (q->mark&NOSCHED)){ + p->mark |= FOLL; + lastp->link = p; + lastp = p; + p = p->link; + xfol(p); + p = q; + if(p && !(p->mark & FOLL)) + goto loop; + return; + } + if(q != P) { + p->mark |= FOLL; + p = q; + if(!(p->mark & FOLL)) + goto loop; + } + } + if(p->mark & FOLL) { + for(i=0,q=p; i<4; i++,q=q->link) { + if(q == lastp || (q->mark&NOSCHED)) + break; + a = q->as; + if(a == ANOP) { + i--; + continue; + } + if(a == AJMP || a == ARET || a == ARFE) + goto copy; + if(!q->cond || (q->cond->mark&FOLL)) + continue; + if(a != ABEQ && a != ABNE) + continue; + copy: + for(;;) { + r = prg(); + *r = *p; + if(!(r->mark&FOLL)) + print("cant happen 1\n"); + r->mark |= FOLL; + if(p != q) { + p = p->link; + lastp->link = r; + lastp = r; + continue; + } + lastp->link = r; + lastp = r; + if(a == AJMP || a == ARET || a == ARFE) + return; + r->as = ABNE; + if(a == ABNE) + r->as = ABEQ; + r->cond = p->link; + r->link = p->cond; + if(!(r->link->mark&FOLL)) + xfol(r->link); + if(!(r->cond->mark&FOLL)) + print("cant happen 2\n"); + return; + } + } + a = AJMP; + q = prg(); + q->as = a; + q->line = p->line; + q->to.type = D_BRANCH; + q->to.offset = p->pc; + q->cond = p; + p = q; + } + p->mark |= FOLL; + lastp->link = p; + lastp = p; + if(a == AJMP || a == ARET || a == ARFE){ + if(p->mark & NOSCHED){ + p = p->link; + goto loop; + } + return; + } + if(p->cond != P) + if(a != AJAL && p->link != P) { + xfol(p->link); + p = p->cond; + if(p == P || (p->mark&FOLL)) + return; + goto loop; + } + p = p->link; + goto loop; +} + +void +patch(void) +{ + vlong c, vexit; + Prog *p, *q; + Sym *s; + int a; + + if(debug['v']) + Bprint(&bso, "%5.2f patch\n", cputime()); + Bflush(&bso); + mkfwd(); + s = lookup("exit", 0); + vexit = s->value; + for(p = firstp; p != P; p = p->link) { + a = p->as; + if(a == ATEXT) + curtext = p; + if((a == AJAL || a == AJMP || a == ARET) && + p->to.type != D_BRANCH && p->to.sym != S) { + s = p->to.sym; + if(s->type != STEXT) { + diag("undefined: %s\n%P", s->name, p); + s->type = STEXT; + s->value = vexit; + } + p->to.offset = s->value; + p->to.type = D_BRANCH; + } + if(p->to.type != D_BRANCH) + continue; + c = p->to.offset; + for(q = firstp; q != P;) { + if(q->forwd != P) + if(c >= q->forwd->pc) { + q = q->forwd; + continue; + } + if(c == q->pc) + break; + q = q->link; + } + if(q == P) { + diag("branch out of range %ld\n%P", c, p); + p->to.type = D_NONE; + } + p->cond = q; + } + + for(p = firstp; p != P; p = p->link) { + if(p->as == ATEXT) + curtext = p; + if(p->cond != P) { + p->cond = brloop(p->cond); + if(p->cond != P) + if(p->to.type == D_BRANCH) + p->to.offset = p->cond->pc; + } + } +} + +#define LOG 5 +void +mkfwd(void) +{ + Prog *p; + long dwn[LOG], cnt[LOG], i; + Prog *lst[LOG]; + + for(i=0; ilink) { + if(p->as == ATEXT) + curtext = p; + i--; + if(i < 0) + i = LOG-1; + p->forwd = P; + dwn[i]--; + if(dwn[i] <= 0) { + dwn[i] = cnt[i]; + if(lst[i] != P) + lst[i]->forwd = p; + lst[i] = p; + } + } +} + +Prog* +brloop(Prog *p) +{ + Prog *q; + int c; + + for(c=0; p!=P;) { + if(p->as != AJMP || (p->mark&NOSCHED)) + return p; + q = p->cond; + if(q <= p) { + c++; + if(q == p || c > 5000) + break; + } + p = q; + } + return P; +} + +long +atolwhex(char *s) +{ + long n; + int f; + + n = 0; + f = 0; + while(*s == ' ' || *s == '\t') + s++; + if(*s == '-' || *s == '+') { + if(*s++ == '-') + f = 1; + while(*s == ' ' || *s == '\t') + s++; + } + if(s[0]=='0' && s[1]){ + if(s[1]=='x' || s[1]=='X'){ + s += 2; + for(;;){ + if(*s >= '0' && *s <= '9') + n = n*16 + *s++ - '0'; + else if(*s >= 'a' && *s <= 'f') + n = n*16 + *s++ - 'a' + 10; + else if(*s >= 'A' && *s <= 'F') + n = n*16 + *s++ - 'A' + 10; + else + break; + } + } else + while(*s >= '0' && *s <= '7') + n = n*8 + *s++ - '0'; + } else + while(*s >= '0' && *s <= '9') + n = n*10 + *s++ - '0'; + if(f) + n = -n; + return n; +} + +vlong +atovlwhex(char *s) +{ + vlong n; + int f; + + n = 0; + f = 0; + while(*s == ' ' || *s == '\t') + s++; + if(*s == '-' || *s == '+') { + if(*s++ == '-') + f = 1; + while(*s == ' ' || *s == '\t') + s++; + } + if(s[0]=='0' && s[1]){ + if(s[1]=='x' || s[1]=='X'){ + s += 2; + for(;;){ + if(*s >= '0' && *s <= '9') + n = n*16 + *s++ - '0'; + else if(*s >= 'a' && *s <= 'f') + n = n*16 + *s++ - 'a' + 10; + else if(*s >= 'A' && *s <= 'F') + n = n*16 + *s++ - 'A' + 10; + else + break; + } + } else + while(*s >= '0' && *s <= '7') + n = n*8 + *s++ - '0'; + } else + while(*s >= '0' && *s <= '9') + n = n*10 + *s++ - '0'; + if(f) + n = -n; + return n; +} + +vlong +rnd(vlong v, vlong r) +{ + vlong c; + + if(r <= 0) + return v; + v += r - 1; + c = v % r; + if(c < 0) + c += r; + v -= c; + return v; +} diff --git a/sys/src/cmd/4l/sched.c b/sys/src/cmd/4l/sched.c new file mode 100644 index 0000000..344bcb8 --- /dev/null +++ b/sys/src/cmd/4l/sched.c @@ -0,0 +1,709 @@ +#include "l.h" + +enum +{ + E_HILO = 1<<0, + E_FCR = 1<<1, + E_MCR = 1<<2, + E_MEM = 1<<3, + E_MEMSP = 1<<4, /* uses offset and size */ + E_MEMSB = 1<<5, /* uses offset and size */ + ANYMEM = E_MEM|E_MEMSP|E_MEMSB, + DELAY = BRANCH|LOAD|FCMP, +}; + +typedef struct Sch Sch; +typedef struct Dep Dep; + +struct Dep +{ + ulong ireg; + ulong freg; + ulong cc; +}; +struct Sch +{ + Prog p; + Dep set; + Dep used; + long soffset; + char size; + char nop; + char comp; +}; + +void regsused(Sch*, Prog*); +int depend(Sch*, Sch*); +int conflict(Sch*, Sch*); +int offoverlap(Sch*, Sch*); +void dumpbits(Sch*, Dep*); + +void +sched(Prog *p0, Prog *pe) +{ + Prog *p, *q; + Sch sch[NSCHED], *s, *t, *u, *se, stmp; + + /* + * build side structure + */ + s = sch; + for(p=p0;; p=p->link) { + memset(s, 0, sizeof(*s)); + s->p = *p; + regsused(s, p); + if(debug['X']) { + Bprint(&bso, "%P\t\tset", &s->p); + dumpbits(s, &s->set); + Bprint(&bso, "; used"); + dumpbits(s, &s->used); + if(s->comp) + Bprint(&bso, "; compound"); + if(s->p.mark & LOAD) + Bprint(&bso, "; load"); + if(s->p.mark & BRANCH) + Bprint(&bso, "; branch"); + if(s->p.mark & FCMP) + Bprint(&bso, "; fcmp"); + Bprint(&bso, "\n"); + } + if(p == pe) + break; + s++; + } + se = s; + + /* + * prepass to move things around + * does nothing, but tries to make + * the actual scheduler work better + */ + for(s=sch; s<=se; s++) { + if(!(s->p.mark & LOAD)) + continue; + /* always good to put nonconflict loads together */ + for(t=s+1; t<=se; t++) { + if(!(t->p.mark & LOAD)) + continue; + if(t->p.mark & BRANCH) + break; + if(conflict(s, t)) + break; + for(u=t-1; u>s; u--) + if(depend(u, t)) + goto no11; + u = s+1; + stmp = *t; + memmove(s+2, u, (uchar*)t - (uchar*)u); + *u = stmp; + break; + } + no11: + + /* put schedule fodder above load */ + for(t=s+1; t<=se; t++) { + if(t->p.mark & BRANCH) + break; + if(s > sch && conflict(s-1, t)) + continue; + for(u=t-1; u>=s; u--) + if(depend(t, u)) + goto no1; + stmp = *t; + memmove(s+1, s, (uchar*)t - (uchar*)s); + *s = stmp; + if(!(s->p.mark & LOAD)) + break; + no1:; + } + } + + for(s=se; s>=sch; s--) { + if(!(s->p.mark & DELAY)) + continue; + if(s < se) + if(!conflict(s, s+1)) + goto out3; + /* + * s is load, s+1 is immediate use of result or end of block + * t is the trial instruction to insert between s and s+1 + */ + if(!debug['Y']) + for(t=s-1; t>=sch; t--) { + if(t->comp) + if(s->p.mark & BRANCH) + goto no2; + if(t->p.mark & DELAY) + if(s >= se || conflict(t, s+1)) + goto no2; + for(u=t+1; u<=s; u++) + if(depend(u, t)) + goto no2; + goto out2; + no2:; + } + if(debug['X']) + Bprint(&bso, "?l%P\n", &s->p); + if(s->p.mark & BRANCH) + s->nop = 1; + if(debug['v']) { + if(s->p.mark & LOAD) { + nop.load.count++; + nop.load.outof++; + } + if(s->p.mark & BRANCH) { + nop.branch.count++; + nop.branch.outof++; + } + if(s->p.mark & FCMP) { + nop.fcmp.count++; + nop.fcmp.outof++; + } + } + continue; + + out2: + if(debug['X']) { + Bprint(&bso, "!l%P\n", &t->p); + Bprint(&bso, "%P\n", &s->p); + } + stmp = *t; + memmove(t, t+1, (uchar*)s - (uchar*)t); + *s = stmp; + s--; + + out3: + if(debug['v']) { + if(s->p.mark & LOAD) + nop.load.outof++; + if(s->p.mark & BRANCH) + nop.branch.outof++; + if(s->p.mark & FCMP) + nop.fcmp.outof++; + } + } + + /* Avoid HI/LO use->set */ + t = sch+1; + for(s=sch; sused.cc & E_HILO) == 0) + continue; + if(t->set.cc & E_HILO) + s->nop = 2; + } + + /* + * put it all back + */ + for(s=sch, p=p0; s<=se; s++, p=q) { + q = p->link; + if(q != s->p.link) { + *p = s->p; + p->link = q; + } + while(s->nop--) + addnop(p); + } + if(debug['X']) { + Bprint(&bso, "\n"); + Bflush(&bso); + } +} + +void +regsused(Sch *s, Prog *realp) +{ + int c, ar, ad, ld, sz; + ulong m; + Prog *p; + + p = &s->p; + s->comp = compound(p); + s->nop = 0; + if(s->comp) { + s->set.ireg |= 1<used.ireg |= 1<as) { + case ATEXT: + curtext = realp; + autosize = p->to.offset + 8; + ad = 1; + break; + case AJAL: + c = p->reg; + if(c == NREG) + c = REGLINK; + s->set.ireg |= 1<set.ireg |= 1<used.cc |= E_FCR; + break; + case ACMPEQD: + case ACMPEQF: + case ACMPGED: + case ACMPGEF: + case ACMPGTD: + case ACMPGTF: + ar = 1; + s->set.cc |= E_FCR; + p->mark |= FCMP; + break; + case AJMP: + ar = 1; + ad = 1; + break; + case AMOVB: + case AMOVBU: + sz = 1; + ld = 1; + break; + case AMOVH: + case AMOVHU: + sz = 2; + ld = 1; + break; + case AMOVF: + case AMOVW: + case AMOVWL: + case AMOVWR: + sz = 4; + ld = 1; + break; + case AMOVD: + case AMOVV: + case AMOVVL: + case AMOVVR: + sz = 8; + ld = 1; + break; + case ADIV: + case ADIVU: + case AMUL: + case AMULU: + case AREM: + case AREMU: + case ADIVV: + case ADIVVU: + case AMULV: + case AMULVU: + case AREMV: + case AREMVU: + s->set.cc = E_HILO; + case AADD: + case AADDU: + case AADDV: + case AADDVU: + case AAND: + case ANOR: + case AOR: + case ASGT: + case ASGTU: + case ASLL: + case ASRA: + case ASRL: + case ASLLV: + case ASRAV: + case ASRLV: + case ASUB: + case ASUBU: + case ASUBV: + case ASUBVU: + case AXOR: + + case AADDD: + case AADDF: + case AADDW: + case ASUBD: + case ASUBF: + case ASUBW: + case AMULF: + case AMULD: + case AMULW: + case ADIVF: + case ADIVD: + case ADIVW: + if(p->reg == NREG) { + if(p->to.type == D_REG || p->to.type == D_FREG) + p->reg = p->to.reg; + if(p->reg == NREG) + print("botch %P\n", p); + } + break; + } + +/* + * flags based on 'to' field + */ + c = p->to.class; + if(c == 0) { + c = aclass(&p->to) + 1; + p->to.class = c; + } + c--; + switch(c) { + default: + print("unknown class %d %D\n", c, &p->to); + + case C_ZCON: + case C_SCON: + case C_ADD0CON: + case C_AND0CON: + case C_ADDCON: + case C_ANDCON: + case C_UCON: + case C_LCON: + case C_NONE: + case C_SBRA: + case C_LBRA: + break; + + case C_HI: + case C_LO: + s->set.cc |= E_HILO; + break; + case C_FCREG: + s->set.cc |= E_FCR; + break; + case C_MREG: + s->set.cc |= E_MCR; + break; + case C_ZOREG: + case C_SOREG: + case C_LOREG: + c = p->to.reg; + s->used.ireg |= 1<size = sz; + s->soffset = regoff(&p->to); + + m = ANYMEM; + if(c == REGSB) + m = E_MEMSB; + if(c == REGSP) + m = E_MEMSP; + + if(ar) + s->used.cc |= m; + else + s->set.cc |= m; + break; + case C_SACON: + case C_LACON: + s->used.ireg |= 1<used.ireg |= 1<used.ireg |= 1<to.reg; + else + s->set.ireg |= 1<to.reg; + break; + case C_FREG: + /* do better -- determine double prec */ + if(ar) { + s->used.freg |= 1<to.reg; + s->used.freg |= 1<<(p->to.reg|1); + } else { + s->set.freg |= 1<to.reg; + s->set.freg |= 1<<(p->to.reg|1); + } + if(ld && p->from.type == D_REG) + p->mark |= LOAD; + break; + case C_SAUTO: + case C_LAUTO: + s->used.ireg |= 1<size = sz; + s->soffset = regoff(&p->to); + + if(ar) + s->used.cc |= E_MEMSP; + else + s->set.cc |= E_MEMSP; + break; + case C_SEXT: + case C_LEXT: + s->used.ireg |= 1<size = sz; + s->soffset = regoff(&p->to); + + if(ar) + s->used.cc |= E_MEMSB; + else + s->set.cc |= E_MEMSB; + break; + } + +/* + * flags based on 'from' field + */ + c = p->from.class; + if(c == 0) { + c = aclass(&p->from) + 1; + p->from.class = c; + } + c--; + switch(c) { + default: + print("unknown class %d %D\n", c, &p->from); + + case C_ZCON: + case C_SCON: + case C_ADD0CON: + case C_AND0CON: + case C_ADDCON: + case C_ANDCON: + case C_UCON: + case C_LCON: + case C_NONE: + case C_SBRA: + case C_LBRA: + break; + case C_HI: + case C_LO: + s->used.cc |= E_HILO; + break; + case C_FCREG: + s->used.cc |= E_FCR; + break; + case C_MREG: + s->used.cc |= E_MCR; + break; + case C_ZOREG: + case C_SOREG: + case C_LOREG: + c = p->from.reg; + s->used.ireg |= 1<mark |= LOAD; + s->size = sz; + s->soffset = regoff(&p->from); + + m = ANYMEM; + if(c == REGSB) + m = E_MEMSB; + if(c == REGSP) + m = E_MEMSP; + + s->used.cc |= m; + break; + case C_SACON: + case C_LACON: + s->used.ireg |= 1<used.ireg |= 1<used.ireg |= 1<from.reg; + break; + case C_FREG: + /* do better -- determine double prec */ + s->used.freg |= 1<from.reg; + s->used.freg |= 1<<(p->from.reg|1); + if(ld && p->to.type == D_REG) + p->mark |= LOAD; + break; + case C_SAUTO: + case C_LAUTO: + s->used.ireg |= 1<mark |= LOAD; + if(ad) + break; + s->size = sz; + s->soffset = regoff(&p->from); + + s->used.cc |= E_MEMSP; + break; + case C_SEXT: + case C_LEXT: + s->used.ireg |= 1<mark |= LOAD; + if(ad) + break; + s->size = sz; + s->soffset = regoff(&p->from); + + s->used.cc |= E_MEMSB; + break; + } + + c = p->reg; + if(c != NREG) { + if(p->from.type == D_FREG || p->to.type == D_FREG) { + s->used.freg |= 1<used.freg |= 1<<(c|1); + } else + s->used.ireg |= 1<set.ireg &= ~(1<set.ireg & (sb->set.ireg|sb->used.ireg)) + return 1; + if(sb->set.ireg & sa->used.ireg) + return 1; + + if(sa->set.freg & (sb->set.freg|sb->used.freg)) + return 1; + if(sb->set.freg & sa->used.freg) + return 1; + + /* + * special case. + * loads from same address cannot pass. + * this is for hardware fifo's and the like + */ + if(sa->used.cc & sb->used.cc & E_MEM) + if(sa->p.reg == sb->p.reg) + if(regoff(&sa->p.from) == regoff(&sb->p.from)) + return 1; + + x = (sa->set.cc & (sb->set.cc|sb->used.cc)) | + (sb->set.cc & sa->used.cc); + if(x) { + /* + * allow SB and SP to pass each other. + * allow SB to pass SB iff doffsets are ok + * anything else conflicts + */ + if(x != E_MEMSP && x != E_MEMSB) + return 1; + x = sa->set.cc | sb->set.cc | + sa->used.cc | sb->used.cc; + if(x & E_MEM) + return 1; + if(offoverlap(sa, sb)) + return 1; + } + + return 0; +} + +int +offoverlap(Sch *sa, Sch *sb) +{ + + if(sa->soffset < sb->soffset) { + if(sa->soffset+sa->size > sb->soffset) + return 1; + return 0; + } + if(sb->soffset+sb->size > sa->soffset) + return 1; + return 0; +} + +/* + * test 2 adjacent instructions + * and find out if inserted instructions + * are desired to prevent stalls. + */ +int +conflict(Sch *sa, Sch *sb) +{ + + if(sa->set.ireg & sb->used.ireg) + return 1; + if(sa->set.freg & sb->used.freg) + return 1; + if(sa->set.cc & sb->used.cc) + return 1; + + return 0; +} + +int +compound(Prog *p) +{ + Optab *o; + + o = oplook(p); + if(o->size != 4) + return 1; + if(p->to.type == D_REG && p->to.reg == REGSB) + return 1; + return 0; +} + +void +dumpbits(Sch *s, Dep *d) +{ + int i; + + for(i=0; i<32; i++) + if(d->ireg & (1<freg & (1<cc & (1<size); + break; + case E_MEMSB: + Bprint(&bso, " SB%d", s->size); + break; + case E_MEMSP: + Bprint(&bso, " SP%d", s->size); + break; + } +} diff --git a/sys/src/cmd/4l/span.c b/sys/src/cmd/4l/span.c new file mode 100644 index 0000000..b4dd0cc --- /dev/null +++ b/sys/src/cmd/4l/span.c @@ -0,0 +1,628 @@ +#include "l.h" + +void +span(void) +{ + Prog *p, *q; + Sym *setext; + Optab *o; + int m, bflag; + vlong c, otxt; + + if(debug['v']) + Bprint(&bso, "%5.2f span\n", cputime()); + Bflush(&bso); + + bflag = 0; + c = INITTEXT; + otxt = c; + for(p = firstp; p != P; p = p->link) { + p->pc = c; + o = oplook(p); + m = o->size; + if(m == 0) { + if(p->as == ATEXT) { + curtext = p; + autosize = p->to.offset + 8; + if(p->from.sym != S) + p->from.sym->value = c; + /* need passes to resolve branches */ + if(c-otxt >= 1L<<17) + bflag = 1; + otxt = c; + continue; + } + diag("zero-width instruction\n%P", p); + continue; + } + c += m; + } + + /* + * if any procedure is large enough to + * generate a large SBRA branch, then + * generate extra passes putting branches + * around jmps to fix. this is rare. + */ + while(bflag) { + if(debug['v']) + Bprint(&bso, "%5.2f span1\n", cputime()); + bflag = 0; + c = INITTEXT; + for(p = firstp; p != P; p = p->link) { + p->pc = c; + o = oplook(p); + if(o->type == 6 && p->cond) { + otxt = p->cond->pc - c; + if(otxt < 0) + otxt = -otxt; + if(otxt >= (1L<<17) - 10) { + q = prg(); + q->link = p->link; + p->link = q; + q->as = AJMP; + q->to.type = D_BRANCH; + q->cond = p->cond; + p->cond = q; + q = prg(); + q->link = p->link; + p->link = q; + q->as = AJMP; + q->to.type = D_BRANCH; + q->cond = q->link->link; + addnop(p->link); + addnop(p); + bflag = 1; + } + } + m = o->size; + if(m == 0) { + if(p->as == ATEXT) { + curtext = p; + autosize = p->to.offset + 8; + if(p->from.sym != S) + p->from.sym->value = c; + continue; + } + diag("zero-width instruction\n%P", p); + continue; + } + c += m; + } + } + c = rnd(c, 8); + + setext = lookup("etext", 0); + if(setext != S) { + setext->value = c; + textsize = c - INITTEXT; + } + if(INITRND) + INITDAT = rnd(c, INITRND); + if(debug['v']) + Bprint(&bso, "tsize = %lux\n", textsize); + Bflush(&bso); +} + +void +xdefine(char *p, int t, long v) +{ + Sym *s; + + s = lookup(p, 0); + if(s->type == 0 || s->type == SXREF) { + s->type = t; + s->value = v; + } +} + +long +regoff(Adr *a) +{ + + instoffset = 0; + aclass(a); + return instoffset; +} + +int +aclass(Adr *a) +{ + Sym *s; + int t; + + switch(a->type) { + case D_NONE: + return C_NONE; + + case D_REG: + return C_REG; + + case D_FREG: + return C_FREG; + + case D_FCREG: + return C_FCREG; + + case D_MREG: + return C_MREG; + + case D_OREG: + switch(a->name) { + case D_EXTERN: + case D_STATIC: + if(a->sym == 0 || a->sym->name == 0) { + print("null sym external\n"); + print("%D\n", a); + return C_GOK; + } + t = a->sym->type; + if(t == 0 || t == SXREF) { + diag("undefined external: %s in %s", + a->sym->name, TNAME); + a->sym->type = SDATA; + } + instoffset = a->sym->value + a->offset - BIG; + if(instoffset >= -BIG && instoffset < BIG) + return C_SEXT; + return C_LEXT; + case D_AUTO: + instoffset = autosize + a->offset; + if(instoffset >= -BIG && instoffset < BIG) + return C_SAUTO; + return C_LAUTO; + + case D_PARAM: + instoffset = autosize + a->offset + 8L; + if(instoffset >= -BIG && instoffset < BIG) + return C_SAUTO; + return C_LAUTO; + case D_NONE: + instoffset = a->offset; + if(instoffset == 0) + return C_ZOREG; + if(instoffset >= -BIG && instoffset < BIG) + return C_SOREG; + return C_LOREG; + } + return C_GOK; + + case D_HI: + return C_LO; + case D_LO: + return C_HI; + + case D_OCONST: + switch(a->name) { + case D_EXTERN: + case D_STATIC: + s = a->sym; + t = s->type; + if(t == 0 || t == SXREF) { + diag("undefined external: %s in %s", + s->name, TNAME); + s->type = SDATA; + } + instoffset = s->value + a->offset + INITDAT; + if(s->type == STEXT || s->type == SLEAF) + instoffset = s->value + a->offset; + return C_LCON; + } + return C_GOK; + + case D_CONST: + switch(a->name) { + + case D_NONE: + instoffset = a->offset; + consize: + if(instoffset > 0) { + if(instoffset <= 0x7fff) + return C_SCON; + if(instoffset <= 0xffff) + return C_ANDCON; + if((instoffset & 0xffff) == 0) + return C_UCON; + return C_LCON; + } + if(instoffset == 0) + return C_ZCON; + if(instoffset >= -0x8000) + return C_ADDCON; + if((instoffset & 0xffff) == 0) + return C_UCON; + return C_LCON; + + case D_EXTERN: + case D_STATIC: + s = a->sym; + if(s == S) + break; + t = s->type; + switch(t) { + case 0: + case SXREF: + diag("undefined external: %s in %s", + s->name, TNAME); + s->type = SDATA; + break; + case SCONST: + instoffset = s->value + a->offset; + goto consize; + case STEXT: + case SLEAF: + instoffset = s->value + a->offset; + return C_LCON; + } + instoffset = s->value + a->offset - BIG; + if(instoffset >= -BIG && instoffset < BIG && instoffset != 0L) + return C_SECON; + instoffset = s->value + a->offset + INITDAT; + return C_LCON; + + case D_AUTO: + instoffset = autosize + a->offset; + if(instoffset >= -BIG && instoffset < BIG) + return C_SACON; + return C_LACON; + + case D_PARAM: + instoffset = autosize + a->offset + 8L; + if(instoffset >= -BIG && instoffset < BIG) + return C_SACON; + return C_LACON; + } + return C_GOK; + + case D_BRANCH: + return C_SBRA; + } + return C_GOK; +} + +Optab* +oplook(Prog *p) +{ + int a1, a2, a3, r, t; + char *c1, *c3; + Optab *o, *e; + + a1 = p->optab; + if(a1) + return optab+(a1-1); + a1 = p->from.class; + if(a1 == 0) { + a1 = aclass(&p->from) + 1; + p->from.class = a1; + } + a1--; + a3 = p->to.class; + if(a3 == 0) { + a3 = aclass(&p->to) + 1; + p->to.class = a3; + } + a3--; + a2 = C_NONE; + if(p->reg != NREG) + a2 = C_REG; + r = p->as; + o = oprange[r].start; + if(o == 0) { + t = opcross[repop[r]][a1][a2][a3]; + if(t) { + p->optab = t+1; + return optab+t; + } + o = oprange[r].stop; /* just generate an error */ + } + e = oprange[r].stop; + c1 = xcmp[a1]; + c3 = xcmp[a3]; + for(; oa2 == a2) + if(c1[o->a1]) + if(c3[o->a3]) { + p->optab = (o-optab)+1; + return o; + } + diag("illegal combination %A %d %d %d", + p->as, a1, a2, a3); + if(!debug['a']) + prasm(p); + o = optab; + p->optab = (o-optab)+1; + return o; +} + +int +cmp(int a, int b) +{ + + if(a == b) + return 1; + switch(a) { + case C_LCON: + if(b == C_ZCON || b == C_SCON || b == C_UCON || + b == C_ADDCON || b == C_ANDCON) + return 1; + break; + case C_ADD0CON: + if(b == C_ADDCON) + return 1; + case C_ADDCON: + if(b == C_ZCON || b == C_SCON) + return 1; + break; + case C_AND0CON: + if(b == C_ANDCON) + return 1; + case C_ANDCON: + if(b == C_ZCON || b == C_SCON) + return 1; + break; + case C_UCON: + if(b == C_ZCON) + return 1; + break; + case C_SCON: + if(b == C_ZCON) + return 1; + break; + case C_LACON: + if(b == C_SACON) + return 1; + break; + case C_LBRA: + if(b == C_SBRA) + return 1; + break; + case C_LEXT: + if(b == C_SEXT) + return 1; + break; + case C_LAUTO: + if(b == C_SAUTO) + return 1; + break; + case C_REG: + if(b == C_ZCON) + return 1; + break; + case C_LOREG: + if(b == C_ZOREG || b == C_SOREG) + return 1; + break; + case C_SOREG: + if(b == C_ZOREG) + return 1; + break; + } + return 0; +} + +int +ocmp(const void *a1, const void *a2) +{ + Optab *p1, *p2; + int n; + + p1 = (Optab*)a1; + p2 = (Optab*)a2; + n = p1->as - p2->as; + if(n) + return n; + n = p1->a1 - p2->a1; + if(n) + return n; + n = p1->a2 - p2->a2; + if(n) + return n; + n = p1->a3 - p2->a3; + if(n) + return n; + return 0; +} + +void +buildop(void) +{ + int i, n, r; + + for(i=0; i<32; i++) + for(n=0; n<32; n++) + xcmp[i][n] = cmp(n, i); + for(n=0; optab[n].as != AXXX; n++) + ; + qsort(optab, n, sizeof(optab[0]), ocmp); + for(i=0; i= 32 || + x >= nelem(opcross)) { + diag("assumptions fail in buildrep"); + errorexit(); + } + repop[as] = x; + p = (opcross + x); + s = oprange[as].start; + e = oprange[as].stop; + for(o=e-1; o>=s; o--) { + n = o-optab; + for(a2=0; a2<2; a2++) { + if(a2) { + if(o->a2 == C_NONE) + continue; + } else + if(o->a2 != C_NONE) + continue; + for(a1=0; a1<32; a1++) { + if(!xcmp[a1][o->a1]) + continue; + for(a3=0; a3<32; a3++) + if(xcmp[a3][o->a3]) + (*p)[a1][a2][a3] = n; + } + } + } + oprange[as].start = 0; +} -- 2.4.11