cpuasm.pas 43 KB

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