i386asm.pas 44 KB

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