cpuasm.pas 47 KB

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