i386asm.pas 45 KB

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