i386asm.pas 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  1. {
  2. $Id$
  3. Copyright (c) 1999 by Florian Klaempfl
  4. Contains the assembler object for the i386
  5. * This code was inspired by the NASM sources
  6. The Netwide Assembler is copyright (C) 1996 Simon Tatham and
  7. Julian Hall. All rights reserved.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. ****************************************************************************
  20. }
  21. unit i386asm;
  22. interface
  23. uses
  24. cobjects,
  25. aasm,globals,verbose,
  26. i386base;
  27. {$ifndef NASMDEBUG}
  28. {$define OPTEA}
  29. {$define PASS2FLAG}
  30. {$endif ndef NASMDEBUG}
  31. {$ifndef TP}
  32. {$define ASMDEBUG}
  33. {$endif}
  34. const
  35. MaxPrefixes=4;
  36. type
  37. { this is for quicker determination of the operand type instead of
  38. using opertype and OT ... etc. }
  39. toptype=(top_none,top_reg,top_ref,top_const,top_symbol);
  40. toper=record
  41. ot : longint;
  42. case typ : toptype of
  43. top_none : ();
  44. top_reg : (reg:tregister);
  45. top_ref : (ref:preference);
  46. top_const : (val:longint);
  47. top_symbol : (sym:pasmsymbol;symofs:longint);
  48. end;
  49. pairegalloc = ^tairegalloc;
  50. tairegalloc = object(tai)
  51. allocation : boolean;
  52. reg : tregister;
  53. constructor alloc(r : tregister);
  54. constructor dealloc(r : tregister);
  55. end;
  56. paitempalloc = ^taitempalloc;
  57. taitempalloc = object(tai)
  58. allocation : boolean;
  59. temppos,
  60. tempsize : longint;
  61. constructor alloc(pos,size:longint);
  62. constructor dealloc(pos,size:longint);
  63. end;
  64. pai386 = ^tai386;
  65. tai386 = object(tai)
  66. opcode : tasmop;
  67. opsize : topsize;
  68. condition : TAsmCond;
  69. ops : longint;
  70. oper : array[0..2] of toper;
  71. constructor op_none(op : tasmop;_size : topsize);
  72. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  73. constructor op_const(op : tasmop;_size : topsize;_op1 : longint);
  74. constructor op_ref(op : tasmop;_size : topsize;_op1 : preference);
  75. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  76. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;_op2 : preference);
  77. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: longint);
  78. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister);
  79. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : longint);
  80. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : longint;_op2 : preference);
  81. constructor op_ref_reg(op : tasmop;_size : topsize;_op1 : preference;_op2 : tregister);
  82. { this is only allowed if _op1 is an int value (_op1^.isintvalue=true) }
  83. constructor op_ref_ref(op : tasmop;_size : topsize;_op1,_op2 : preference);
  84. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  85. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister;_op3 : tregister);
  86. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : preference;_op3 : tregister);
  87. constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister; _op3 : preference);
  88. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister;_op3 : preference);
  89. constructor op_sym(op : tasmop;_size : topsize;_op1 : pasmsymbol);
  90. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : pasmsymbol;_op1ofs:longint);
  91. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : pasmsymbol;_op1ofs:longint;_op2 : tregister);
  92. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : pasmsymbol;_op1ofs:longint;_op2 : preference);
  93. procedure loadconst(opidx:longint;l:longint);
  94. procedure loadsymbol(opidx:longint;s:pasmsymbol;sofs:longint);
  95. procedure loadref(opidx:longint;p:preference);
  96. procedure loadreg(opidx:longint;r:tregister);
  97. procedure loadoper(opidx:longint;o:toper);
  98. procedure changeopsize(siz:topsize);
  99. destructor done;virtual;
  100. function getcopy:plinkedlist_item;virtual;
  101. public
  102. procedure SetCondition(c:TAsmCond);
  103. function Pass1(offset:longint):longint;virtual;
  104. procedure Pass2;virtual;
  105. function GetString:string;
  106. private
  107. segprefix : tregister;
  108. { next fields are filled in pass1, so pass2 is faster }
  109. insentry : PInsEntry;
  110. LastInsOffset,
  111. insoffset,
  112. inssize : longint;
  113. procedure init(op : tasmop;_size : topsize); { this need to be called by all constructor }
  114. function InsEnd:longint;
  115. procedure create_ot;
  116. function Matches(p:PInsEntry):longint;
  117. function calcsize(p:PInsEntry):longint;
  118. procedure gencode;
  119. function NeedAddrPrefix(opidx:byte):boolean;
  120. end;
  121. pai386_labeled = ^tai386_labeled;
  122. tai386_labeled = object(tai386)
  123. lab : plabel;
  124. constructor op_lab(op : tasmop; l : plabel);
  125. constructor op_cond_lab(op : tasmop; cond:tasmcond;l : plabel);
  126. destructor done;virtual;
  127. function Pass1(offset:longint):longint;virtual;
  128. procedure Pass2;virtual;
  129. end;
  130. implementation
  131. uses
  132. og386;
  133. {*****************************************************************************
  134. TaiRegAlloc
  135. *****************************************************************************}
  136. constructor tairegalloc.alloc(r : tregister);
  137. begin
  138. inherited init;
  139. typ:=ait_regalloc;
  140. allocation:=true;
  141. reg:=r;
  142. end;
  143. constructor tairegalloc.dealloc(r : tregister);
  144. begin
  145. inherited init;
  146. typ:=ait_regalloc;
  147. allocation:=false;
  148. reg:=r;
  149. end;
  150. {*****************************************************************************
  151. TaiTempAlloc
  152. *****************************************************************************}
  153. constructor taitempalloc.alloc(pos,size:longint);
  154. begin
  155. inherited init;
  156. typ:=ait_tempalloc;
  157. allocation:=true;
  158. temppos:=pos;
  159. tempsize:=size;
  160. end;
  161. constructor taitempalloc.dealloc(pos,size:longint);
  162. begin
  163. inherited init;
  164. typ:=ait_tempalloc;
  165. allocation:=false;
  166. temppos:=pos;
  167. tempsize:=size;
  168. end;
  169. {*****************************************************************************
  170. Tai386 Constructors
  171. *****************************************************************************}
  172. procedure tai386.loadconst(opidx:longint;l:longint);
  173. begin
  174. with oper[opidx] do
  175. begin
  176. if typ=top_ref then
  177. disposereference(ref);
  178. val:=l;
  179. typ:=top_const;
  180. end;
  181. end;
  182. procedure tai386.loadsymbol(opidx:longint;s:pasmsymbol;sofs:longint);
  183. begin
  184. with oper[opidx] do
  185. begin
  186. if typ=top_ref then
  187. disposereference(ref);
  188. sym:=s;
  189. symofs:=sofs;
  190. typ:=top_symbol;
  191. end;
  192. end;
  193. procedure tai386.loadref(opidx:longint;p:preference);
  194. begin
  195. with oper[opidx] do
  196. begin
  197. if typ=top_ref then
  198. disposereference(ref);
  199. if p^.is_immediate then
  200. begin
  201. {$ifdef ASMDEBUG1}
  202. Comment(V_Warning,'Reference immediate');
  203. {$endif}
  204. val:=p^.offset;
  205. disposereference(p);
  206. typ:=top_const;
  207. end
  208. else
  209. begin
  210. ref:=p;
  211. if not(ref^.segment in [R_DS,R_NO,R_DEFAULT_SEG]) then
  212. segprefix:=ref^.segment;
  213. typ:=top_ref;
  214. end;
  215. end;
  216. end;
  217. procedure tai386.loadreg(opidx:longint;r:tregister);
  218. begin
  219. with oper[opidx] do
  220. begin
  221. if typ=top_ref then
  222. disposereference(ref);
  223. reg:=r;
  224. typ:=top_reg;
  225. end;
  226. end;
  227. procedure tai386.loadoper(opidx:longint;o:toper);
  228. begin
  229. if oper[opidx].typ=top_ref then
  230. disposereference(oper[opidx].ref);
  231. oper[opidx]:=o;
  232. { copy also the reference }
  233. if oper[opidx].typ=top_ref then
  234. oper[opidx].ref:=newreference(o.ref^);
  235. end;
  236. procedure tai386.changeopsize(siz:topsize);
  237. begin
  238. opsize:=siz;
  239. end;
  240. procedure tai386.init(op : tasmop;_size : topsize);
  241. begin
  242. typ:=ait_instruction;
  243. insentry:=nil;
  244. LastInsOffset:=-1;
  245. InsOffset:=0;
  246. segprefix:=R_NO;
  247. opcode:=op;
  248. opsize:=_size;
  249. ops:=0;
  250. fillchar(oper,sizeof(oper),0);
  251. end;
  252. constructor tai386.op_none(op : tasmop;_size : topsize);
  253. begin
  254. inherited init;
  255. init(op,_size);
  256. end;
  257. constructor tai386.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  258. begin
  259. inherited init;
  260. init(op,_size);
  261. ops:=1;
  262. loadreg(0,_op1);
  263. end;
  264. constructor tai386.op_const(op : tasmop;_size : topsize;_op1 : longint);
  265. begin
  266. inherited init;
  267. init(op,_size);
  268. ops:=1;
  269. loadconst(0,_op1);
  270. end;
  271. constructor tai386.op_ref(op : tasmop;_size : topsize;_op1 : preference);
  272. begin
  273. inherited init;
  274. init(op,_size);
  275. ops:=1;
  276. loadref(0,_op1);
  277. end;
  278. constructor tai386.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  279. begin
  280. inherited init;
  281. init(op,_size);
  282. ops:=2;
  283. loadreg(0,_op1);
  284. loadreg(1,_op2);
  285. end;
  286. constructor tai386.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: longint);
  287. begin
  288. inherited init;
  289. init(op,_size);
  290. ops:=2;
  291. loadreg(0,_op1);
  292. loadconst(1,_op2);
  293. end;
  294. constructor tai386.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;_op2 : preference);
  295. begin
  296. inherited init;
  297. init(op,_size);
  298. ops:=2;
  299. loadreg(0,_op1);
  300. loadref(1,_op2);
  301. end;
  302. constructor tai386.op_const_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister);
  303. begin
  304. inherited init;
  305. init(op,_size);
  306. ops:=2;
  307. loadconst(0,_op1);
  308. loadreg(1,_op2);
  309. end;
  310. constructor tai386.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : longint);
  311. begin
  312. inherited init;
  313. init(op,_size);
  314. ops:=2;
  315. loadconst(0,_op1);
  316. loadconst(1,_op2);
  317. end;
  318. constructor tai386.op_const_ref(op : tasmop;_size : topsize;_op1 : longint;_op2 : preference);
  319. begin
  320. inherited init;
  321. init(op,_size);
  322. ops:=2;
  323. loadconst(0,_op1);
  324. loadref(1,_op2);
  325. end;
  326. constructor tai386.op_ref_reg(op : tasmop;_size : topsize;_op1 : preference;_op2 : tregister);
  327. begin
  328. inherited init;
  329. init(op,_size);
  330. ops:=2;
  331. loadref(0,_op1);
  332. loadreg(1,_op2);
  333. end;
  334. constructor tai386.op_ref_ref(op : tasmop;_size : topsize;_op1,_op2 : preference);
  335. begin
  336. inherited init;
  337. init(op,_size);
  338. ops:=2;
  339. loadref(0,_op1);
  340. loadref(1,_op2);
  341. end;
  342. constructor tai386.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  343. begin
  344. inherited init;
  345. init(op,_size);
  346. ops:=3;
  347. loadreg(0,_op1);
  348. loadreg(1,_op2);
  349. loadreg(2,_op3);
  350. end;
  351. constructor tai386.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister;_op3 : tregister);
  352. begin
  353. inherited init;
  354. init(op,_size);
  355. ops:=3;
  356. loadconst(0,_op1);
  357. loadreg(1,_op2);
  358. loadreg(2,_op3);
  359. end;
  360. constructor tai386.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;_op3 : preference);
  361. begin
  362. inherited init;
  363. init(op,_size);
  364. ops:=3;
  365. loadreg(0,_op1);
  366. loadreg(1,_op2);
  367. loadref(2,_op3);
  368. end;
  369. constructor tai386.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : preference;_op3 : tregister);
  370. begin
  371. inherited init;
  372. init(op,_size);
  373. ops:=3;
  374. loadconst(0,_op1);
  375. loadref(1,_op2);
  376. loadreg(2,_op3);
  377. end;
  378. constructor tai386.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister;_op3 : preference);
  379. begin
  380. inherited init;
  381. init(op,_size);
  382. ops:=3;
  383. loadconst(0,_op1);
  384. loadreg(1,_op2);
  385. loadref(2,_op3);
  386. end;
  387. constructor tai386.op_sym(op : tasmop;_size : topsize;_op1 : pasmsymbol);
  388. begin
  389. inherited init;
  390. init(op,_size);
  391. ops:=1;
  392. loadsymbol(0,_op1,0);
  393. end;
  394. constructor tai386.op_sym_ofs(op : tasmop;_size : topsize;_op1 : pasmsymbol;_op1ofs:longint);
  395. begin
  396. inherited init;
  397. init(op,_size);
  398. ops:=1;
  399. loadsymbol(0,_op1,_op1ofs);
  400. end;
  401. constructor tai386.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : pasmsymbol;_op1ofs:longint;_op2 : tregister);
  402. begin
  403. inherited init;
  404. init(op,_size);
  405. ops:=2;
  406. loadsymbol(0,_op1,_op1ofs);
  407. loadreg(1,_op2);
  408. end;
  409. constructor tai386.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : pasmsymbol;_op1ofs:longint;_op2 : preference);
  410. begin
  411. inherited init;
  412. init(op,_size);
  413. ops:=2;
  414. loadsymbol(0,_op1,_op1ofs);
  415. loadref(0,_op2);
  416. end;
  417. destructor tai386.done;
  418. var
  419. i : longint;
  420. begin
  421. for i:=1 to ops do
  422. if (oper[i-1].typ=top_ref) then
  423. dispose(oper[i-1].ref);
  424. inherited done;
  425. end;
  426. function tai386.getcopy:plinkedlist_item;
  427. var
  428. i : longint;
  429. p : plinkedlist_item;
  430. begin
  431. p:=inherited getcopy;
  432. { make a copy of the references }
  433. for i:=1 to ops do
  434. if (pai386(p)^.oper[i-1].typ=top_ref) then
  435. begin
  436. new(pai386(p)^.oper[i-1].ref);
  437. pai386(p)^.oper[i-1].ref^:=oper[i-1].ref^;
  438. end;
  439. getcopy:=p;
  440. end;
  441. procedure tai386.SetCondition(c:TAsmCond);
  442. begin
  443. condition:=c;
  444. end;
  445. function tai386.GetString:string;
  446. {$ifdef ASMDEBUG}
  447. var
  448. i : longint;
  449. s : string;
  450. {$endif}
  451. begin
  452. {$ifdef ASMDEBUG}
  453. s:='['+int_op2str[opcode];
  454. for i:=1to ops do
  455. begin
  456. if i=1 then
  457. s:=s+' '
  458. else
  459. s:=s+',';
  460. { type }
  461. if (oper[i-1].ot and OT_REGISTER)<>0 then
  462. s:=s+'reg'
  463. else
  464. if (oper[i-1].ot and OT_IMMEDIATE)<>0 then
  465. s:=s+'imm'
  466. else
  467. if (oper[i-1].ot and OT_MEMORY)<>0 then
  468. s:=s+'mem'
  469. else
  470. s:=s+'???';
  471. { size }
  472. if (oper[i-1].ot and OT_BITS8)<>0 then
  473. s:=s+'8'
  474. else
  475. if (oper[i-1].ot and OT_BITS16)<>0 then
  476. s:=s+'16'
  477. else
  478. if (oper[i-1].ot and OT_BITS32)<>0 then
  479. s:=s+'32'
  480. else
  481. s:=s+'??';
  482. { signed }
  483. if (oper[i-1].ot and OT_SIGNED)<>0 then
  484. s:=s+'s';
  485. end;
  486. GetString:=s+']';
  487. {$else}
  488. GetString:='';
  489. {$endif ASMDEBUG}
  490. end;
  491. {*****************************************************************************
  492. Assembler
  493. *****************************************************************************}
  494. type
  495. ea=packed record
  496. sib_present : boolean;
  497. bytes : byte;
  498. size : byte;
  499. modrm : byte;
  500. sib : byte;
  501. end;
  502. procedure tai386.create_ot;
  503. {
  504. this function will also fix some other fields which only needs to be once
  505. }
  506. var
  507. i,l,relsize : longint;
  508. begin
  509. if ops=0 then
  510. exit;
  511. { update oper[].ot field }
  512. for i:=0 to ops-1 do
  513. with oper[i] do
  514. begin
  515. case typ of
  516. top_reg :
  517. ot:=reg_2_type[reg];
  518. top_ref :
  519. begin
  520. { create ot field }
  521. ot:=OT_MEMORY or opsize_2_type[i,opsize];
  522. if (ref^.base=R_NO) and (ref^.index=R_NO) then
  523. ot:=ot or OT_MEM_OFFS;
  524. { handle also the offsetfixup }
  525. inc(ref^.offset,ref^.offsetfixup);
  526. ref^.offsetfixup:=0;
  527. { fix scalefactor }
  528. if (ref^.index=R_NO) then
  529. ref^.scalefactor:=0
  530. else
  531. if (ref^.scalefactor=0) then
  532. ref^.scalefactor:=1;
  533. end;
  534. top_const :
  535. begin
  536. if (opsize<>S_W) and (val>=-128) and (val<=127) then
  537. ot:=OT_IMM8 or OT_SIGNED
  538. else
  539. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  540. end;
  541. top_symbol :
  542. begin
  543. if LastInsOffset=-1 then
  544. l:=0
  545. else
  546. l:=InsOffset-LastInsOffset;
  547. inc(l,symofs);
  548. if assigned(sym) then
  549. inc(l,sym^.address);
  550. { instruction size will then always become 2 (PFV) }
  551. relsize:=InsOffset+2-l;
  552. if (l<>-1) and
  553. (not assigned(sym) or (sym^.typ<>AS_EXTERNAL)) and
  554. (relsize>=-128) and (relsize<=127) then
  555. ot:=OT_IMM32 or OT_SHORT
  556. else
  557. ot:=OT_IMM32 or OT_NEAR;
  558. end;
  559. end;
  560. end;
  561. end;
  562. function tai386.InsEnd:longint;
  563. begin
  564. InsEnd:=InsOffset+InsSize;
  565. end;
  566. function tai386.Matches(p:PInsEntry):longint;
  567. { * IF_SM stands for Size Match: any operand whose size is not
  568. * explicitly specified by the template is `really' intended to be
  569. * the same size as the first size-specified operand.
  570. * Non-specification is tolerated in the input instruction, but
  571. * _wrong_ specification is not.
  572. *
  573. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  574. * three-operand instructions such as SHLD: it implies that the
  575. * first two operands must match in size, but that the third is
  576. * required to be _unspecified_.
  577. *
  578. * IF_SB invokes Size Byte: operands with unspecified size in the
  579. * template are really bytes, and so no non-byte specification in
  580. * the input instruction will be tolerated. IF_SW similarly invokes
  581. * Size Word, and IF_SD invokes Size Doubleword.
  582. *
  583. * (The default state if neither IF_SM nor IF_SM2 is specified is
  584. * that any operand with unspecified size in the template is
  585. * required to have unspecified size in the instruction too...)
  586. }
  587. var
  588. i,siz,oprs : longint;
  589. begin
  590. Matches:=100;
  591. { Check the opcode and operands }
  592. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  593. begin
  594. Matches:=0;
  595. exit;
  596. end;
  597. { Check that no spurious colons or TOs are present }
  598. for i:=0 to p^.ops-1 do
  599. if (oper[i].ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  600. begin
  601. Matches:=0;
  602. exit;
  603. end;
  604. { Check that the operand flags all match up }
  605. for i:=0 to p^.ops-1 do
  606. begin
  607. if (p^.optypes[i] and (not oper[i].ot) or
  608. ((p^.optypes[i] and OT_SIZE_MASK) and
  609. ((p^.optypes[i] xor oper[i].ot) and OT_SIZE_MASK)))<>0 then
  610. begin
  611. if ((p^.optypes[i] and (not oper[i].ot) and OT_NON_SIZE) or
  612. (oper[i].ot and OT_SIZE_MASK))<>0 then
  613. begin
  614. Matches:=0;
  615. exit;
  616. end
  617. else
  618. Matches:=1;
  619. end;
  620. end;
  621. { Check operand sizes }
  622. { as default an untyped size can get all the sizes, this is different
  623. from nasm, but else we need to do a lot checking which opcodes want
  624. size or not with the automatic size generation }
  625. siz:=$ffffffff;
  626. if (p^.flags and IF_SB)<>0 then
  627. siz:=OT_BITS8
  628. else if (p^.flags and IF_SW)<>0 then
  629. siz:=OT_BITS16
  630. else if (p^.flags and IF_SD)<>0 then
  631. siz:=OT_BITS32
  632. else if (p^.flags and (IF_SM or IF_SM2))<>0 then
  633. begin
  634. if (p^.flags and IF_SM2)<>0 then
  635. oprs:=2
  636. else
  637. oprs:=p^.ops;
  638. for i:=0 to oprs-1 do
  639. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  640. begin
  641. siz:=p^.optypes[i] and OT_SIZE_MASK;
  642. break;
  643. end;
  644. end;
  645. { Check operand sizes }
  646. for i:=0to p^.ops-1 do
  647. begin
  648. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  649. ((oper[i].ot and OT_SIZE_MASK and (not siz))<>0) and
  650. { Immediates can always include smaller size }
  651. ((oper[i].ot and OT_IMMEDIATE)=0) and
  652. (((p^.optypes[i] and OT_SIZE_MASK) or siz)<(oper[i].ot and OT_SIZE_MASK)) then
  653. Matches:=2;
  654. end;
  655. end;
  656. function tai386.Pass1(offset:longint):longint;
  657. var
  658. m,i,size_prob : longint;
  659. p : toper;
  660. begin
  661. Pass1:=0;
  662. { Save the old offset and set the new offset }
  663. InsOffset:=Offset;
  664. { Things which may only be done once, not when a second pass is done to
  665. optimize }
  666. if Insentry=nil then
  667. begin
  668. { Check if error last time then InsSize=-1 }
  669. if InsSize=-1 then
  670. exit;
  671. { Fix the operands which are in AT&T style and we need them in Intel style }
  672. case ops of
  673. 2 : begin
  674. { 0,1 -> 1,0 }
  675. p:=oper[0];
  676. oper[0]:=oper[1];
  677. oper[1]:=p;
  678. end;
  679. 3 : begin
  680. { 0,1,2 -> 2,1,0 }
  681. p:=oper[0];
  682. oper[0]:=oper[2];
  683. oper[2]:=p;
  684. end;
  685. end;
  686. { create the .ot fields }
  687. create_ot;
  688. { set the file postion }
  689. aktfilepos:=fileinfo;
  690. end
  691. else
  692. begin
  693. {$ifdef PASS2FLAG}
  694. { we are here in a second pass, check if the instruction can be optimized }
  695. if (InsEntry^.flags and IF_PASS2)=0 then
  696. begin
  697. Pass1:=InsSize;
  698. exit;
  699. end;
  700. { update the .ot fields, some top_const can be updated }
  701. create_ot;
  702. {$endif}
  703. end;
  704. { Lookup opcode in the table }
  705. InsSize:=-1;
  706. size_prob:=0;
  707. i:=instabcache^[opcode];
  708. if i=-1 then
  709. begin
  710. Comment(V_Warning,'opcode not in the table !');
  711. exit;
  712. end;
  713. insentry:=@instab[i];
  714. while (insentry^.opcode=opcode) do
  715. begin
  716. m:=matches(insentry);
  717. if m=100 then
  718. begin
  719. InsSize:=calcsize(insentry);
  720. if not(segprefix in [R_NO,R_DEFAULT_SEG]) then
  721. inc(InsSize);
  722. Pass1:=InsSize;
  723. LastInsOffset:=InsOffset;
  724. exit;
  725. end;
  726. inc(i);
  727. insentry:=@instab[i];
  728. end;
  729. if insentry^.opcode<>opcode then
  730. begin
  731. case size_prob of
  732. 1 : Comment(V_Warning,GetString+' operation size not specified');
  733. 2 : Comment(V_Warning,GetString+' mismatch in operand sizes');
  734. else
  735. Comment(V_Warning,GetString+' invalid combination of opcode and operands');
  736. end;
  737. end;
  738. { No instruction found, set insentry to nil and inssize to -1 }
  739. insentry:=nil;
  740. inssize:=-1;
  741. LastInsOffset:=-1;
  742. end;
  743. procedure tai386.Pass2;
  744. var
  745. c : longint;
  746. begin
  747. { error in pass1 ? }
  748. if insentry=nil then
  749. exit;
  750. aktfilepos:=fileinfo;
  751. { Segment override }
  752. if not(segprefix in [R_NO,R_DEFAULT_SEG]) then
  753. begin
  754. case segprefix of
  755. R_CS : c:=$2e;
  756. R_DS : c:=$3e;
  757. R_ES : c:=$26;
  758. R_FS : c:=$64;
  759. R_GS : c:=$65;
  760. R_SS : c:=$36;
  761. end;
  762. objectoutput^.writebytes(c,1);
  763. { fix the offset for GenNode }
  764. inc(InsOffset);
  765. end;
  766. { Generate the instruction }
  767. GenCode;
  768. end;
  769. function tai386.NeedAddrPrefix(opidx:byte):boolean;
  770. var
  771. i,b : tregister;
  772. begin
  773. if (OT_MEMORY and (not oper[opidx].ot))=0 then
  774. begin
  775. i:=oper[opidx].ref^.index;
  776. b:=oper[opidx].ref^.base;
  777. if not(i in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) or
  778. not(b in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) then
  779. begin
  780. NeedAddrPrefix:=true;
  781. exit;
  782. end;
  783. end;
  784. NeedAddrPrefix:=false;
  785. end;
  786. function regval(r:tregister):byte;
  787. begin
  788. case r of
  789. R_EAX,R_AX,R_AL,R_ES,R_CR0,R_DR0,R_ST0,R_MM0 :
  790. regval:=0;
  791. R_ECX,R_CX,R_CL,R_CS,R_DR1,R_ST1,R_MM1 :
  792. regval:=1;
  793. R_EDX,R_DX,R_DL,R_SS,R_CR2,R_DR2,R_ST2,R_MM2 :
  794. regval:=2;
  795. R_EBX,R_BX,R_BL,R_DS,R_CR3,R_DR3,R_TR3,R_ST3,R_MM3 :
  796. regval:=3;
  797. R_ESP,R_SP,R_AH,R_FS,R_CR4,R_TR4,R_ST4,R_MM4 :
  798. regval:=4;
  799. R_EBP,R_BP,R_CH,R_GS,R_TR5,R_ST5,R_MM5 :
  800. regval:=5;
  801. R_ESI,R_SI,R_DH,R_DR6,R_TR6,R_ST6,R_MM6 :
  802. regval:=6;
  803. R_EDI,R_DI,R_BH,R_DR7,R_TR7,R_ST7,R_MM7 :
  804. regval:=7;
  805. else
  806. begin
  807. Comment(V_Warning,'invalid register operand given to regval()');
  808. regval:=0;
  809. end;
  810. end;
  811. end;
  812. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  813. const
  814. regs : array[0..31] of tregister=(
  815. R_MM0, R_EAX, R_AX, R_AL, R_MM1, R_ECX, R_CX, R_CL,
  816. R_MM2, R_EDX, R_DX, R_DL, R_MM3, R_EBX, R_BX, R_BL,
  817. R_MM4, R_ESP, R_SP, R_AH, R_MM5, R_EBP, R_BP, R_CH,
  818. R_MM6, R_ESI, R_SI, R_DH, R_MM7, R_EDI, R_DI, R_BH
  819. );
  820. var
  821. j : longint;
  822. i,b : tregister;
  823. sym : pasmsymbol;
  824. md,s : byte;
  825. base,index,scalefactor,
  826. o : longint;
  827. begin
  828. process_ea:=false;
  829. { register ? }
  830. if (input.typ=top_reg) then
  831. begin
  832. j:=0;
  833. while (j<=high(regs)) do
  834. begin
  835. if input.reg=regs[j] then
  836. break;
  837. inc(j);
  838. end;
  839. if j<=high(regs) then
  840. begin
  841. output.sib_present:=false;
  842. output.bytes:=0;
  843. output.modrm:=$c0 or (rfield shl 3) or (j shr 2);
  844. output.size:=1;
  845. process_ea:=true;
  846. end;
  847. exit;
  848. end;
  849. { memory reference }
  850. i:=input.ref^.index;
  851. b:=input.ref^.base;
  852. s:=input.ref^.scalefactor;
  853. o:=input.ref^.offset;
  854. sym:=input.ref^.symbol;
  855. { it's direct address }
  856. if (b=R_NO) and (i=R_NO) then
  857. begin
  858. { it's a pure offset }
  859. output.sib_present:=false;
  860. output.bytes:=4;
  861. output.modrm:=5 or (rfield shl 3);
  862. end
  863. else
  864. { it's an indirection }
  865. begin
  866. { 16 bit address? }
  867. if not((i in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) and
  868. (b in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI])) then
  869. Comment(V_Warning,'16 bit ea not supported');
  870. {$ifdef OPTEA}
  871. { make single reg base }
  872. if (b=R_NO) and (s=1) then
  873. begin
  874. b:=i;
  875. i:=R_NO;
  876. end;
  877. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  878. if (b=R_NO) and
  879. (((s=2) and (i<>R_ESP)) or
  880. (s=3) or (s=5) or (s=9)) then
  881. begin
  882. b:=i;
  883. dec(s);
  884. end;
  885. { swap ESP into base if scalefactor is 1 }
  886. if (s=1) and (i=R_ESP) then
  887. begin
  888. i:=b;
  889. b:=R_ESP;
  890. end;
  891. {$endif}
  892. { wrong, for various reasons }
  893. if (i=R_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (i<>R_NO)) then
  894. exit;
  895. { base }
  896. case b of
  897. R_EAX : base:=0;
  898. R_ECX : base:=1;
  899. R_EDX : base:=2;
  900. R_EBX : base:=3;
  901. R_ESP : base:=4;
  902. R_NO,
  903. R_EBP : base:=5;
  904. R_ESI : base:=6;
  905. R_EDI : base:=7;
  906. else
  907. exit;
  908. end;
  909. { index }
  910. case i of
  911. R_EAX : index:=0;
  912. R_ECX : index:=1;
  913. R_EDX : index:=2;
  914. R_EBX : index:=3;
  915. R_NO : index:=4;
  916. R_EBP : index:=5;
  917. R_ESI : index:=6;
  918. R_EDI : index:=7;
  919. else
  920. exit;
  921. end;
  922. case s of
  923. 0,
  924. 1 : scalefactor:=0;
  925. 2 : scalefactor:=1;
  926. 4 : scalefactor:=2;
  927. 8 : scalefactor:=3;
  928. else
  929. exit;
  930. end;
  931. if (b=R_NO) or
  932. ((b<>R_EBP) and (o=0) and (sym=nil)) then
  933. md:=0
  934. else
  935. if ((o>=-128) and (o<=127) and (sym=nil)) then
  936. md:=1
  937. else
  938. md:=2;
  939. if (b=R_NO) or (md=2) then
  940. output.bytes:=4
  941. else
  942. output.bytes:=md;
  943. { SIB needed ? }
  944. if (i=R_NO) and (b<>R_ESP) then
  945. begin
  946. output.sib_present:=false;
  947. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  948. end
  949. else
  950. begin
  951. output.sib_present:=true;
  952. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  953. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  954. end;
  955. end;
  956. if output.sib_present then
  957. output.size:=2+output.bytes
  958. else
  959. output.size:=1+output.bytes;
  960. process_ea:=true;
  961. end;
  962. function tai386.calcsize(p:PInsEntry):longint;
  963. var
  964. codes : pchar;
  965. c : byte;
  966. len : longint;
  967. ea_data : ea;
  968. begin
  969. len:=0;
  970. codes:=@p^.code;
  971. repeat
  972. c:=ord(codes^);
  973. inc(codes);
  974. case c of
  975. 0 :
  976. break;
  977. 1,2,3 :
  978. begin
  979. inc(codes,c);
  980. inc(len,c);
  981. end;
  982. 8,9,10 :
  983. begin
  984. inc(codes);
  985. inc(len);
  986. end;
  987. 4,5,6,7,
  988. 15,
  989. 12,13,14,
  990. 16,17,18,
  991. 20,21,22,
  992. 40,41,42 :
  993. inc(len);
  994. 24,25,26,
  995. 31,
  996. 48,49,50 :
  997. inc(len,2);
  998. 28,29,30, { we don't have 16 bit immediates code }
  999. 32,33,34,
  1000. 52,53,54,
  1001. 56,57,58 :
  1002. inc(len,4);
  1003. 192,193,194 :
  1004. if NeedAddrPrefix(c-192) then
  1005. inc(len);
  1006. 208 :
  1007. inc(len);
  1008. 200,
  1009. 201,
  1010. 202,
  1011. 209,
  1012. 210 : ;
  1013. 216 :
  1014. begin
  1015. inc(codes);
  1016. inc(len);
  1017. end;
  1018. 224,225,226 :
  1019. begin
  1020. Comment(V_Warning,'not supported');
  1021. end;
  1022. else
  1023. begin
  1024. if (c>=64) and (c<=191) then
  1025. begin
  1026. if not process_ea(oper[(c shr 3) and 7], ea_data, 0) then
  1027. Comment(V_Warning,'invalid effective address')
  1028. else
  1029. inc(len,ea_data.size);
  1030. end
  1031. else
  1032. begin
  1033. Comment(V_Warning,'internal instruction table corrupt: instruction code '+tostr(c)+' given');
  1034. end;
  1035. end;
  1036. end;
  1037. until false;
  1038. calcsize:=len;
  1039. end;
  1040. procedure tai386.GenCode;
  1041. {
  1042. * the actual codes (C syntax, i.e. octal):
  1043. * \0 - terminates the code. (Unless it's a literal of course.)
  1044. * \1, \2, \3 - that many literal bytes follow in the code stream
  1045. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1046. * (POP is never used for CS) depending on operand 0
  1047. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1048. * on operand 0
  1049. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1050. * to the register value of operand 0, 1 or 2
  1051. * \17 - encodes the literal byte 0. (Some compilers don't take
  1052. * kindly to a zero byte in the _middle_ of a compile time
  1053. * string constant, so I had to put this hack in.)
  1054. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1055. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1056. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1057. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1058. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1059. * assembly mode or the address-size override on the operand
  1060. * \37 - a word constant, from the _segment_ part of operand 0
  1061. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1062. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1063. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1064. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1065. * assembly mode or the address-size override on the operand
  1066. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1067. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1068. * field the register value of operand b.
  1069. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1070. * field equal to digit b.
  1071. * \30x - might be an 0x67 byte, depending on the address size of
  1072. * the memory reference in operand x.
  1073. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1074. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1075. * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1076. * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1077. * \322 - indicates that this instruction is only valid when the
  1078. * operand size is the default (instruction to disassembler,
  1079. * generates no code in the assembler)
  1080. * \330 - a literal byte follows in the code stream, to be added
  1081. * to the condition code value of the instruction.
  1082. * \340 - reserve <operand 0> bytes of uninitialised storage.
  1083. * Operand 0 had better be a segmentless constant.
  1084. }
  1085. var
  1086. currval : longint;
  1087. currsym : pasmsymbol;
  1088. procedure getvalsym(opidx:longint);
  1089. begin
  1090. case oper[opidx].typ of
  1091. top_ref :
  1092. begin
  1093. currval:=oper[opidx].ref^.offset;
  1094. currsym:=oper[opidx].ref^.symbol;
  1095. end;
  1096. top_const :
  1097. begin
  1098. currval:=oper[opidx].val;
  1099. currsym:=nil;
  1100. end;
  1101. top_symbol :
  1102. begin
  1103. currval:=oper[opidx].symofs;
  1104. currsym:=oper[opidx].sym;
  1105. end;
  1106. else
  1107. Comment(V_Error,'immediate or reference expected');
  1108. end;
  1109. end;
  1110. const
  1111. CondVal:array[TAsmCond] of byte=($0,
  1112. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1113. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1114. $0, $A, $A, $B, $8, $4);
  1115. var
  1116. c : byte;
  1117. pb,
  1118. codes : pchar;
  1119. bytes : array[0..3] of byte;
  1120. rfield,
  1121. data,s,opidx : longint;
  1122. ea_data : ea;
  1123. begin
  1124. codes:=insentry^.code;
  1125. repeat
  1126. c:=ord(codes^);
  1127. inc(codes);
  1128. case c of
  1129. 0 :
  1130. break;
  1131. 1,2,3 :
  1132. begin
  1133. objectoutput^.writebytes(codes^,c);
  1134. inc(codes,c);
  1135. end;
  1136. 4,6 :
  1137. begin
  1138. case oper[0].reg of
  1139. R_CS :
  1140. begin
  1141. if c=4 then
  1142. bytes[0]:=$f
  1143. else
  1144. bytes[0]:=$e;
  1145. end;
  1146. R_DEFAULT_SEG,
  1147. R_DS :
  1148. begin
  1149. if c=4 then
  1150. bytes[0]:=$1f
  1151. else
  1152. bytes[0]:=$1e;
  1153. end;
  1154. R_ES :
  1155. begin
  1156. if c=4 then
  1157. bytes[0]:=$7
  1158. else
  1159. bytes[0]:=$6;
  1160. end;
  1161. R_SS :
  1162. begin
  1163. if c=4 then
  1164. bytes[0]:=$17
  1165. else
  1166. bytes[0]:=$16;
  1167. end;
  1168. else
  1169. Comment(V_Warning,'bizarre 8086 segment register received');
  1170. end;
  1171. objectoutput^.writebytes(bytes,1);
  1172. end;
  1173. 5,7 :
  1174. begin
  1175. case oper[0].reg of
  1176. R_FS :
  1177. begin
  1178. if c=5 then
  1179. bytes[0]:=$a1
  1180. else
  1181. bytes[0]:=$a0;
  1182. end;
  1183. R_GS :
  1184. begin
  1185. if c=5 then
  1186. bytes[0]:=$a9
  1187. else
  1188. bytes[0]:=$a8;
  1189. end;
  1190. else
  1191. Comment(V_Warning,'bizarre 386 segment register received');
  1192. end;
  1193. objectoutput^.writebytes(bytes,1);
  1194. end;
  1195. 8,9,10 :
  1196. begin
  1197. bytes[0]:=ord(codes^)+regval(oper[c-8].reg);
  1198. inc(codes);
  1199. objectoutput^.writebytes(bytes,1);
  1200. end;
  1201. 15 :
  1202. begin
  1203. bytes[0]:=0;
  1204. objectoutput^.writebytes(bytes,1);
  1205. end;
  1206. 12,13,14 :
  1207. begin
  1208. getvalsym(c-12);
  1209. if (currval<-128) or (currval>127) then
  1210. Comment(V_Warning,'signed byte value exceeds bounds '+tostr(currval));
  1211. if assigned(currsym) then
  1212. objectoutput^.writereloc(currval,1,currsym,relative_false)
  1213. else
  1214. objectoutput^.writebytes(currval,1);
  1215. end;
  1216. 16,17,18 :
  1217. begin
  1218. getvalsym(c-16);
  1219. if (currval<-256) or (currval>255) then
  1220. Comment(V_Warning,'byte value exceeds bounds '+tostr(currval));
  1221. if assigned(currsym) then
  1222. objectoutput^.writereloc(currval,1,currsym,relative_false)
  1223. else
  1224. objectoutput^.writebytes(currval,1);
  1225. end;
  1226. 20,21,22 :
  1227. begin
  1228. getvalsym(c-20);
  1229. if (currval<0) or (currval>255) then
  1230. Comment(V_Warning,'unsigned byte value exceeds bounds '+tostr(currval));
  1231. if assigned(currsym) then
  1232. objectoutput^.writereloc(currval,1,currsym,relative_false)
  1233. else
  1234. objectoutput^.writebytes(currval,1);
  1235. end;
  1236. 24,25,26 :
  1237. begin
  1238. getvalsym(c-24);
  1239. if (currval<-65536) or (currval>65535) then
  1240. Comment(V_Warning,'word value exceeds bounds '+tostr(currval));
  1241. if assigned(currsym) then
  1242. objectoutput^.writereloc(currval,2,currsym,relative_false)
  1243. else
  1244. objectoutput^.writebytes(currval,2);
  1245. end;
  1246. 31 :
  1247. begin
  1248. Comment(V_Warning,'not supported');
  1249. end;
  1250. 28,29,30 :
  1251. begin
  1252. getvalsym(c-28);
  1253. if assigned(currsym) then
  1254. objectoutput^.writereloc(currval,4,currsym,relative_false)
  1255. else
  1256. objectoutput^.writebytes(currval,4);
  1257. end;
  1258. 32,33,34 :
  1259. begin
  1260. getvalsym(c-32);
  1261. if assigned(currsym) then
  1262. objectoutput^.writereloc(currval,4,currsym,relative_false)
  1263. else
  1264. objectoutput^.writebytes(currval,4);
  1265. end;
  1266. 40,41,42 :
  1267. begin
  1268. getvalsym(c-40);
  1269. data:=currval-insend;
  1270. if assigned(currsym) then
  1271. inc(data,currsym^.address);
  1272. if (data>127) or (data<-128) then
  1273. Comment(V_Warning,'short jump is out of range '+tostr(data));
  1274. objectoutput^.writebytes(data,1);
  1275. end;
  1276. 48,49,50 :
  1277. begin
  1278. Comment(V_Warning,'rel2adr not supported');
  1279. end;
  1280. 52,53,54 :
  1281. begin
  1282. getvalsym(c-52);
  1283. if assigned(currsym) then
  1284. objectoutput^.writereloc(currval,4,currsym,relative_true)
  1285. else
  1286. objectoutput^.writereloc(currval-insend,4,nil,relative_false)
  1287. end;
  1288. 56,57,58 :
  1289. begin
  1290. getvalsym(c-56);
  1291. if assigned(currsym) then
  1292. objectoutput^.writereloc(currval,4,currsym,relative_true)
  1293. else
  1294. objectoutput^.writereloc(currval-insend,4,nil,relative_false)
  1295. end;
  1296. 192,193,194 :
  1297. begin
  1298. if NeedAddrPrefix(c-192) then
  1299. begin
  1300. bytes[0]:=$67;
  1301. objectoutput^.writebytes(bytes,1);
  1302. end;
  1303. end;
  1304. 200 :
  1305. begin
  1306. bytes[0]:=$67;
  1307. objectoutput^.writebytes(bytes,1);
  1308. end;
  1309. 201 :
  1310. begin
  1311. end;
  1312. 202 :
  1313. begin
  1314. end;
  1315. 208 :
  1316. begin
  1317. bytes[0]:=$66;
  1318. objectoutput^.writebytes(bytes,1);
  1319. end;
  1320. 209 :
  1321. begin
  1322. end;
  1323. 210 :
  1324. begin
  1325. end;
  1326. 216 :
  1327. begin
  1328. bytes[0]:=ord(codes^)+condval[condition];
  1329. inc(codes);
  1330. objectoutput^.writebytes(bytes,1);
  1331. end;
  1332. 224,225,226 :
  1333. begin
  1334. Comment(V_Warning,'not supported');
  1335. end
  1336. else
  1337. begin
  1338. if (c>=64) and (c<=191) then
  1339. begin
  1340. if (c<127) then
  1341. begin
  1342. if (oper[c and 7].typ=top_reg) then
  1343. rfield:=regval(oper[c and 7].reg)
  1344. else
  1345. rfield:=regval(oper[c and 7].ref^.base);
  1346. end
  1347. else
  1348. rfield:=c and 7;
  1349. opidx:=(c shr 3) and 7;
  1350. if not process_ea(oper[opidx], ea_data, rfield) then
  1351. Comment(V_Warning,'invalid effective address');
  1352. pb:=@bytes;
  1353. pb^:=chr(ea_data.modrm);
  1354. inc(pb);
  1355. if ea_data.sib_present then
  1356. begin
  1357. pb^:=chr(ea_data.sib);
  1358. inc(pb);
  1359. end;
  1360. s:=pb-pchar(@bytes);
  1361. objectoutput^.writebytes(bytes,s);
  1362. case ea_data.bytes of
  1363. 0 : ;
  1364. 1 :
  1365. begin
  1366. if (oper[opidx].ot and OT_MEMORY)=OT_MEMORY then
  1367. objectoutput^.writereloc(oper[opidx].ref^.offset,1,oper[opidx].ref^.symbol,relative_false)
  1368. else
  1369. begin
  1370. bytes[0]:=oper[opidx].ref^.offset;
  1371. objectoutput^.writebytes(bytes,1);
  1372. end;
  1373. inc(s);
  1374. end;
  1375. 2,4 :
  1376. begin
  1377. objectoutput^.writereloc(oper[opidx].ref^.offset,ea_data.bytes,
  1378. oper[opidx].ref^.symbol,relative_false);
  1379. inc(s,ea_data.bytes);
  1380. end;
  1381. end;
  1382. end
  1383. else
  1384. begin
  1385. Comment(V_Warning,'internal instruction table corrupt: instruction code '+tostr(c)+' given');
  1386. end;
  1387. end;
  1388. end;
  1389. until false;
  1390. end;
  1391. {*****************************************************************************
  1392. Tai_Labeled
  1393. *****************************************************************************}
  1394. constructor tai386_labeled.op_lab(op : tasmop; l : plabel);
  1395. begin
  1396. inherited op_none(op,S_NO);
  1397. typ:=ait_labeled_instruction;
  1398. lab:=l;
  1399. lab^.is_used:=true;
  1400. inc(lab^.refcount);
  1401. end;
  1402. constructor tai386_labeled.op_cond_lab(op : tasmop; cond:tasmcond;l : plabel);
  1403. begin
  1404. inherited op_none(op,S_NO);
  1405. condition:=cond;
  1406. typ:=ait_labeled_instruction;
  1407. lab:=l;
  1408. lab^.is_used:=true;
  1409. inc(lab^.refcount);
  1410. end;
  1411. destructor tai386_labeled.done;
  1412. begin
  1413. dec(lab^.refcount);
  1414. if lab^.refcount=0 then
  1415. Begin
  1416. lab^.is_used := False;
  1417. If Not(lab^.is_set) Then
  1418. Dispose(lab);
  1419. End;
  1420. inherited done;
  1421. end;
  1422. function tai386_labeled.Pass1(offset:longint):longint;
  1423. begin
  1424. { Only create the Operand if it's not set yet }
  1425. ops:=1;
  1426. loadsymbol(0,nil,lab^.address);
  1427. Pass1:=inherited Pass1(offset);
  1428. end;
  1429. procedure tai386_labeled.Pass2;
  1430. begin
  1431. { update the address which can be changed if it was
  1432. a forward reference }
  1433. oper[0].symofs:=lab^.address;
  1434. inherited Pass2;
  1435. end;
  1436. end.
  1437. {
  1438. $Log$
  1439. Revision 1.2 1999-05-02 21:33:52 florian
  1440. * several bugs regarding -Or fixed
  1441. Revision 1.1 1999/05/01 13:24:23 peter
  1442. * merged nasm compiler
  1443. * old asm moved to oldasm/
  1444. Revision 1.18 1999/04/16 11:49:51 peter
  1445. + tempalloc
  1446. + -at to show temp alloc info in .s file
  1447. Revision 1.17 1999/04/16 09:35:11 pierre
  1448. + tai constructors needed for SHRD and IMUL added
  1449. Revision 1.16 1999/04/01 21:58:21 peter
  1450. * small fixes for proces_ea
  1451. Revision 1.15 1999/03/31 13:55:32 peter
  1452. * assembler inlining working for ag386bin
  1453. Revision 1.14 1999/03/29 16:05:49 peter
  1454. * optimizer working for ag386bin
  1455. Revision 1.13 1999/03/26 00:01:11 peter
  1456. * first things for optimizer (compiles but cycle crashes)
  1457. Revision 1.12 1999/03/12 00:20:04 pierre
  1458. + win32 output working !
  1459. Revision 1.11 1999/03/10 13:41:08 pierre
  1460. + partial implementation for win32 !
  1461. winhello works but pp still does not !
  1462. Revision 1.10 1999/03/09 19:25:24 peter
  1463. * only pass jmp's a second time in pass1
  1464. Revision 1.9 1999/03/08 14:51:06 peter
  1465. + smartlinking for ag386bin
  1466. Revision 1.8 1999/03/06 17:24:19 peter
  1467. * rewritten intel parser a lot, especially reference reading
  1468. * size checking added for asm parsers
  1469. Revision 1.7 1999/03/02 02:56:19 peter
  1470. + stabs support for binary writers
  1471. * more fixes and missing updates from the previous commit :(
  1472. Revision 1.6 1999/03/01 15:46:21 peter
  1473. * ag386bin finally make cycles correct
  1474. * prefixes are now also normal opcodes
  1475. Revision 1.5 1999/02/26 00:48:28 peter
  1476. * assembler writers fixed for ag386bin
  1477. Revision 1.4 1999/02/25 21:03:02 peter
  1478. * ag386bin updates
  1479. + coff writer
  1480. Revision 1.3 1999/02/22 02:44:17 peter
  1481. * ag386bin doesn't use i386.pas anymore
  1482. Revision 1.2 1999/02/22 02:16:02 peter
  1483. * updates for ag386bin
  1484. Revision 1.1 1999/02/16 17:59:37 peter
  1485. + initial files
  1486. }