cpuasm.pas 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  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,j,asize,oprs : longint;
  711. siz : array[0..2] of longint;
  712. begin
  713. Matches:=100;
  714. { Check the opcode and operands }
  715. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  716. begin
  717. Matches:=0;
  718. exit;
  719. end;
  720. { Check that no spurious colons or TOs are present }
  721. for i:=0 to p^.ops-1 do
  722. if (oper[i].ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  723. begin
  724. Matches:=0;
  725. exit;
  726. end;
  727. { Check that the operand flags all match up }
  728. for i:=0 to p^.ops-1 do
  729. begin
  730. if (p^.optypes[i] and (not oper[i].ot) or
  731. ((p^.optypes[i] and OT_SIZE_MASK) and
  732. ((p^.optypes[i] xor oper[i].ot) and OT_SIZE_MASK)))<>0 then
  733. begin
  734. if ((p^.optypes[i] and (not oper[i].ot) and OT_NON_SIZE) or
  735. (oper[i].ot and OT_SIZE_MASK))<>0 then
  736. begin
  737. Matches:=0;
  738. exit;
  739. end
  740. else
  741. Matches:=1;
  742. end;
  743. end;
  744. { Check operand sizes }
  745. { as default an untyped size can get all the sizes, this is different
  746. from nasm, but else we need to do a lot checking which opcodes want
  747. size or not with the automatic size generation }
  748. asize:=$ffffffff;
  749. if (p^.flags and IF_SB)<>0 then
  750. asize:=OT_BITS8
  751. else if (p^.flags and IF_SW)<>0 then
  752. asize:=OT_BITS16
  753. else if (p^.flags and IF_SD)<>0 then
  754. asize:=OT_BITS32;
  755. if (p^.flags and IF_ARMASK)<>0 then
  756. begin
  757. siz[0]:=0;
  758. siz[1]:=0;
  759. siz[2]:=0;
  760. if (p^.flags and IF_AR0)<>0 then
  761. siz[0]:=asize
  762. else if (p^.flags and IF_AR1)<>0 then
  763. siz[1]:=asize
  764. else if (p^.flags and IF_AR2)<>0 then
  765. siz[2]:=asize;
  766. end
  767. else
  768. begin
  769. { siz[0]:=asize;
  770. siz[1]:=asize;
  771. siz[2]:=asize; }
  772. { we can leave because the size for all operands is forced to be
  773. the same }
  774. exit;
  775. end;
  776. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  777. begin
  778. if (p^.flags and IF_SM2)<>0 then
  779. oprs:=2
  780. else
  781. oprs:=p^.ops;
  782. for i:=0 to oprs-1 do
  783. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  784. begin
  785. for j:=0 to oprs-1 do
  786. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  787. break;
  788. end;
  789. end
  790. else
  791. oprs:=2;
  792. { Check operand sizes }
  793. for i:=0to p^.ops-1 do
  794. begin
  795. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  796. ((oper[i].ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  797. { Immediates can always include smaller size }
  798. ((oper[i].ot and OT_IMMEDIATE)=0) and
  799. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i].ot and OT_SIZE_MASK)) then
  800. Matches:=2;
  801. end;
  802. end;
  803. procedure taicpu.ResetPass2;
  804. begin
  805. { we are here in a second pass, check if the instruction can be optimized }
  806. if assigned(InsEntry) and
  807. ((InsEntry^.flags and IF_PASS2)<>0) then
  808. begin
  809. InsEntry:=nil;
  810. InsSize:=0;
  811. end;
  812. LastInsOffset:=-1;
  813. end;
  814. function taicpu.Pass1(offset:longint):longint;
  815. var
  816. m,i : longint;
  817. begin
  818. Pass1:=0;
  819. { Save the old offset and set the new offset }
  820. InsOffset:=Offset;
  821. { Things which may only be done once, not when a second pass is done to
  822. optimize }
  823. if Insentry=nil then
  824. begin
  825. { Check if error last time then InsSize=-1 }
  826. if InsSize=-1 then
  827. exit;
  828. { We need intel style operands }
  829. SwapOperands;
  830. { create the .ot fields }
  831. create_ot;
  832. { set the file postion }
  833. aktfilepos:=fileinfo;
  834. end
  835. else
  836. begin
  837. {$ifdef PASS2FLAG}
  838. { we are here in a second pass, check if the instruction can be optimized }
  839. if (InsEntry^.flags and IF_PASS2)=0 then
  840. begin
  841. Pass1:=InsSize;
  842. exit;
  843. end;
  844. { update the .ot fields, some top_const can be updated }
  845. create_ot;
  846. {$endif}
  847. end;
  848. { Lookup opcode in the table }
  849. InsSize:=-1;
  850. i:=instabcache^[opcode];
  851. if i=-1 then
  852. begin
  853. {$ifdef TP}
  854. Message1(asmw_e_opcode_not_in_table,'');
  855. {$else}
  856. Message1(asmw_e_opcode_not_in_table,att_op2str[opcode]);
  857. {$endif}
  858. exit;
  859. end;
  860. insentry:=@instab[i];
  861. while (insentry^.opcode=opcode) do
  862. begin
  863. m:=matches(insentry);
  864. if m=100 then
  865. begin
  866. InsSize:=calcsize(insentry);
  867. if (segprefix<>R_NO) then
  868. inc(InsSize);
  869. Pass1:=InsSize;
  870. LastInsOffset:=InsOffset;
  871. exit;
  872. end;
  873. inc(i);
  874. insentry:=@instab[i];
  875. end;
  876. if insentry^.opcode<>opcode then
  877. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  878. { No instruction found, set insentry to nil and inssize to -1 }
  879. insentry:=nil;
  880. inssize:=-1;
  881. LastInsOffset:=-1;
  882. end;
  883. procedure taicpu.Pass2;
  884. var
  885. c : longint;
  886. begin
  887. { error in pass1 ? }
  888. if insentry=nil then
  889. exit;
  890. aktfilepos:=fileinfo;
  891. { Segment override }
  892. if (segprefix<>R_NO) then
  893. begin
  894. case segprefix of
  895. R_CS : c:=$2e;
  896. R_DS : c:=$3e;
  897. R_ES : c:=$26;
  898. R_FS : c:=$64;
  899. R_GS : c:=$65;
  900. R_SS : c:=$36;
  901. end;
  902. objectoutput^.writebytes(c,1);
  903. { fix the offset for GenNode }
  904. inc(InsOffset);
  905. end;
  906. { Generate the instruction }
  907. GenCode;
  908. end;
  909. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  910. var
  911. i,b : tregister;
  912. begin
  913. if (OT_MEMORY and (not oper[opidx].ot))=0 then
  914. begin
  915. i:=oper[opidx].ref^.index;
  916. b:=oper[opidx].ref^.base;
  917. if not(i in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) or
  918. not(b in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) then
  919. begin
  920. NeedAddrPrefix:=true;
  921. exit;
  922. end;
  923. end;
  924. NeedAddrPrefix:=false;
  925. end;
  926. function regval(r:tregister):byte;
  927. begin
  928. case r of
  929. R_EAX,R_AX,R_AL,R_ES,R_CR0,R_DR0,R_ST,R_ST0,R_MM0 :
  930. regval:=0;
  931. R_ECX,R_CX,R_CL,R_CS,R_DR1,R_ST1,R_MM1 :
  932. regval:=1;
  933. R_EDX,R_DX,R_DL,R_SS,R_CR2,R_DR2,R_ST2,R_MM2 :
  934. regval:=2;
  935. R_EBX,R_BX,R_BL,R_DS,R_CR3,R_DR3,R_TR3,R_ST3,R_MM3 :
  936. regval:=3;
  937. R_ESP,R_SP,R_AH,R_FS,R_CR4,R_TR4,R_ST4,R_MM4 :
  938. regval:=4;
  939. R_EBP,R_BP,R_CH,R_GS,R_TR5,R_ST5,R_MM5 :
  940. regval:=5;
  941. R_ESI,R_SI,R_DH,R_DR6,R_TR6,R_ST6,R_MM6 :
  942. regval:=6;
  943. R_EDI,R_DI,R_BH,R_DR7,R_TR7,R_ST7,R_MM7 :
  944. regval:=7;
  945. else
  946. begin
  947. internalerror(777001);
  948. regval:=0;
  949. end;
  950. end;
  951. end;
  952. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  953. const
  954. regs : array[0..31] of tregister=(
  955. R_MM0, R_EAX, R_AX, R_AL, R_MM1, R_ECX, R_CX, R_CL,
  956. R_MM2, R_EDX, R_DX, R_DL, R_MM3, R_EBX, R_BX, R_BL,
  957. R_MM4, R_ESP, R_SP, R_AH, R_MM5, R_EBP, R_BP, R_CH,
  958. R_MM6, R_ESI, R_SI, R_DH, R_MM7, R_EDI, R_DI, R_BH
  959. );
  960. var
  961. j : longint;
  962. i,b : tregister;
  963. sym : pasmsymbol;
  964. md,s : byte;
  965. base,index,scalefactor,
  966. o : longint;
  967. begin
  968. process_ea:=false;
  969. { register ? }
  970. if (input.typ=top_reg) then
  971. begin
  972. j:=0;
  973. while (j<=high(regs)) do
  974. begin
  975. if input.reg=regs[j] then
  976. break;
  977. inc(j);
  978. end;
  979. if j<=high(regs) then
  980. begin
  981. output.sib_present:=false;
  982. output.bytes:=0;
  983. output.modrm:=$c0 or (rfield shl 3) or (j shr 2);
  984. output.size:=1;
  985. process_ea:=true;
  986. end;
  987. exit;
  988. end;
  989. { memory reference }
  990. i:=input.ref^.index;
  991. b:=input.ref^.base;
  992. s:=input.ref^.scalefactor;
  993. o:=input.ref^.offset;
  994. sym:=input.ref^.symbol;
  995. { it's direct address }
  996. if (b=R_NO) and (i=R_NO) then
  997. begin
  998. { it's a pure offset }
  999. output.sib_present:=false;
  1000. output.bytes:=4;
  1001. output.modrm:=5 or (rfield shl 3);
  1002. end
  1003. else
  1004. { it's an indirection }
  1005. begin
  1006. { 16 bit address? }
  1007. if not((i in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) and
  1008. (b in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI])) then
  1009. Message(asmw_e_16bit_not_supported);
  1010. {$ifdef OPTEA}
  1011. { make single reg base }
  1012. if (b=R_NO) and (s=1) then
  1013. begin
  1014. b:=i;
  1015. i:=R_NO;
  1016. end;
  1017. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1018. if (b=R_NO) and
  1019. (((s=2) and (i<>R_ESP)) or
  1020. (s=3) or (s=5) or (s=9)) then
  1021. begin
  1022. b:=i;
  1023. dec(s);
  1024. end;
  1025. { swap ESP into base if scalefactor is 1 }
  1026. if (s=1) and (i=R_ESP) then
  1027. begin
  1028. i:=b;
  1029. b:=R_ESP;
  1030. end;
  1031. {$endif}
  1032. { wrong, for various reasons }
  1033. if (i=R_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (i<>R_NO)) then
  1034. exit;
  1035. { base }
  1036. case b of
  1037. R_EAX : base:=0;
  1038. R_ECX : base:=1;
  1039. R_EDX : base:=2;
  1040. R_EBX : base:=3;
  1041. R_ESP : base:=4;
  1042. R_NO,
  1043. R_EBP : base:=5;
  1044. R_ESI : base:=6;
  1045. R_EDI : base:=7;
  1046. else
  1047. exit;
  1048. end;
  1049. { index }
  1050. case i of
  1051. R_EAX : index:=0;
  1052. R_ECX : index:=1;
  1053. R_EDX : index:=2;
  1054. R_EBX : index:=3;
  1055. R_NO : index:=4;
  1056. R_EBP : index:=5;
  1057. R_ESI : index:=6;
  1058. R_EDI : index:=7;
  1059. else
  1060. exit;
  1061. end;
  1062. case s of
  1063. 0,
  1064. 1 : scalefactor:=0;
  1065. 2 : scalefactor:=1;
  1066. 4 : scalefactor:=2;
  1067. 8 : scalefactor:=3;
  1068. else
  1069. exit;
  1070. end;
  1071. if (b=R_NO) or
  1072. ((b<>R_EBP) and (o=0) and (sym=nil)) then
  1073. md:=0
  1074. else
  1075. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1076. md:=1
  1077. else
  1078. md:=2;
  1079. if (b=R_NO) or (md=2) then
  1080. output.bytes:=4
  1081. else
  1082. output.bytes:=md;
  1083. { SIB needed ? }
  1084. if (i=R_NO) and (b<>R_ESP) then
  1085. begin
  1086. output.sib_present:=false;
  1087. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1088. end
  1089. else
  1090. begin
  1091. output.sib_present:=true;
  1092. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1093. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1094. end;
  1095. end;
  1096. if output.sib_present then
  1097. output.size:=2+output.bytes
  1098. else
  1099. output.size:=1+output.bytes;
  1100. process_ea:=true;
  1101. end;
  1102. function taicpu.calcsize(p:PInsEntry):longint;
  1103. var
  1104. codes : pchar;
  1105. c : byte;
  1106. len : longint;
  1107. ea_data : ea;
  1108. begin
  1109. len:=0;
  1110. codes:=@p^.code;
  1111. repeat
  1112. c:=ord(codes^);
  1113. inc(codes);
  1114. case c of
  1115. 0 :
  1116. break;
  1117. 1,2,3 :
  1118. begin
  1119. inc(codes,c);
  1120. inc(len,c);
  1121. end;
  1122. 8,9,10 :
  1123. begin
  1124. inc(codes);
  1125. inc(len);
  1126. end;
  1127. 4,5,6,7 :
  1128. begin
  1129. if opsize=S_W then
  1130. inc(len,2)
  1131. else
  1132. inc(len);
  1133. end;
  1134. 15,
  1135. 12,13,14,
  1136. 16,17,18,
  1137. 20,21,22,
  1138. 40,41,42 :
  1139. inc(len);
  1140. 24,25,26,
  1141. 31,
  1142. 48,49,50 :
  1143. inc(len,2);
  1144. 28,29,30, { we don't have 16 bit immediates code }
  1145. 32,33,34,
  1146. 52,53,54,
  1147. 56,57,58 :
  1148. inc(len,4);
  1149. 192,193,194 :
  1150. if NeedAddrPrefix(c-192) then
  1151. inc(len);
  1152. 208 :
  1153. inc(len);
  1154. 200,
  1155. 201,
  1156. 202,
  1157. 209,
  1158. 210,
  1159. 217,218,219 : ;
  1160. 216 :
  1161. begin
  1162. inc(codes);
  1163. inc(len);
  1164. end;
  1165. 224,225,226 :
  1166. begin
  1167. InternalError(777002);
  1168. end;
  1169. else
  1170. begin
  1171. if (c>=64) and (c<=191) then
  1172. begin
  1173. if not process_ea(oper[(c shr 3) and 7], ea_data, 0) then
  1174. Message(asmw_e_invalid_effective_address)
  1175. else
  1176. inc(len,ea_data.size);
  1177. end
  1178. else
  1179. InternalError(777003);
  1180. end;
  1181. end;
  1182. until false;
  1183. calcsize:=len;
  1184. end;
  1185. procedure taicpu.GenCode;
  1186. {
  1187. * the actual codes (C syntax, i.e. octal):
  1188. * \0 - terminates the code. (Unless it's a literal of course.)
  1189. * \1, \2, \3 - that many literal bytes follow in the code stream
  1190. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1191. * (POP is never used for CS) depending on operand 0
  1192. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1193. * on operand 0
  1194. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1195. * to the register value of operand 0, 1 or 2
  1196. * \17 - encodes the literal byte 0. (Some compilers don't take
  1197. * kindly to a zero byte in the _middle_ of a compile time
  1198. * string constant, so I had to put this hack in.)
  1199. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1200. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1201. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1202. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1203. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1204. * assembly mode or the address-size override on the operand
  1205. * \37 - a word constant, from the _segment_ part of operand 0
  1206. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1207. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1208. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1209. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1210. * assembly mode or the address-size override on the operand
  1211. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1212. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1213. * field the register value of operand b.
  1214. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1215. * field equal to digit b.
  1216. * \30x - might be an 0x67 byte, depending on the address size of
  1217. * the memory reference in operand x.
  1218. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1219. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1220. * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1221. * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1222. * \322 - indicates that this instruction is only valid when the
  1223. * operand size is the default (instruction to disassembler,
  1224. * generates no code in the assembler)
  1225. * \330 - a literal byte follows in the code stream, to be added
  1226. * to the condition code value of the instruction.
  1227. * \340 - reserve <operand 0> bytes of uninitialised storage.
  1228. * Operand 0 had better be a segmentless constant.
  1229. }
  1230. var
  1231. currval : longint;
  1232. currsym : pasmsymbol;
  1233. procedure getvalsym(opidx:longint);
  1234. begin
  1235. case oper[opidx].typ of
  1236. top_ref :
  1237. begin
  1238. currval:=oper[opidx].ref^.offset;
  1239. currsym:=oper[opidx].ref^.symbol;
  1240. end;
  1241. top_const :
  1242. begin
  1243. currval:=oper[opidx].val;
  1244. currsym:=nil;
  1245. end;
  1246. top_symbol :
  1247. begin
  1248. currval:=oper[opidx].symofs;
  1249. currsym:=oper[opidx].sym;
  1250. end;
  1251. else
  1252. Message(asmw_e_immediate_or_reference_expected);
  1253. end;
  1254. end;
  1255. const
  1256. CondVal:array[TAsmCond] of byte=($0,
  1257. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1258. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1259. $0, $A, $A, $B, $8, $4);
  1260. var
  1261. c : byte;
  1262. pb,
  1263. codes : pchar;
  1264. bytes : array[0..3] of byte;
  1265. rfield,
  1266. data,s,opidx : longint;
  1267. ea_data : ea;
  1268. begin
  1269. codes:=insentry^.code;
  1270. { Force word push/pop for registers }
  1271. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1272. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1273. begin
  1274. bytes[0]:=$66;
  1275. objectoutput^.writebytes(bytes,1);
  1276. end;
  1277. repeat
  1278. c:=ord(codes^);
  1279. inc(codes);
  1280. case c of
  1281. 0 :
  1282. break;
  1283. 1,2,3 :
  1284. begin
  1285. objectoutput^.writebytes(codes^,c);
  1286. inc(codes,c);
  1287. end;
  1288. 4,6 :
  1289. begin
  1290. case oper[0].reg of
  1291. R_CS :
  1292. begin
  1293. if c=4 then
  1294. bytes[0]:=$f
  1295. else
  1296. bytes[0]:=$e;
  1297. end;
  1298. R_NO,
  1299. R_DS :
  1300. begin
  1301. if c=4 then
  1302. bytes[0]:=$1f
  1303. else
  1304. bytes[0]:=$1e;
  1305. end;
  1306. R_ES :
  1307. begin
  1308. if c=4 then
  1309. bytes[0]:=$7
  1310. else
  1311. bytes[0]:=$6;
  1312. end;
  1313. R_SS :
  1314. begin
  1315. if c=4 then
  1316. bytes[0]:=$17
  1317. else
  1318. bytes[0]:=$16;
  1319. end;
  1320. else
  1321. InternalError(777004);
  1322. end;
  1323. objectoutput^.writebytes(bytes,1);
  1324. end;
  1325. 5,7 :
  1326. begin
  1327. case oper[0].reg of
  1328. R_FS :
  1329. begin
  1330. if c=5 then
  1331. bytes[0]:=$a1
  1332. else
  1333. bytes[0]:=$a0;
  1334. end;
  1335. R_GS :
  1336. begin
  1337. if c=5 then
  1338. bytes[0]:=$a9
  1339. else
  1340. bytes[0]:=$a8;
  1341. end;
  1342. else
  1343. InternalError(777005);
  1344. end;
  1345. objectoutput^.writebytes(bytes,1);
  1346. end;
  1347. 8,9,10 :
  1348. begin
  1349. bytes[0]:=ord(codes^)+regval(oper[c-8].reg);
  1350. inc(codes);
  1351. objectoutput^.writebytes(bytes,1);
  1352. end;
  1353. 15 :
  1354. begin
  1355. bytes[0]:=0;
  1356. objectoutput^.writebytes(bytes,1);
  1357. end;
  1358. 12,13,14 :
  1359. begin
  1360. getvalsym(c-12);
  1361. if (currval<-128) or (currval>127) then
  1362. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1363. if assigned(currsym) then
  1364. objectoutput^.writereloc(currval,1,currsym,relative_false)
  1365. else
  1366. objectoutput^.writebytes(currval,1);
  1367. end;
  1368. 16,17,18 :
  1369. begin
  1370. getvalsym(c-16);
  1371. if (currval<-256) or (currval>255) then
  1372. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1373. if assigned(currsym) then
  1374. objectoutput^.writereloc(currval,1,currsym,relative_false)
  1375. else
  1376. objectoutput^.writebytes(currval,1);
  1377. end;
  1378. 20,21,22 :
  1379. begin
  1380. getvalsym(c-20);
  1381. if (currval<0) or (currval>255) then
  1382. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1383. if assigned(currsym) then
  1384. objectoutput^.writereloc(currval,1,currsym,relative_false)
  1385. else
  1386. objectoutput^.writebytes(currval,1);
  1387. end;
  1388. 24,25,26 :
  1389. begin
  1390. getvalsym(c-24);
  1391. if (currval<-65536) or (currval>65535) then
  1392. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1393. if assigned(currsym) then
  1394. objectoutput^.writereloc(currval,2,currsym,relative_false)
  1395. else
  1396. objectoutput^.writebytes(currval,2);
  1397. end;
  1398. 28,29,30 :
  1399. begin
  1400. getvalsym(c-28);
  1401. if assigned(currsym) then
  1402. objectoutput^.writereloc(currval,4,currsym,relative_false)
  1403. else
  1404. objectoutput^.writebytes(currval,4);
  1405. end;
  1406. 32,33,34 :
  1407. begin
  1408. getvalsym(c-32);
  1409. if assigned(currsym) then
  1410. objectoutput^.writereloc(currval,4,currsym,relative_false)
  1411. else
  1412. objectoutput^.writebytes(currval,4);
  1413. end;
  1414. 40,41,42 :
  1415. begin
  1416. getvalsym(c-40);
  1417. data:=currval-insend;
  1418. if assigned(currsym) then
  1419. inc(data,currsym^.address);
  1420. if (data>127) or (data<-128) then
  1421. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1422. objectoutput^.writebytes(data,1);
  1423. end;
  1424. 52,53,54 :
  1425. begin
  1426. getvalsym(c-52);
  1427. if assigned(currsym) then
  1428. objectoutput^.writereloc(currval,4,currsym,relative_true)
  1429. else
  1430. objectoutput^.writereloc(currval-insend,4,nil,relative_false)
  1431. end;
  1432. 56,57,58 :
  1433. begin
  1434. getvalsym(c-56);
  1435. if assigned(currsym) then
  1436. objectoutput^.writereloc(currval,4,currsym,relative_true)
  1437. else
  1438. objectoutput^.writereloc(currval-insend,4,nil,relative_false)
  1439. end;
  1440. 192,193,194 :
  1441. begin
  1442. if NeedAddrPrefix(c-192) then
  1443. begin
  1444. bytes[0]:=$67;
  1445. objectoutput^.writebytes(bytes,1);
  1446. end;
  1447. end;
  1448. 200 :
  1449. begin
  1450. bytes[0]:=$67;
  1451. objectoutput^.writebytes(bytes,1);
  1452. end;
  1453. 208 :
  1454. begin
  1455. bytes[0]:=$66;
  1456. objectoutput^.writebytes(bytes,1);
  1457. end;
  1458. 216 :
  1459. begin
  1460. bytes[0]:=ord(codes^)+condval[condition];
  1461. inc(codes);
  1462. objectoutput^.writebytes(bytes,1);
  1463. end;
  1464. 201,
  1465. 202,
  1466. 209,
  1467. 210,
  1468. 217,218,219 :
  1469. begin
  1470. { these are dissambler hints or 32 bit prefixes which
  1471. are not needed }
  1472. end;
  1473. 31,
  1474. 48,49,50,
  1475. 224,225,226 :
  1476. begin
  1477. InternalError(777006);
  1478. end
  1479. else
  1480. begin
  1481. if (c>=64) and (c<=191) then
  1482. begin
  1483. if (c<127) then
  1484. begin
  1485. if (oper[c and 7].typ=top_reg) then
  1486. rfield:=regval(oper[c and 7].reg)
  1487. else
  1488. rfield:=regval(oper[c and 7].ref^.base);
  1489. end
  1490. else
  1491. rfield:=c and 7;
  1492. opidx:=(c shr 3) and 7;
  1493. if not process_ea(oper[opidx], ea_data, rfield) then
  1494. Message(asmw_e_invalid_effective_address);
  1495. pb:=@bytes;
  1496. pb^:=chr(ea_data.modrm);
  1497. inc(pb);
  1498. if ea_data.sib_present then
  1499. begin
  1500. pb^:=chr(ea_data.sib);
  1501. inc(pb);
  1502. end;
  1503. s:=pb-pchar(@bytes);
  1504. objectoutput^.writebytes(bytes,s);
  1505. case ea_data.bytes of
  1506. 0 : ;
  1507. 1 :
  1508. begin
  1509. if (oper[opidx].ot and OT_MEMORY)=OT_MEMORY then
  1510. objectoutput^.writereloc(oper[opidx].ref^.offset,1,oper[opidx].ref^.symbol,relative_false)
  1511. else
  1512. begin
  1513. bytes[0]:=oper[opidx].ref^.offset;
  1514. objectoutput^.writebytes(bytes,1);
  1515. end;
  1516. inc(s);
  1517. end;
  1518. 2,4 :
  1519. begin
  1520. objectoutput^.writereloc(oper[opidx].ref^.offset,ea_data.bytes,
  1521. oper[opidx].ref^.symbol,relative_false);
  1522. inc(s,ea_data.bytes);
  1523. end;
  1524. end;
  1525. end
  1526. else
  1527. InternalError(777007);
  1528. end;
  1529. end;
  1530. until false;
  1531. end;
  1532. {$endif NOAG386BIN}
  1533. end.
  1534. {
  1535. $Log$
  1536. Revision 1.2 2000-07-13 11:32:38 michael
  1537. + removed logs
  1538. }