l.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "../6c/6.out.h"
  5. #ifndef EXTERN
  6. #define EXTERN extern
  7. #endif
  8. #define P ((Prog*)0)
  9. #define S ((Sym*)0)
  10. #define TNAME (curtext?curtext->from.sym->name:noname)
  11. #define cput(c)\
  12. { *cbp++ = c;\
  13. if(--cbc <= 0)\
  14. cflush(); }
  15. typedef struct Adr Adr;
  16. typedef struct Prog Prog;
  17. typedef struct Sym Sym;
  18. typedef struct Auto Auto;
  19. typedef struct Optab Optab;
  20. typedef struct Movtab Movtab;
  21. struct Adr
  22. {
  23. union
  24. {
  25. vlong u0offset;
  26. char u0scon[8];
  27. Prog *u0cond; /* not used, but should be D_BRANCH */
  28. Ieee u0ieee;
  29. } u0;
  30. union
  31. {
  32. Auto* u1autom;
  33. Sym* u1sym;
  34. } u1;
  35. short type;
  36. char index;
  37. char scale;
  38. };
  39. #define offset u0.u0offset
  40. #define scon u0.u0scon
  41. #define cond u0.u0cond
  42. #define ieee u0.u0ieee
  43. #define autom u1.u1autom
  44. #define sym u1.u1sym
  45. struct Prog
  46. {
  47. Adr from;
  48. Adr to;
  49. Prog *forwd;
  50. Prog* link;
  51. Prog* pcond; /* work on this */
  52. vlong pc;
  53. long line;
  54. uchar mark; /* work on these */
  55. uchar back;
  56. short as;
  57. char width; /* fake for DATA */
  58. char mode; /* 16, 32, or 64 */
  59. };
  60. struct Auto
  61. {
  62. Sym* asym;
  63. Auto* link;
  64. long aoffset;
  65. short type;
  66. };
  67. struct Sym
  68. {
  69. char *name;
  70. short type;
  71. short version;
  72. short become;
  73. short frame;
  74. uchar subtype;
  75. ushort file;
  76. vlong value;
  77. long sig;
  78. Sym* link;
  79. };
  80. struct Optab
  81. {
  82. short as;
  83. uchar* ytab;
  84. uchar prefix;
  85. uchar op[20];
  86. };
  87. struct Movtab
  88. {
  89. short as;
  90. uchar ft;
  91. uchar tt;
  92. uchar code;
  93. uchar op[4];
  94. };
  95. enum
  96. {
  97. STEXT = 1,
  98. SDATA,
  99. SBSS,
  100. SDATA1,
  101. SXREF,
  102. SFILE,
  103. SCONST,
  104. SUNDEF,
  105. SIMPORT,
  106. SEXPORT,
  107. NHASH = 10007,
  108. NHUNK = 100000,
  109. MINSIZ = 8,
  110. STRINGSZ = 200,
  111. MINLC = 1,
  112. MAXIO = 8192,
  113. MAXHIST = 20, /* limit of path elements for history symbols */
  114. Yxxx = 0,
  115. Ynone,
  116. Yi0,
  117. Yi1,
  118. Yi8,
  119. Ys32,
  120. Yi32,
  121. Yi64,
  122. Yiauto,
  123. Yal,
  124. Ycl,
  125. Yax,
  126. Ycx,
  127. Yrb,
  128. Yrl,
  129. Yrf,
  130. Yf0,
  131. Yrx,
  132. Ymb,
  133. Yml,
  134. Ym,
  135. Ybr,
  136. Ycol,
  137. Ycs, Yss, Yds, Yes, Yfs, Ygs,
  138. Ygdtr, Yidtr, Yldtr, Ymsw, Ytask,
  139. Ycr0, Ycr1, Ycr2, Ycr3, Ycr4, Ycr5, Ycr6, Ycr7, Ycr8,
  140. Ydr0, Ydr1, Ydr2, Ydr3, Ydr4, Ydr5, Ydr6, Ydr7,
  141. Ytr0, Ytr1, Ytr2, Ytr3, Ytr4, Ytr5, Ytr6, Ytr7, Yrl32, Yrl64,
  142. Ymr, Ymm,
  143. Yxr, Yxm,
  144. Ymax,
  145. Zxxx = 0,
  146. Zlit,
  147. Z_rp,
  148. Zbr,
  149. Zcall,
  150. Zib_,
  151. Zib_rp,
  152. Zibo_m,
  153. Zibo_m_xm,
  154. Zil_,
  155. Zil_rp,
  156. Ziq_rp,
  157. Zilo_m,
  158. Ziqo_m,
  159. Zjmp,
  160. Zloop,
  161. Zo_iw,
  162. Zm_o,
  163. Zm_r,
  164. Zm_r_xm,
  165. Zm_r_i_xm,
  166. Zm_r_3d,
  167. Zm_r_xm_nr,
  168. Zr_m_xm_nr,
  169. Zibm_r, /* mmx1,mmx2/mem64,imm8 */
  170. Zmb_r,
  171. Zaut_r,
  172. Zo_m,
  173. Zo_m64,
  174. Zpseudo,
  175. Zr_m,
  176. Zr_m_xm,
  177. Zr_m_i_xm,
  178. Zrp_,
  179. Z_ib,
  180. Z_il,
  181. Zm_ibo,
  182. Zm_ilo,
  183. Zib_rr,
  184. Zil_rr,
  185. Zclr,
  186. Zbyte,
  187. Zmax,
  188. Px = 0,
  189. P32 = 0x32, /* 32-bit only */
  190. Pe = 0x66, /* operand escape */
  191. Pm = 0x0f, /* 2byte opcode escape */
  192. Pq = 0xff, /* both escape */
  193. Pb = 0xfe, /* byte operands */
  194. Pf2 = 0xf2, /* xmm escape 1 */
  195. Pf3 = 0xf3, /* xmm escape 2 */
  196. Pw = 0x48, /* Rex.w */
  197. Py = 0x80, /* defaults to 64-bit mode */
  198. Rxf = 1<<9, /* internal flag for Rxr on from */
  199. Rxt = 1<<8, /* internal flag for Rxr on to */
  200. Rxw = 1<<3, /* =1, 64-bit operand size */
  201. Rxr = 1<<2, /* extend modrm reg */
  202. Rxx = 1<<1, /* extend sib index */
  203. Rxb = 1<<0, /* extend modrm r/m, sib base, or opcode reg */
  204. Roffset = 22, /* no. bits for offset in relocation address */
  205. Rindex = 10, /* no. bits for index in relocation address */
  206. };
  207. EXTERN union
  208. {
  209. struct
  210. {
  211. char obuf[MAXIO]; /* output buffer */
  212. uchar ibuf[MAXIO]; /* input buffer */
  213. } u;
  214. char dbuf[1];
  215. } buf;
  216. #define cbuf u.obuf
  217. #define xbuf u.ibuf
  218. #pragma varargck type "A" int
  219. #pragma varargck type "A" uint
  220. #pragma varargck type "D" Adr*
  221. #pragma varargck type "P" Prog*
  222. #pragma varargck type "R" int
  223. #pragma varargck type "S" char*
  224. #pragma varargck argpos diag 1
  225. EXTERN long HEADR;
  226. EXTERN long HEADTYPE;
  227. EXTERN vlong INITDAT;
  228. EXTERN long INITRND;
  229. EXTERN vlong INITTEXT;
  230. EXTERN char* INITENTRY; /* entry point */
  231. EXTERN Biobuf bso;
  232. EXTERN long bsssize;
  233. EXTERN int cbc;
  234. EXTERN char* cbp;
  235. EXTERN char* pcstr;
  236. EXTERN int cout;
  237. EXTERN Auto* curauto;
  238. EXTERN Auto* curhist;
  239. EXTERN Prog* curp;
  240. EXTERN Prog* curtext;
  241. EXTERN Prog* datap;
  242. EXTERN Prog* edatap;
  243. EXTERN vlong datsize;
  244. EXTERN char debug[128];
  245. EXTERN char literal[128];
  246. EXTERN Prog* etextp;
  247. EXTERN Prog* firstp;
  248. EXTERN uchar fnuxi8[8];
  249. EXTERN uchar fnuxi4[4];
  250. EXTERN Sym* hash[NHASH];
  251. EXTERN Sym* histfrog[MAXHIST];
  252. EXTERN int histfrogp;
  253. EXTERN int histgen;
  254. EXTERN char* library[50];
  255. EXTERN char* libraryobj[50];
  256. EXTERN int libraryp;
  257. EXTERN int xrefresolv;
  258. EXTERN char* hunk;
  259. EXTERN uchar inuxi1[1];
  260. EXTERN uchar inuxi2[2];
  261. EXTERN uchar inuxi4[4];
  262. EXTERN uchar inuxi8[8];
  263. EXTERN char ycover[Ymax*Ymax];
  264. EXTERN uchar* andptr;
  265. EXTERN uchar* rexptr;
  266. EXTERN uchar and[30];
  267. EXTERN int reg[D_NONE];
  268. EXTERN int regrex[D_NONE+1];
  269. EXTERN Prog* lastp;
  270. EXTERN long lcsize;
  271. EXTERN int nerrors;
  272. EXTERN long nhunk;
  273. EXTERN long nsymbol;
  274. EXTERN char* noname;
  275. EXTERN char* outfile;
  276. EXTERN vlong pc;
  277. EXTERN long spsize;
  278. EXTERN Sym* symlist;
  279. EXTERN long symsize;
  280. EXTERN Prog* textp;
  281. EXTERN vlong textsize;
  282. EXTERN long thunk;
  283. EXTERN int version;
  284. EXTERN Prog zprg;
  285. EXTERN int dtype;
  286. EXTERN char* paramspace;
  287. EXTERN Adr* reloca;
  288. EXTERN int doexp, dlm;
  289. EXTERN int imports, nimports;
  290. EXTERN int exports, nexports;
  291. EXTERN char* EXPTAB;
  292. EXTERN Prog undefp;
  293. #define UP (&undefp)
  294. extern Optab optab[];
  295. extern Optab* opindex[];
  296. extern char* anames[];
  297. int Aconv(Fmt*);
  298. int Dconv(Fmt*);
  299. int Pconv(Fmt*);
  300. int Rconv(Fmt*);
  301. int Sconv(Fmt*);
  302. void addhist(long, int);
  303. Prog* appendp(Prog*);
  304. void asmb(void);
  305. void asmdyn(void);
  306. void asmins(Prog*);
  307. void asmlc(void);
  308. void asmsp(void);
  309. void asmsym(void);
  310. vlong atolwhex(char*);
  311. Prog* brchain(Prog*);
  312. Prog* brloop(Prog*);
  313. void buildop(void);
  314. void cflush(void);
  315. void ckoff(Sym*, long);
  316. Prog* copyp(Prog*);
  317. double cputime(void);
  318. void datblk(long, long);
  319. void diag(char*, ...);
  320. void dodata(void);
  321. void doinit(void);
  322. void doprof1(void);
  323. void doprof2(void);
  324. void dostkoff(void);
  325. void dynreloc(Sym*, ulong, int);
  326. vlong entryvalue(void);
  327. void errorexit(void);
  328. void export(void);
  329. int find1(long, int);
  330. int find2(long, int);
  331. void follow(void);
  332. void gethunk(void);
  333. void histtoauto(void);
  334. double ieeedtod(Ieee*);
  335. long ieeedtof(Ieee*);
  336. void import(void);
  337. void ldobj(int, long, char*);
  338. void loadlib(void);
  339. void listinit(void);
  340. Sym* lookup(char*, int);
  341. void lput(long);
  342. void lputl(long);
  343. void main(int, char*[]);
  344. void mkfwd(void);
  345. void* mysbrk(ulong);
  346. void nuxiinit(void);
  347. void objfile(char*);
  348. int opsize(Prog*);
  349. void patch(void);
  350. Prog* prg(void);
  351. void readundefs(char*, int);
  352. int relinv(int);
  353. long reuse(Prog*, Sym*);
  354. vlong rnd(vlong, vlong);
  355. void span(void);
  356. void undef(void);
  357. void undefsym(Sym*);
  358. vlong vaddr(Adr*);
  359. void wput(ushort);
  360. void xdefine(char*, int, vlong);
  361. void xfol(Prog*);
  362. int zaddr(uchar*, Adr*, Sym*[]);
  363. void zerosig(char*);
  364. void Lflag(char*);