span.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. #include "l.h"
  2. void
  3. span(void)
  4. {
  5. Prog *p, *q;
  6. long v, c, idat;
  7. int m, n, again;
  8. xdefine("etext", STEXT, 0L);
  9. idat = INITDAT;
  10. for(p = firstp; p != P; p = p->link) {
  11. if(p->as == ATEXT)
  12. curtext = p;
  13. n = 0;
  14. if(p->to.type == D_BRANCH)
  15. if(p->pcond == P)
  16. p->pcond = p;
  17. if((q = p->pcond) != P)
  18. if(q->back != 2)
  19. n = 1;
  20. p->back = n;
  21. if(p->as == AADJSP) {
  22. p->to.type = D_SP;
  23. v = -p->from.offset;
  24. p->from.offset = v;
  25. p->as = AADDL;
  26. if(v < 0) {
  27. p->as = ASUBL;
  28. v = -v;
  29. p->from.offset = v;
  30. }
  31. if(v == 0)
  32. p->as = ANOP;
  33. }
  34. }
  35. n = 0;
  36. start:
  37. if(debug['v'])
  38. Bprint(&bso, "%5.2f span\n", cputime());
  39. Bflush(&bso);
  40. c = INITTEXT;
  41. for(p = firstp; p != P; p = p->link) {
  42. if(p->as == ATEXT)
  43. curtext = p;
  44. if(p->to.type == D_BRANCH)
  45. if(p->back)
  46. p->pc = c;
  47. asmins(p);
  48. p->pc = c;
  49. m = andptr-and;
  50. p->mark = m;
  51. c += m;
  52. }
  53. loop:
  54. n++;
  55. if(debug['v'])
  56. Bprint(&bso, "%5.2f span %d\n", cputime(), n);
  57. Bflush(&bso);
  58. if(n > 50) {
  59. print("span must be looping\n");
  60. errorexit();
  61. }
  62. again = 0;
  63. c = INITTEXT;
  64. for(p = firstp; p != P; p = p->link) {
  65. if(p->as == ATEXT)
  66. curtext = p;
  67. if(p->to.type == D_BRANCH) {
  68. if(p->back)
  69. p->pc = c;
  70. asmins(p);
  71. m = andptr-and;
  72. if(m != p->mark) {
  73. p->mark = m;
  74. again++;
  75. }
  76. }
  77. p->pc = c;
  78. c += p->mark;
  79. }
  80. if(again) {
  81. textsize = c;
  82. goto loop;
  83. }
  84. if(INITRND) {
  85. INITDAT = rnd(c, INITRND);
  86. if(INITDAT != idat) {
  87. idat = INITDAT;
  88. goto start;
  89. }
  90. }
  91. xdefine("etext", STEXT, c);
  92. if(debug['v'])
  93. Bprint(&bso, "etext = %lux\n", c);
  94. Bflush(&bso);
  95. for(p = textp; p != P; p = p->pcond)
  96. p->from.sym->value = p->pc;
  97. textsize = c - INITTEXT;
  98. }
  99. void
  100. xdefine(char *p, int t, long v)
  101. {
  102. Sym *s;
  103. s = lookup(p, 0);
  104. if(s->type == 0 || s->type == SXREF) {
  105. s->type = t;
  106. s->value = v;
  107. }
  108. if(s->type == STEXT && s->value == 0)
  109. s->value = v;
  110. }
  111. void
  112. putsymb(char *s, int t, long v, int ver)
  113. {
  114. int i, f;
  115. if(t == 'f')
  116. s++;
  117. lput(v);
  118. if(ver)
  119. t += 'a' - 'A';
  120. cput(t+0x80); /* 0x80 is variable length */
  121. if(t == 'Z' || t == 'z') {
  122. cput(s[0]);
  123. for(i=1; s[i] != 0 || s[i+1] != 0; i += 2) {
  124. cput(s[i]);
  125. cput(s[i+1]);
  126. }
  127. cput(0);
  128. cput(0);
  129. i++;
  130. }
  131. else {
  132. for(i=0; s[i]; i++)
  133. cput(s[i]);
  134. cput(0);
  135. }
  136. symsize += 4 + 1 + i + 1;
  137. if(debug['n']) {
  138. if(t == 'z' || t == 'Z') {
  139. Bprint(&bso, "%c %.8lux ", t, v);
  140. for(i=1; s[i] != 0 || s[i+1] != 0; i+=2) {
  141. f = ((s[i]&0xff) << 8) | (s[i+1]&0xff);
  142. Bprint(&bso, "/%x", f);
  143. }
  144. Bprint(&bso, "\n");
  145. return;
  146. }
  147. if(ver)
  148. Bprint(&bso, "%c %.8lux %s<%d>\n", t, v, s, ver);
  149. else
  150. Bprint(&bso, "%c %.8lux %s\n", t, v, s);
  151. }
  152. }
  153. void
  154. asmsym(void)
  155. {
  156. Prog *p;
  157. Auto *a;
  158. Sym *s;
  159. int h;
  160. s = lookup("etext", 0);
  161. if(s->type == STEXT)
  162. putsymb(s->name, 'T', s->value, s->version);
  163. for(h=0; h<NHASH; h++)
  164. for(s=hash[h]; s!=S; s=s->link)
  165. switch(s->type) {
  166. case SCONST:
  167. putsymb(s->name, 'D', s->value, s->version);
  168. continue;
  169. case SDATA:
  170. putsymb(s->name, 'D', s->value+INITDAT, s->version);
  171. continue;
  172. case SBSS:
  173. putsymb(s->name, 'B', s->value+INITDAT, s->version);
  174. continue;
  175. case SFILE:
  176. putsymb(s->name, 'f', s->value, s->version);
  177. continue;
  178. }
  179. for(p=textp; p!=P; p=p->pcond) {
  180. s = p->from.sym;
  181. if(s->type != STEXT)
  182. continue;
  183. /* filenames first */
  184. for(a=p->to.autom; a; a=a->link)
  185. if(a->type == D_FILE)
  186. putsymb(a->asym->name, 'z', a->aoffset, 0);
  187. else
  188. if(a->type == D_FILE1)
  189. putsymb(a->asym->name, 'Z', a->aoffset, 0);
  190. putsymb(s->name, 'T', s->value, s->version);
  191. /* frame, auto and param after */
  192. putsymb(".frame", 'm', p->to.offset+4, 0);
  193. for(a=p->to.autom; a; a=a->link)
  194. if(a->type == D_AUTO)
  195. putsymb(a->asym->name, 'a', -a->aoffset, 0);
  196. else
  197. if(a->type == D_PARAM)
  198. putsymb(a->asym->name, 'p', a->aoffset, 0);
  199. }
  200. if(debug['v'] || debug['n'])
  201. Bprint(&bso, "symsize = %lud\n", symsize);
  202. Bflush(&bso);
  203. }
  204. void
  205. asmlc(void)
  206. {
  207. long oldpc, oldlc;
  208. Prog *p;
  209. long v, s;
  210. oldpc = INITTEXT;
  211. oldlc = 0;
  212. for(p = firstp; p != P; p = p->link) {
  213. if(p->line == oldlc || p->as == ATEXT || p->as == ANOP) {
  214. if(p->as == ATEXT)
  215. curtext = p;
  216. if(debug['L'])
  217. Bprint(&bso, "%6lux %P\n",
  218. p->pc, p);
  219. continue;
  220. }
  221. if(debug['L'])
  222. Bprint(&bso, "\t\t%6ld", lcsize);
  223. v = (p->pc - oldpc) / MINLC;
  224. while(v) {
  225. s = 127;
  226. if(v < 127)
  227. s = v;
  228. cput(s+128); /* 129-255 +pc */
  229. if(debug['L'])
  230. Bprint(&bso, " pc+%ld*%d(%ld)", s, MINLC, s+128);
  231. v -= s;
  232. lcsize++;
  233. }
  234. s = p->line - oldlc;
  235. oldlc = p->line;
  236. oldpc = p->pc + MINLC;
  237. if(s > 64 || s < -64) {
  238. cput(0); /* 0 vv +lc */
  239. cput(s>>24);
  240. cput(s>>16);
  241. cput(s>>8);
  242. cput(s);
  243. if(debug['L']) {
  244. if(s > 0)
  245. Bprint(&bso, " lc+%ld(%d,%ld)\n",
  246. s, 0, s);
  247. else
  248. Bprint(&bso, " lc%ld(%d,%ld)\n",
  249. s, 0, s);
  250. Bprint(&bso, "%6lux %P\n",
  251. p->pc, p);
  252. }
  253. lcsize += 5;
  254. continue;
  255. }
  256. if(s > 0) {
  257. cput(0+s); /* 1-64 +lc */
  258. if(debug['L']) {
  259. Bprint(&bso, " lc+%ld(%ld)\n", s, 0+s);
  260. Bprint(&bso, "%6lux %P\n",
  261. p->pc, p);
  262. }
  263. } else {
  264. cput(64-s); /* 65-128 -lc */
  265. if(debug['L']) {
  266. Bprint(&bso, " lc%ld(%ld)\n", s, 64-s);
  267. Bprint(&bso, "%6lux %P\n",
  268. p->pc, p);
  269. }
  270. }
  271. lcsize++;
  272. }
  273. while(lcsize & 1) {
  274. s = 129;
  275. cput(s);
  276. lcsize++;
  277. }
  278. if(debug['v'] || debug['L'])
  279. Bprint(&bso, "lcsize = %ld\n", lcsize);
  280. Bflush(&bso);
  281. }
  282. int
  283. prefixof(Adr *a)
  284. {
  285. switch(a->type) {
  286. case D_INDIR+D_CS:
  287. return 0x2e;
  288. case D_INDIR+D_DS:
  289. return 0x3e;
  290. case D_INDIR+D_ES:
  291. return 0x26;
  292. case D_INDIR+D_FS:
  293. return 0x64;
  294. case D_INDIR+D_GS:
  295. return 0x65;
  296. }
  297. return 0;
  298. }
  299. int
  300. oclass(Adr *a)
  301. {
  302. long v;
  303. if(a->type >= D_INDIR || a->index != D_NONE) {
  304. if(a->index != D_NONE && a->scale == 0) {
  305. if(a->type == D_ADDR) {
  306. switch(a->index) {
  307. case D_EXTERN:
  308. case D_STATIC:
  309. return Yi32;
  310. case D_AUTO:
  311. case D_PARAM:
  312. return Yiauto;
  313. }
  314. return Yxxx;
  315. }
  316. return Ycol;
  317. }
  318. return Ym;
  319. }
  320. switch(a->type)
  321. {
  322. case D_AL:
  323. return Yal;
  324. case D_AX:
  325. return Yax;
  326. case D_CL:
  327. return Ycl;
  328. case D_DL:
  329. case D_BL:
  330. case D_AH:
  331. case D_CH:
  332. case D_DH:
  333. case D_BH:
  334. return Yrb;
  335. case D_CX:
  336. return Ycx;
  337. case D_DX:
  338. case D_BX:
  339. return Yrx;
  340. case D_SP:
  341. case D_BP:
  342. case D_SI:
  343. case D_DI:
  344. return Yrl;
  345. case D_F0+0:
  346. return Yf0;
  347. case D_F0+1:
  348. case D_F0+2:
  349. case D_F0+3:
  350. case D_F0+4:
  351. case D_F0+5:
  352. case D_F0+6:
  353. case D_F0+7:
  354. return Yrf;
  355. case D_NONE:
  356. return Ynone;
  357. case D_CS: return Ycs;
  358. case D_SS: return Yss;
  359. case D_DS: return Yds;
  360. case D_ES: return Yes;
  361. case D_FS: return Yfs;
  362. case D_GS: return Ygs;
  363. case D_GDTR: return Ygdtr;
  364. case D_IDTR: return Yidtr;
  365. case D_LDTR: return Yldtr;
  366. case D_MSW: return Ymsw;
  367. case D_TASK: return Ytask;
  368. case D_CR+0: return Ycr0;
  369. case D_CR+1: return Ycr1;
  370. case D_CR+2: return Ycr2;
  371. case D_CR+3: return Ycr3;
  372. case D_CR+4: return Ycr4;
  373. case D_CR+5: return Ycr5;
  374. case D_CR+6: return Ycr6;
  375. case D_CR+7: return Ycr7;
  376. case D_DR+0: return Ydr0;
  377. case D_DR+1: return Ydr1;
  378. case D_DR+2: return Ydr2;
  379. case D_DR+3: return Ydr3;
  380. case D_DR+4: return Ydr4;
  381. case D_DR+5: return Ydr5;
  382. case D_DR+6: return Ydr6;
  383. case D_DR+7: return Ydr7;
  384. case D_TR+0: return Ytr0;
  385. case D_TR+1: return Ytr1;
  386. case D_TR+2: return Ytr2;
  387. case D_TR+3: return Ytr3;
  388. case D_TR+4: return Ytr4;
  389. case D_TR+5: return Ytr5;
  390. case D_TR+6: return Ytr6;
  391. case D_TR+7: return Ytr7;
  392. case D_EXTERN:
  393. case D_STATIC:
  394. case D_AUTO:
  395. case D_PARAM:
  396. return Ym;
  397. case D_CONST:
  398. case D_ADDR:
  399. if(a->sym == S) {
  400. v = a->offset;
  401. if(v == 0)
  402. return Yi0;
  403. if(v == 1)
  404. return Yi1;
  405. if(v >= -128 && v <= 127)
  406. return Yi8;
  407. }
  408. return Yi32;
  409. case D_BRANCH:
  410. return Ybr;
  411. }
  412. return Yxxx;
  413. }
  414. void
  415. asmidx(Adr *a, int base)
  416. {
  417. int i;
  418. switch(a->index) {
  419. default:
  420. goto bad;
  421. case D_NONE:
  422. i = 4 << 3;
  423. goto bas;
  424. case D_AX:
  425. case D_CX:
  426. case D_DX:
  427. case D_BX:
  428. case D_BP:
  429. case D_SI:
  430. case D_DI:
  431. i = reg[a->index] << 3;
  432. break;
  433. }
  434. switch(a->scale) {
  435. default:
  436. goto bad;
  437. case 1:
  438. break;
  439. case 2:
  440. i |= (1<<6);
  441. break;
  442. case 4:
  443. i |= (2<<6);
  444. break;
  445. case 8:
  446. i |= (3<<6);
  447. break;
  448. }
  449. bas:
  450. switch(base) {
  451. default:
  452. goto bad;
  453. case D_NONE: /* must be mod=00 */
  454. i |= 5;
  455. break;
  456. case D_AX:
  457. case D_CX:
  458. case D_DX:
  459. case D_BX:
  460. case D_SP:
  461. case D_BP:
  462. case D_SI:
  463. case D_DI:
  464. i |= reg[base];
  465. break;
  466. }
  467. *andptr++ = i;
  468. return;
  469. bad:
  470. diag("asmidx: bad address %D", a);
  471. *andptr++ = 0;
  472. return;
  473. }
  474. static void
  475. put4(long v)
  476. {
  477. if(dlm && curp != P && reloca != nil){
  478. dynreloc(reloca->sym, curp->pc + andptr - &and[0], 1);
  479. reloca = nil;
  480. }
  481. andptr[0] = v;
  482. andptr[1] = v>>8;
  483. andptr[2] = v>>16;
  484. andptr[3] = v>>24;
  485. andptr += 4;
  486. }
  487. long
  488. vaddr(Adr *a)
  489. {
  490. int t;
  491. long v;
  492. Sym *s;
  493. t = a->type;
  494. v = a->offset;
  495. if(t == D_ADDR)
  496. t = a->index;
  497. switch(t) {
  498. case D_STATIC:
  499. case D_EXTERN:
  500. s = a->sym;
  501. if(s != nil) {
  502. if(dlm && curp != P)
  503. reloca = a;
  504. switch(s->type) {
  505. case SUNDEF:
  506. ckoff(s, v);
  507. case STEXT:
  508. case SCONST:
  509. v += s->value;
  510. break;
  511. default:
  512. v += INITDAT + s->value;
  513. }
  514. }
  515. }
  516. return v;
  517. }
  518. void
  519. asmand(Adr *a, int r)
  520. {
  521. long v;
  522. int t;
  523. Adr aa;
  524. v = a->offset;
  525. t = a->type;
  526. if(a->index != D_NONE) {
  527. if(t >= D_INDIR) {
  528. t -= D_INDIR;
  529. if(t == D_NONE) {
  530. *andptr++ = (0 << 6) | (4 << 0) | (r << 3);
  531. asmidx(a, t);
  532. put4(v);
  533. return;
  534. }
  535. if(v == 0 && t != D_BP) {
  536. *andptr++ = (0 << 6) | (4 << 0) | (r << 3);
  537. asmidx(a, t);
  538. return;
  539. }
  540. if(v >= -128 && v < 128) {
  541. *andptr++ = (1 << 6) | (4 << 0) | (r << 3);
  542. asmidx(a, t);
  543. *andptr++ = v;
  544. return;
  545. }
  546. *andptr++ = (2 << 6) | (4 << 0) | (r << 3);
  547. asmidx(a, t);
  548. put4(v);
  549. return;
  550. }
  551. switch(t) {
  552. default:
  553. goto bad;
  554. case D_STATIC:
  555. case D_EXTERN:
  556. aa.type = D_NONE+D_INDIR;
  557. break;
  558. case D_AUTO:
  559. case D_PARAM:
  560. aa.type = D_SP+D_INDIR;
  561. break;
  562. }
  563. aa.offset = vaddr(a);
  564. aa.index = a->index;
  565. aa.scale = a->scale;
  566. asmand(&aa, r);
  567. return;
  568. }
  569. if(t >= D_AL && t <= D_F0+7) {
  570. if(v)
  571. goto bad;
  572. *andptr++ = (3 << 6) | (reg[t] << 0) | (r << 3);
  573. return;
  574. }
  575. if(t >= D_INDIR) {
  576. t -= D_INDIR;
  577. if(t == D_NONE || D_CS <= t && t <= D_GS) {
  578. *andptr++ = (0 << 6) | (5 << 0) | (r << 3);
  579. put4(v);
  580. return;
  581. }
  582. if(t == D_SP) {
  583. if(v == 0) {
  584. *andptr++ = (0 << 6) | (4 << 0) | (r << 3);
  585. asmidx(a, D_SP);
  586. return;
  587. }
  588. if(v >= -128 && v < 128) {
  589. *andptr++ = (1 << 6) | (4 << 0) | (r << 3);
  590. asmidx(a, D_SP);
  591. *andptr++ = v;
  592. return;
  593. }
  594. *andptr++ = (2 << 6) | (4 << 0) | (r << 3);
  595. asmidx(a, D_SP);
  596. put4(v);
  597. return;
  598. }
  599. if(t >= D_AX && t <= D_DI) {
  600. if(v == 0 && t != D_BP) {
  601. *andptr++ = (0 << 6) | (reg[t] << 0) | (r << 3);
  602. return;
  603. }
  604. if(v >= -128 && v < 128) {
  605. andptr[0] = (1 << 6) | (reg[t] << 0) | (r << 3);
  606. andptr[1] = v;
  607. andptr += 2;
  608. return;
  609. }
  610. *andptr++ = (2 << 6) | (reg[t] << 0) | (r << 3);
  611. put4(v);
  612. return;
  613. }
  614. goto bad;
  615. }
  616. switch(a->type) {
  617. default:
  618. goto bad;
  619. case D_STATIC:
  620. case D_EXTERN:
  621. aa.type = D_NONE+D_INDIR;
  622. break;
  623. case D_AUTO:
  624. case D_PARAM:
  625. aa.type = D_SP+D_INDIR;
  626. break;
  627. }
  628. aa.index = D_NONE;
  629. aa.scale = 1;
  630. aa.offset = vaddr(a);
  631. asmand(&aa, r);
  632. return;
  633. bad:
  634. diag("asmand: bad address %D", a);
  635. return;
  636. }
  637. #define E 0xff
  638. uchar ymovtab[] =
  639. {
  640. /* push */
  641. APUSHL, Ycs, Ynone, 0, 0x0e,E,0,0,
  642. APUSHL, Yss, Ynone, 0, 0x16,E,0,0,
  643. APUSHL, Yds, Ynone, 0, 0x1e,E,0,0,
  644. APUSHL, Yes, Ynone, 0, 0x06,E,0,0,
  645. APUSHL, Yfs, Ynone, 0, 0x0f,0xa0,E,0,
  646. APUSHL, Ygs, Ynone, 0, 0x0f,0xa8,E,0,
  647. APUSHW, Ycs, Ynone, 0, Pe,0x0e,E,0,
  648. APUSHW, Yss, Ynone, 0, Pe,0x16,E,0,
  649. APUSHW, Yds, Ynone, 0, Pe,0x1e,E,0,
  650. APUSHW, Yes, Ynone, 0, Pe,0x06,E,0,
  651. APUSHW, Yfs, Ynone, 0, Pe,0x0f,0xa0,E,
  652. APUSHW, Ygs, Ynone, 0, Pe,0x0f,0xa8,E,
  653. /* pop */
  654. APOPL, Ynone, Yds, 0, 0x1f,E,0,0,
  655. APOPL, Ynone, Yes, 0, 0x07,E,0,0,
  656. APOPL, Ynone, Yss, 0, 0x17,E,0,0,
  657. APOPL, Ynone, Yfs, 0, 0x0f,0xa1,E,0,
  658. APOPL, Ynone, Ygs, 0, 0x0f,0xa9,E,0,
  659. APOPW, Ynone, Yds, 0, Pe,0x1f,E,0,
  660. APOPW, Ynone, Yes, 0, Pe,0x07,E,0,
  661. APOPW, Ynone, Yss, 0, Pe,0x17,E,0,
  662. APOPW, Ynone, Yfs, 0, Pe,0x0f,0xa1,E,
  663. APOPW, Ynone, Ygs, 0, Pe,0x0f,0xa9,E,
  664. /* mov seg */
  665. AMOVW, Yes, Yml, 1, 0x8c,0,0,0,
  666. AMOVW, Ycs, Yml, 1, 0x8c,1,0,0,
  667. AMOVW, Yss, Yml, 1, 0x8c,2,0,0,
  668. AMOVW, Yds, Yml, 1, 0x8c,3,0,0,
  669. AMOVW, Yfs, Yml, 1, 0x8c,4,0,0,
  670. AMOVW, Ygs, Yml, 1, 0x8c,5,0,0,
  671. AMOVW, Yml, Yes, 2, 0x8e,0,0,0,
  672. AMOVW, Yml, Ycs, 2, 0x8e,1,0,0,
  673. AMOVW, Yml, Yss, 2, 0x8e,2,0,0,
  674. AMOVW, Yml, Yds, 2, 0x8e,3,0,0,
  675. AMOVW, Yml, Yfs, 2, 0x8e,4,0,0,
  676. AMOVW, Yml, Ygs, 2, 0x8e,5,0,0,
  677. /* mov cr */
  678. AMOVL, Ycr0, Yml, 3, 0x0f,0x20,0,0,
  679. AMOVL, Ycr2, Yml, 3, 0x0f,0x20,2,0,
  680. AMOVL, Ycr3, Yml, 3, 0x0f,0x20,3,0,
  681. AMOVL, Ycr4, Yml, 3, 0x0f,0x20,4,0,
  682. AMOVL, Yml, Ycr0, 4, 0x0f,0x22,0,0,
  683. AMOVL, Yml, Ycr2, 4, 0x0f,0x22,2,0,
  684. AMOVL, Yml, Ycr3, 4, 0x0f,0x22,3,0,
  685. AMOVL, Yml, Ycr4, 4, 0x0f,0x22,4,0,
  686. /* mov dr */
  687. AMOVL, Ydr0, Yml, 3, 0x0f,0x21,0,0,
  688. AMOVL, Ydr6, Yml, 3, 0x0f,0x21,6,0,
  689. AMOVL, Ydr7, Yml, 3, 0x0f,0x21,7,0,
  690. AMOVL, Yml, Ydr0, 4, 0x0f,0x23,0,0,
  691. AMOVL, Yml, Ydr6, 4, 0x0f,0x23,6,0,
  692. AMOVL, Yml, Ydr7, 4, 0x0f,0x23,7,0,
  693. /* mov tr */
  694. AMOVL, Ytr6, Yml, 3, 0x0f,0x24,6,0,
  695. AMOVL, Ytr7, Yml, 3, 0x0f,0x24,7,0,
  696. AMOVL, Yml, Ytr6, 4, 0x0f,0x26,6,E,
  697. AMOVL, Yml, Ytr7, 4, 0x0f,0x26,7,E,
  698. /* lgdt, sgdt, lidt, sidt */
  699. AMOVL, Ym, Ygdtr, 4, 0x0f,0x01,2,0,
  700. AMOVL, Ygdtr, Ym, 3, 0x0f,0x01,0,0,
  701. AMOVL, Ym, Yidtr, 4, 0x0f,0x01,3,0,
  702. AMOVL, Yidtr, Ym, 3, 0x0f,0x01,1,0,
  703. /* lldt, sldt */
  704. AMOVW, Yml, Yldtr, 4, 0x0f,0x00,2,0,
  705. AMOVW, Yldtr, Yml, 3, 0x0f,0x00,0,0,
  706. /* lmsw, smsw */
  707. AMOVW, Yml, Ymsw, 4, 0x0f,0x01,6,0,
  708. AMOVW, Ymsw, Yml, 3, 0x0f,0x01,4,0,
  709. /* ltr, str */
  710. AMOVW, Yml, Ytask, 4, 0x0f,0x00,3,0,
  711. AMOVW, Ytask, Yml, 3, 0x0f,0x00,1,0,
  712. /* load full pointer */
  713. AMOVL, Yml, Ycol, 5, 0,0,0,0,
  714. AMOVW, Yml, Ycol, 5, Pe,0,0,0,
  715. /* double shift */
  716. ASHLL, Ycol, Yml, 6, 0xa4,0xa5,0,0,
  717. ASHRL, Ycol, Yml, 6, 0xac,0xad,0,0,
  718. /* extra imul */
  719. AIMULW, Yml, Yrl, 7, Pq,0xaf,0,0,
  720. AIMULL, Yml, Yrl, 7, Pm,0xaf,0,0,
  721. 0
  722. };
  723. int
  724. isax(Adr *a)
  725. {
  726. switch(a->type) {
  727. case D_AX:
  728. case D_AL:
  729. case D_AH:
  730. case D_INDIR+D_AX:
  731. return 1;
  732. }
  733. if(a->index == D_AX)
  734. return 1;
  735. return 0;
  736. }
  737. void
  738. subreg(Prog *p, int from, int to)
  739. {
  740. if(debug['Q'])
  741. print("\n%P s/%R/%R/\n", p, from, to);
  742. if(p->from.type == from)
  743. p->from.type = to;
  744. if(p->to.type == from)
  745. p->to.type = to;
  746. if(p->from.index == from)
  747. p->from.index = to;
  748. if(p->to.index == from)
  749. p->to.index = to;
  750. from += D_INDIR;
  751. if(p->from.type == from)
  752. p->from.type = to+D_INDIR;
  753. if(p->to.type == from)
  754. p->to.type = to+D_INDIR;
  755. if(debug['Q'])
  756. print("%P\n", p);
  757. }
  758. void
  759. doasm(Prog *p)
  760. {
  761. Optab *o;
  762. Prog *q, pp;
  763. uchar *t;
  764. int z, op, ft, tt;
  765. long v, pre;
  766. pre = prefixof(&p->from);
  767. if(pre)
  768. *andptr++ = pre;
  769. pre = prefixof(&p->to);
  770. if(pre)
  771. *andptr++ = pre;
  772. o = &optab[p->as];
  773. ft = oclass(&p->from) * Ymax;
  774. tt = oclass(&p->to) * Ymax;
  775. t = o->ytab;
  776. if(t == 0) {
  777. diag("asmins: noproto %P", p);
  778. return;
  779. }
  780. for(z=0; *t; z+=t[3],t+=4)
  781. if(ycover[ft+t[0]])
  782. if(ycover[tt+t[1]])
  783. goto found;
  784. goto domov;
  785. found:
  786. switch(o->prefix) {
  787. case Pq: /* 16 bit escape and opcode escape */
  788. *andptr++ = Pe;
  789. *andptr++ = Pm;
  790. break;
  791. case Pm: /* opcode escape */
  792. *andptr++ = Pm;
  793. break;
  794. case Pe: /* 16 bit escape */
  795. *andptr++ = Pe;
  796. break;
  797. case Pb: /* botch */
  798. break;
  799. }
  800. v = vaddr(&p->from);
  801. op = o->op[z];
  802. switch(t[2]) {
  803. default:
  804. diag("asmins: unknown z %d %P", t[2], p);
  805. return;
  806. case Zpseudo:
  807. break;
  808. case Zlit:
  809. for(; op = o->op[z]; z++)
  810. *andptr++ = op;
  811. break;
  812. case Zm_r:
  813. *andptr++ = op;
  814. asmand(&p->from, reg[p->to.type]);
  815. break;
  816. case Zaut_r:
  817. *andptr++ = 0x8d; /* leal */
  818. if(p->from.type != D_ADDR)
  819. diag("asmins: Zaut sb type ADDR");
  820. p->from.type = p->from.index;
  821. p->from.index = D_NONE;
  822. asmand(&p->from, reg[p->to.type]);
  823. p->from.index = p->from.type;
  824. p->from.type = D_ADDR;
  825. break;
  826. case Zm_o:
  827. *andptr++ = op;
  828. asmand(&p->from, o->op[z+1]);
  829. break;
  830. case Zr_m:
  831. *andptr++ = op;
  832. asmand(&p->to, reg[p->from.type]);
  833. break;
  834. case Zo_m:
  835. *andptr++ = op;
  836. asmand(&p->to, o->op[z+1]);
  837. break;
  838. case Zm_ibo:
  839. v = vaddr(&p->to);
  840. *andptr++ = op;
  841. asmand(&p->from, o->op[z+1]);
  842. *andptr++ = v;
  843. break;
  844. case Zibo_m:
  845. *andptr++ = op;
  846. asmand(&p->to, o->op[z+1]);
  847. *andptr++ = v;
  848. break;
  849. case Z_ib:
  850. v = vaddr(&p->to);
  851. case Zib_:
  852. *andptr++ = op;
  853. *andptr++ = v;
  854. break;
  855. case Zib_rp:
  856. *andptr++ = op + reg[p->to.type];
  857. *andptr++ = v;
  858. break;
  859. case Zil_rp:
  860. *andptr++ = op + reg[p->to.type];
  861. if(o->prefix == Pe) {
  862. *andptr++ = v;
  863. *andptr++ = v>>8;
  864. }
  865. else
  866. put4(v);
  867. break;
  868. case Zib_rr:
  869. *andptr++ = op;
  870. asmand(&p->to, reg[p->to.type]);
  871. *andptr++ = v;
  872. break;
  873. case Z_il:
  874. v = vaddr(&p->to);
  875. case Zil_:
  876. *andptr++ = op;
  877. if(o->prefix == Pe) {
  878. *andptr++ = v;
  879. *andptr++ = v>>8;
  880. }
  881. else
  882. put4(v);
  883. break;
  884. case Zm_ilo:
  885. v = vaddr(&p->to);
  886. *andptr++ = op;
  887. asmand(&p->from, o->op[z+1]);
  888. if(o->prefix == Pe) {
  889. *andptr++ = v;
  890. *andptr++ = v>>8;
  891. }
  892. else
  893. put4(v);
  894. break;
  895. case Zilo_m:
  896. *andptr++ = op;
  897. asmand(&p->to, o->op[z+1]);
  898. if(o->prefix == Pe) {
  899. *andptr++ = v;
  900. *andptr++ = v>>8;
  901. }
  902. else
  903. put4(v);
  904. break;
  905. case Zil_rr:
  906. *andptr++ = op;
  907. asmand(&p->to, reg[p->to.type]);
  908. if(o->prefix == Pe) {
  909. *andptr++ = v;
  910. *andptr++ = v>>8;
  911. }
  912. else
  913. put4(v);
  914. break;
  915. case Z_rp:
  916. *andptr++ = op + reg[p->to.type];
  917. break;
  918. case Zrp_:
  919. *andptr++ = op + reg[p->from.type];
  920. break;
  921. case Zclr:
  922. *andptr++ = op;
  923. asmand(&p->to, reg[p->to.type]);
  924. break;
  925. case Zbr:
  926. q = p->pcond;
  927. if(q) {
  928. v = q->pc - p->pc - 2;
  929. if(v >= -128 && v <= 127) {
  930. *andptr++ = op;
  931. *andptr++ = v;
  932. } else {
  933. v -= 6-2;
  934. *andptr++ = 0x0f;
  935. *andptr++ = o->op[z+1];
  936. *andptr++ = v;
  937. *andptr++ = v>>8;
  938. *andptr++ = v>>16;
  939. *andptr++ = v>>24;
  940. }
  941. }
  942. break;
  943. case Zcall:
  944. q = p->pcond;
  945. if(q) {
  946. v = q->pc - p->pc - 5;
  947. if(dlm && curp != P && p->to.sym->type == SUNDEF){
  948. /* v = 0 - p->pc - 5; */
  949. v = 0;
  950. ckoff(p->to.sym, v);
  951. v += p->to.sym->value;
  952. dynreloc(p->to.sym, p->pc+1, 0);
  953. }
  954. *andptr++ = op;
  955. *andptr++ = v;
  956. *andptr++ = v>>8;
  957. *andptr++ = v>>16;
  958. *andptr++ = v>>24;
  959. }
  960. break;
  961. case Zjmp:
  962. q = p->pcond;
  963. if(q) {
  964. v = q->pc - p->pc - 2;
  965. if(v >= -128 && v <= 127) {
  966. *andptr++ = op;
  967. *andptr++ = v;
  968. } else {
  969. v -= 5-2;
  970. *andptr++ = o->op[z+1];
  971. *andptr++ = v;
  972. *andptr++ = v>>8;
  973. *andptr++ = v>>16;
  974. *andptr++ = v>>24;
  975. }
  976. }
  977. break;
  978. case Zloop:
  979. q = p->pcond;
  980. if(q) {
  981. v = q->pc - p->pc - 2;
  982. if(v < -128 && v > 127)
  983. diag("loop too far: %P", p);
  984. *andptr++ = op;
  985. *andptr++ = v;
  986. }
  987. break;
  988. case Zbyte:
  989. *andptr++ = v;
  990. if(op > 1) {
  991. *andptr++ = v>>8;
  992. if(op > 2) {
  993. *andptr++ = v>>16;
  994. *andptr++ = v>>24;
  995. }
  996. }
  997. break;
  998. case Zmov:
  999. goto domov;
  1000. }
  1001. return;
  1002. domov:
  1003. for(t=ymovtab; *t; t+=8)
  1004. if(p->as == t[0])
  1005. if(ycover[ft+t[1]])
  1006. if(ycover[tt+t[2]])
  1007. goto mfound;
  1008. bad:
  1009. /*
  1010. * here, the assembly has failed.
  1011. * if its a byte instruction that has
  1012. * unaddressable registers, try to
  1013. * exchange registers and reissue the
  1014. * instruction with the operands renamed.
  1015. */
  1016. pp = *p;
  1017. z = p->from.type;
  1018. if(z >= D_BP && z <= D_DI) {
  1019. if(isax(&p->to)) {
  1020. *andptr++ = 0x87; /* xchg lhs,bx */
  1021. asmand(&p->from, reg[D_BX]);
  1022. subreg(&pp, z, D_BX);
  1023. doasm(&pp);
  1024. *andptr++ = 0x87; /* xchg lhs,bx */
  1025. asmand(&p->from, reg[D_BX]);
  1026. } else {
  1027. *andptr++ = 0x90 + reg[z]; /* xchg lsh,ax */
  1028. subreg(&pp, z, D_AX);
  1029. doasm(&pp);
  1030. *andptr++ = 0x90 + reg[z]; /* xchg lsh,ax */
  1031. }
  1032. return;
  1033. }
  1034. z = p->to.type;
  1035. if(z >= D_BP && z <= D_DI) {
  1036. if(isax(&p->from)) {
  1037. *andptr++ = 0x87; /* xchg rhs,bx */
  1038. asmand(&p->to, reg[D_BX]);
  1039. subreg(&pp, z, D_BX);
  1040. doasm(&pp);
  1041. *andptr++ = 0x87; /* xchg rhs,bx */
  1042. asmand(&p->to, reg[D_BX]);
  1043. } else {
  1044. *andptr++ = 0x90 + reg[z]; /* xchg rsh,ax */
  1045. subreg(&pp, z, D_AX);
  1046. doasm(&pp);
  1047. *andptr++ = 0x90 + reg[z]; /* xchg rsh,ax */
  1048. }
  1049. return;
  1050. }
  1051. diag("doasm: notfound t2=%ux from=%ux to=%ux %P", t[2], p->from.type, p->to.type, p);
  1052. return;
  1053. mfound:
  1054. switch(t[3]) {
  1055. default:
  1056. diag("asmins: unknown mov %d %P", t[3], p);
  1057. break;
  1058. case 0: /* lit */
  1059. for(z=4; t[z]!=E; z++)
  1060. *andptr++ = t[z];
  1061. break;
  1062. case 1: /* r,m */
  1063. *andptr++ = t[4];
  1064. asmand(&p->to, t[5]);
  1065. break;
  1066. case 2: /* m,r */
  1067. *andptr++ = t[4];
  1068. asmand(&p->from, t[5]);
  1069. break;
  1070. case 3: /* r,m - 2op */
  1071. *andptr++ = t[4];
  1072. *andptr++ = t[5];
  1073. asmand(&p->to, t[6]);
  1074. break;
  1075. case 4: /* m,r - 2op */
  1076. *andptr++ = t[4];
  1077. *andptr++ = t[5];
  1078. asmand(&p->from, t[6]);
  1079. break;
  1080. case 5: /* load full pointer, trash heap */
  1081. if(t[4])
  1082. *andptr++ = t[4];
  1083. switch(p->to.index) {
  1084. default:
  1085. goto bad;
  1086. case D_DS:
  1087. *andptr++ = 0xc5;
  1088. break;
  1089. case D_SS:
  1090. *andptr++ = 0x0f;
  1091. *andptr++ = 0xb2;
  1092. break;
  1093. case D_ES:
  1094. *andptr++ = 0xc4;
  1095. break;
  1096. case D_FS:
  1097. *andptr++ = 0x0f;
  1098. *andptr++ = 0xb4;
  1099. break;
  1100. case D_GS:
  1101. *andptr++ = 0x0f;
  1102. *andptr++ = 0xb5;
  1103. break;
  1104. }
  1105. asmand(&p->from, reg[p->to.type]);
  1106. break;
  1107. case 6: /* double shift */
  1108. z = p->from.type;
  1109. switch(z) {
  1110. default:
  1111. goto bad;
  1112. case D_CONST:
  1113. *andptr++ = 0x0f;
  1114. *andptr++ = t[4];
  1115. asmand(&p->to, reg[p->from.index]);
  1116. *andptr++ = p->from.offset;
  1117. break;
  1118. case D_CL:
  1119. case D_CX:
  1120. *andptr++ = 0x0f;
  1121. *andptr++ = t[5];
  1122. asmand(&p->to, reg[p->from.index]);
  1123. break;
  1124. }
  1125. break;
  1126. case 7: /* imul rm,r */
  1127. *andptr++ = t[4];
  1128. *andptr++ = t[5];
  1129. asmand(&p->from, reg[p->to.type]);
  1130. break;
  1131. }
  1132. }
  1133. void
  1134. asmins(Prog *p)
  1135. {
  1136. andptr = and;
  1137. doasm(p);
  1138. }
  1139. enum{
  1140. ABSD = 0,
  1141. ABSU = 1,
  1142. RELD = 2,
  1143. RELU = 3,
  1144. };
  1145. int modemap[4] = { 0, 1, -1, 2, };
  1146. typedef struct Reloc Reloc;
  1147. struct Reloc
  1148. {
  1149. int n;
  1150. int t;
  1151. uchar *m;
  1152. ulong *a;
  1153. };
  1154. Reloc rels;
  1155. static void
  1156. grow(Reloc *r)
  1157. {
  1158. int t;
  1159. uchar *m, *nm;
  1160. ulong *a, *na;
  1161. t = r->t;
  1162. r->t += 64;
  1163. m = r->m;
  1164. a = r->a;
  1165. r->m = nm = malloc(r->t*sizeof(uchar));
  1166. r->a = na = malloc(r->t*sizeof(ulong));
  1167. memmove(nm, m, t*sizeof(uchar));
  1168. memmove(na, a, t*sizeof(ulong));
  1169. free(m);
  1170. free(a);
  1171. }
  1172. void
  1173. dynreloc(Sym *s, ulong v, int abs)
  1174. {
  1175. int i, k, n;
  1176. uchar *m;
  1177. ulong *a;
  1178. Reloc *r;
  1179. if(s->type == SUNDEF)
  1180. k = abs ? ABSU : RELU;
  1181. else
  1182. k = abs ? ABSD : RELD;
  1183. /* Bprint(&bso, "R %s a=%ld(%lx) %d\n", s->name, v, v, k); */
  1184. k = modemap[k];
  1185. r = &rels;
  1186. n = r->n;
  1187. if(n >= r->t)
  1188. grow(r);
  1189. m = r->m;
  1190. a = r->a;
  1191. for(i = n; i > 0; i--){
  1192. if(v < a[i-1]){ /* happens occasionally for data */
  1193. m[i] = m[i-1];
  1194. a[i] = a[i-1];
  1195. }
  1196. else
  1197. break;
  1198. }
  1199. m[i] = k;
  1200. a[i] = v;
  1201. r->n++;
  1202. }
  1203. static int
  1204. sput(char *s)
  1205. {
  1206. char *p;
  1207. p = s;
  1208. while(*s)
  1209. cput(*s++);
  1210. cput(0);
  1211. return s-p+1;
  1212. }
  1213. void
  1214. asmdyn()
  1215. {
  1216. int i, n, t, c;
  1217. Sym *s;
  1218. ulong la, ra, *a;
  1219. vlong off;
  1220. uchar *m;
  1221. Reloc *r;
  1222. cflush();
  1223. off = seek(cout, 0, 1);
  1224. lput(0);
  1225. t = 0;
  1226. lput(imports);
  1227. t += 4;
  1228. for(i = 0; i < NHASH; i++)
  1229. for(s = hash[i]; s != S; s = s->link)
  1230. if(s->type == SUNDEF){
  1231. lput(s->sig);
  1232. t += 4;
  1233. t += sput(s->name);
  1234. }
  1235. la = 0;
  1236. r = &rels;
  1237. n = r->n;
  1238. m = r->m;
  1239. a = r->a;
  1240. lput(n);
  1241. t += 4;
  1242. for(i = 0; i < n; i++){
  1243. ra = *a-la;
  1244. if(*a < la)
  1245. diag("bad relocation order");
  1246. if(ra < 256)
  1247. c = 0;
  1248. else if(ra < 65536)
  1249. c = 1;
  1250. else
  1251. c = 2;
  1252. cput((c<<6)|*m++);
  1253. t++;
  1254. if(c == 0){
  1255. cput(ra);
  1256. t++;
  1257. }
  1258. else if(c == 1){
  1259. wput(ra);
  1260. t += 2;
  1261. }
  1262. else{
  1263. lput(ra);
  1264. t += 4;
  1265. }
  1266. la = *a++;
  1267. }
  1268. cflush();
  1269. seek(cout, off, 0);
  1270. lput(t);
  1271. if(debug['v']){
  1272. Bprint(&bso, "import table entries = %d\n", imports);
  1273. Bprint(&bso, "export table entries = %d\n", exports);
  1274. }
  1275. }