cpuasm.pas 46 KB

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