i386asm.pas 44 KB

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