cpuasm.pas 47 KB

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