i386asm.pas 43 KB

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