cpuasm.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  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 CheckIfValid:boolean;
  102. function Pass1(offset:longint):longint;virtual;
  103. procedure Pass2;virtual;
  104. private
  105. { next fields are filled in pass1, so pass2 is faster }
  106. insentry : PInsEntry;
  107. insoffset,
  108. inssize : longint;
  109. LastInsOffset : longint; { need to be public to be reset }
  110. function InsEnd:longint;
  111. procedure create_ot;
  112. function Matches(p:PInsEntry):longint;
  113. function calcsize(p:PInsEntry):longint;
  114. procedure gencode;
  115. function NeedAddrPrefix(opidx:byte):boolean;
  116. {$endif NOAG386BIN}
  117. end;
  118. implementation
  119. uses
  120. cutils,
  121. ogbase;
  122. {*****************************************************************************
  123. TaiRegAlloc
  124. *****************************************************************************}
  125. constructor tairegalloc.alloc(r : tregister);
  126. begin
  127. inherited create;
  128. typ:=ait_regalloc;
  129. allocation:=true;
  130. reg:=r;
  131. end;
  132. constructor tairegalloc.dealloc(r : tregister);
  133. begin
  134. inherited create;
  135. typ:=ait_regalloc;
  136. allocation:=false;
  137. reg:=r;
  138. end;
  139. {****************************************************************************
  140. TAI_ALIGN
  141. ****************************************************************************}
  142. constructor tai_align.create(b: byte);
  143. begin
  144. inherited create(b);
  145. reg := R_ECX;
  146. end;
  147. constructor tai_align.create_op(b: byte; _op: byte);
  148. begin
  149. inherited create_op(b,_op);
  150. reg := R_NO;
  151. end;
  152. function tai_align.getfillbuf:pchar;
  153. const
  154. alignarray:array[0..5] of string[8]=(
  155. #$8D#$B4#$26#$00#$00#$00#$00,
  156. #$8D#$B6#$00#$00#$00#$00,
  157. #$8D#$74#$26#$00,
  158. #$8D#$76#$00,
  159. #$89#$F6,
  160. #$90
  161. );
  162. var
  163. bufptr : pchar;
  164. j : longint;
  165. begin
  166. if not use_op then
  167. begin
  168. bufptr:=@buf;
  169. while (fillsize>0) do
  170. begin
  171. for j:=0 to 5 do
  172. if (fillsize>=length(alignarray[j])) then
  173. break;
  174. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  175. inc(bufptr,length(alignarray[j]));
  176. dec(fillsize,length(alignarray[j]));
  177. end;
  178. end;
  179. getfillbuf:=pchar(@buf);
  180. end;
  181. {*****************************************************************************
  182. Taicpu Constructors
  183. *****************************************************************************}
  184. procedure taicpu.loadconst(opidx:longint;l:longint);
  185. begin
  186. if opidx>=ops then
  187. ops:=opidx+1;
  188. with oper[opidx] do
  189. begin
  190. if typ=top_ref then
  191. disposereference(ref);
  192. val:=l;
  193. typ:=top_const;
  194. end;
  195. end;
  196. procedure taicpu.loadsymbol(opidx:longint;s:pasmsymbol;sofs:longint);
  197. begin
  198. if opidx>=ops then
  199. ops:=opidx+1;
  200. with oper[opidx] do
  201. begin
  202. if typ=top_ref then
  203. disposereference(ref);
  204. sym:=s;
  205. symofs:=sofs;
  206. typ:=top_symbol;
  207. end;
  208. { Mark the symbol as used }
  209. if assigned(s) then
  210. inc(s^.refs);
  211. end;
  212. procedure taicpu.loadref(opidx:longint;p:preference);
  213. begin
  214. if opidx>=ops then
  215. ops:=opidx+1;
  216. with oper[opidx] do
  217. begin
  218. if typ=top_ref then
  219. disposereference(ref);
  220. if p^.is_immediate then
  221. begin
  222. {$ifdef REF_IMMEDIATE_WARN}
  223. Comment(V_Warning,'Reference immediate');
  224. {$endif}
  225. val:=p^.offset;
  226. disposereference(p);
  227. typ:=top_const;
  228. end
  229. else
  230. begin
  231. ref:=p;
  232. if not(ref^.segment in [R_DS,R_NO]) then
  233. segprefix:=ref^.segment;
  234. typ:=top_ref;
  235. { mark symbol as used }
  236. if assigned(ref^.symbol) then
  237. inc(ref^.symbol^.refs);
  238. end;
  239. end;
  240. end;
  241. procedure taicpu.loadreg(opidx:longint;r:tregister);
  242. begin
  243. if opidx>=ops then
  244. ops:=opidx+1;
  245. with oper[opidx] do
  246. begin
  247. if typ=top_ref then
  248. disposereference(ref);
  249. reg:=r;
  250. typ:=top_reg;
  251. end;
  252. end;
  253. procedure taicpu.loadoper(opidx:longint;o:toper);
  254. begin
  255. if opidx>=ops then
  256. ops:=opidx+1;
  257. if oper[opidx].typ=top_ref then
  258. disposereference(oper[opidx].ref);
  259. oper[opidx]:=o;
  260. { copy also the reference }
  261. if oper[opidx].typ=top_ref then
  262. oper[opidx].ref:=newreference(o.ref^);
  263. end;
  264. procedure taicpu.changeopsize(siz:topsize);
  265. begin
  266. opsize:=siz;
  267. end;
  268. procedure taicpu.init(op : tasmop;_size : topsize);
  269. begin
  270. typ:=ait_instruction;
  271. is_jmp:=false;
  272. segprefix:=R_NO;
  273. opcode:=op;
  274. opsize:=_size;
  275. ops:=0;
  276. condition:=c_none;
  277. fillchar(oper,sizeof(oper),0);
  278. {$ifndef NOAG386BIN}
  279. insentry:=nil;
  280. LastInsOffset:=-1;
  281. InsOffset:=0;
  282. InsSize:=0;
  283. {$endif}
  284. end;
  285. constructor taicpu.op_none(op : tasmop;_size : topsize);
  286. begin
  287. inherited create;
  288. init(op,_size);
  289. end;
  290. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  291. begin
  292. inherited create;
  293. init(op,_size);
  294. ops:=1;
  295. loadreg(0,_op1);
  296. end;
  297. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : longint);
  298. begin
  299. inherited create;
  300. init(op,_size);
  301. ops:=1;
  302. loadconst(0,_op1);
  303. end;
  304. constructor taicpu.op_ref(op : tasmop;_size : topsize;_op1 : preference);
  305. begin
  306. inherited create;
  307. init(op,_size);
  308. ops:=1;
  309. loadref(0,_op1);
  310. end;
  311. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  312. begin
  313. inherited create;
  314. init(op,_size);
  315. ops:=2;
  316. loadreg(0,_op1);
  317. loadreg(1,_op2);
  318. end;
  319. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: longint);
  320. begin
  321. inherited create;
  322. init(op,_size);
  323. ops:=2;
  324. loadreg(0,_op1);
  325. loadconst(1,_op2);
  326. end;
  327. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;_op2 : preference);
  328. begin
  329. inherited create;
  330. init(op,_size);
  331. ops:=2;
  332. loadreg(0,_op1);
  333. loadref(1,_op2);
  334. end;
  335. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister);
  336. begin
  337. inherited create;
  338. init(op,_size);
  339. ops:=2;
  340. loadconst(0,_op1);
  341. loadreg(1,_op2);
  342. end;
  343. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : longint);
  344. begin
  345. inherited create;
  346. init(op,_size);
  347. ops:=2;
  348. loadconst(0,_op1);
  349. loadconst(1,_op2);
  350. end;
  351. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : longint;_op2 : preference);
  352. begin
  353. inherited create;
  354. init(op,_size);
  355. ops:=2;
  356. loadconst(0,_op1);
  357. loadref(1,_op2);
  358. end;
  359. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;_op1 : preference;_op2 : tregister);
  360. begin
  361. inherited create;
  362. init(op,_size);
  363. ops:=2;
  364. loadref(0,_op1);
  365. loadreg(1,_op2);
  366. end;
  367. constructor taicpu.op_ref_ref(op : tasmop;_size : topsize;_op1,_op2 : preference);
  368. begin
  369. inherited create;
  370. init(op,_size);
  371. ops:=2;
  372. loadref(0,_op1);
  373. loadref(1,_op2);
  374. end;
  375. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  376. begin
  377. inherited create;
  378. init(op,_size);
  379. ops:=3;
  380. loadreg(0,_op1);
  381. loadreg(1,_op2);
  382. loadreg(2,_op3);
  383. end;
  384. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister;_op3 : tregister);
  385. begin
  386. inherited create;
  387. init(op,_size);
  388. ops:=3;
  389. loadconst(0,_op1);
  390. loadreg(1,_op2);
  391. loadreg(2,_op3);
  392. end;
  393. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;_op3 : preference);
  394. begin
  395. inherited create;
  396. init(op,_size);
  397. ops:=3;
  398. loadreg(0,_op1);
  399. loadreg(1,_op2);
  400. loadref(2,_op3);
  401. end;
  402. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : preference;_op3 : tregister);
  403. begin
  404. inherited create;
  405. init(op,_size);
  406. ops:=3;
  407. loadconst(0,_op1);
  408. loadref(1,_op2);
  409. loadreg(2,_op3);
  410. end;
  411. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister;_op3 : preference);
  412. begin
  413. inherited create;
  414. init(op,_size);
  415. ops:=3;
  416. loadconst(0,_op1);
  417. loadreg(1,_op2);
  418. loadref(2,_op3);
  419. end;
  420. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : pasmsymbol);
  421. begin
  422. inherited create;
  423. init(op,_size);
  424. condition:=cond;
  425. ops:=1;
  426. loadsymbol(0,_op1,0);
  427. end;
  428. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : pasmsymbol);
  429. begin
  430. inherited create;
  431. init(op,_size);
  432. ops:=1;
  433. loadsymbol(0,_op1,0);
  434. end;
  435. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : pasmsymbol;_op1ofs:longint);
  436. begin
  437. inherited create;
  438. init(op,_size);
  439. ops:=1;
  440. loadsymbol(0,_op1,_op1ofs);
  441. end;
  442. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : pasmsymbol;_op1ofs:longint;_op2 : tregister);
  443. begin
  444. inherited create;
  445. init(op,_size);
  446. ops:=2;
  447. loadsymbol(0,_op1,_op1ofs);
  448. loadreg(1,_op2);
  449. end;
  450. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : pasmsymbol;_op1ofs:longint;_op2 : preference);
  451. begin
  452. inherited create;
  453. init(op,_size);
  454. ops:=2;
  455. loadsymbol(0,_op1,_op1ofs);
  456. loadref(1,_op2);
  457. end;
  458. destructor taicpu.destroy;
  459. begin
  460. { unrolled for speed }
  461. if (ops>0) then
  462. begin
  463. case oper[0].typ of
  464. top_ref:
  465. dispose(oper[0].ref);
  466. top_symbol:
  467. dec(Pasmsymbol(oper[0].sym)^.refs);
  468. end;
  469. if (ops>1) then
  470. begin
  471. if (oper[1].typ=top_ref) then
  472. dispose(oper[1].ref);
  473. if (ops>2) and (oper[2].typ=top_ref) then
  474. dispose(oper[2].ref);
  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.CheckIfValid:boolean;
  830. var
  831. m,i : longint;
  832. begin
  833. CheckIfValid:=false;
  834. { Things which may only be done once, not when a second pass is done to
  835. optimize }
  836. if Insentry=nil then
  837. begin
  838. { We need intel style operands }
  839. SwapOperands;
  840. { create the .ot fields }
  841. create_ot;
  842. { set the file postion }
  843. aktfilepos:=fileinfo;
  844. end
  845. else
  846. begin
  847. { we've already an insentry so it's valid }
  848. CheckIfValid:=true;
  849. exit;
  850. end;
  851. { Lookup opcode in the table }
  852. InsSize:=-1;
  853. i:=instabcache^[opcode];
  854. if i=-1 then
  855. begin
  856. {$ifdef TP}
  857. Message1(asmw_e_opcode_not_in_table,'');
  858. {$else}
  859. Message1(asmw_e_opcode_not_in_table,att_op2str[opcode]);
  860. {$endif}
  861. exit;
  862. end;
  863. insentry:=@instab[i];
  864. while (insentry^.opcode=opcode) do
  865. begin
  866. m:=matches(insentry);
  867. if m=100 then
  868. begin
  869. InsSize:=calcsize(insentry);
  870. if (segprefix<>R_NO) then
  871. inc(InsSize);
  872. CheckIfValid:=true;
  873. exit;
  874. end;
  875. inc(i);
  876. insentry:=@instab[i];
  877. end;
  878. if insentry^.opcode<>opcode then
  879. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  880. { No instruction found, set insentry to nil and inssize to -1 }
  881. insentry:=nil;
  882. inssize:=-1;
  883. end;
  884. function taicpu.Pass1(offset:longint):longint;
  885. begin
  886. Pass1:=0;
  887. { Save the old offset and set the new offset }
  888. InsOffset:=Offset;
  889. { Things which may only be done once, not when a second pass is done to
  890. optimize }
  891. if Insentry=nil then
  892. begin
  893. { Check if error last time then InsSize=-1 }
  894. if InsSize=-1 then
  895. exit;
  896. { set the file postion }
  897. aktfilepos:=fileinfo;
  898. end
  899. else
  900. begin
  901. {$ifdef PASS2FLAG}
  902. { we are here in a second pass, check if the instruction can be optimized }
  903. if (InsEntry^.flags and IF_PASS2)=0 then
  904. begin
  905. Pass1:=InsSize;
  906. exit;
  907. end;
  908. { update the .ot fields, some top_const can be updated }
  909. create_ot;
  910. {$endif}
  911. end;
  912. { Check if it's a valid instruction }
  913. if CheckIfValid then
  914. begin
  915. LastInsOffset:=InsOffset;
  916. Pass1:=InsSize;
  917. exit;
  918. end;
  919. LastInsOffset:=-1;
  920. end;
  921. procedure taicpu.Pass2;
  922. var
  923. c : longint;
  924. begin
  925. { error in pass1 ? }
  926. if insentry=nil then
  927. exit;
  928. aktfilepos:=fileinfo;
  929. { Segment override }
  930. if (segprefix<>R_NO) then
  931. begin
  932. case segprefix of
  933. R_CS : c:=$2e;
  934. R_DS : c:=$3e;
  935. R_ES : c:=$26;
  936. R_FS : c:=$64;
  937. R_GS : c:=$65;
  938. R_SS : c:=$36;
  939. end;
  940. objectdata.writebytes(c,1);
  941. { fix the offset for GenNode }
  942. inc(InsOffset);
  943. end;
  944. { Generate the instruction }
  945. GenCode;
  946. end;
  947. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  948. var
  949. i,b : tregister;
  950. begin
  951. if (OT_MEMORY and (not oper[opidx].ot))=0 then
  952. begin
  953. i:=oper[opidx].ref^.index;
  954. b:=oper[opidx].ref^.base;
  955. if not(i in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) or
  956. not(b in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) then
  957. begin
  958. NeedAddrPrefix:=true;
  959. exit;
  960. end;
  961. end;
  962. NeedAddrPrefix:=false;
  963. end;
  964. function regval(r:tregister):byte;
  965. begin
  966. case r of
  967. R_EAX,R_AX,R_AL,R_ES,R_CR0,R_DR0,R_ST,R_ST0,R_MM0,R_XMM0 :
  968. regval:=0;
  969. R_ECX,R_CX,R_CL,R_CS,R_DR1,R_ST1,R_MM1,R_XMM1 :
  970. regval:=1;
  971. R_EDX,R_DX,R_DL,R_SS,R_CR2,R_DR2,R_ST2,R_MM2,R_XMM2 :
  972. regval:=2;
  973. R_EBX,R_BX,R_BL,R_DS,R_CR3,R_DR3,R_TR3,R_ST3,R_MM3,R_XMM3 :
  974. regval:=3;
  975. R_ESP,R_SP,R_AH,R_FS,R_CR4,R_TR4,R_ST4,R_MM4,R_XMM4 :
  976. regval:=4;
  977. R_EBP,R_BP,R_CH,R_GS,R_TR5,R_ST5,R_MM5,R_XMM5 :
  978. regval:=5;
  979. R_ESI,R_SI,R_DH,R_DR6,R_TR6,R_ST6,R_MM6,R_XMM6 :
  980. regval:=6;
  981. R_EDI,R_DI,R_BH,R_DR7,R_TR7,R_ST7,R_MM7,R_XMM7 :
  982. regval:=7;
  983. else
  984. begin
  985. internalerror(777001);
  986. regval:=0;
  987. end;
  988. end;
  989. end;
  990. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  991. const
  992. regs : array[0..63] of tregister=(
  993. R_MM0, R_EAX, R_AX, R_AL, R_XMM0, R_NO, R_NO, R_NO,
  994. R_MM1, R_ECX, R_CX, R_CL, R_XMM1, R_NO, R_NO, R_NO,
  995. R_MM2, R_EDX, R_DX, R_DL, R_XMM2, R_NO, R_NO, R_NO,
  996. R_MM3, R_EBX, R_BX, R_BL, R_XMM3, R_NO, R_NO, R_NO,
  997. R_MM4, R_ESP, R_SP, R_AH, R_XMM4, R_NO, R_NO, R_NO,
  998. R_MM5, R_EBP, R_BP, R_CH, R_XMM5, R_NO, R_NO, R_NO,
  999. R_MM6, R_ESI, R_SI, R_DH, R_XMM6, R_NO, R_NO, R_NO,
  1000. R_MM7, R_EDI, R_DI, R_BH, R_XMM7, R_NO, R_NO, R_NO
  1001. );
  1002. var
  1003. j : longint;
  1004. i,b : tregister;
  1005. sym : pasmsymbol;
  1006. md,s : byte;
  1007. base,index,scalefactor,
  1008. o : longint;
  1009. begin
  1010. process_ea:=false;
  1011. { register ? }
  1012. if (input.typ=top_reg) then
  1013. begin
  1014. j:=0;
  1015. while (j<=high(regs)) do
  1016. begin
  1017. if input.reg=regs[j] then
  1018. break;
  1019. inc(j);
  1020. end;
  1021. if j<=high(regs) then
  1022. begin
  1023. output.sib_present:=false;
  1024. output.bytes:=0;
  1025. output.modrm:=$c0 or (rfield shl 3) or (j shr 3);
  1026. output.size:=1;
  1027. process_ea:=true;
  1028. end;
  1029. exit;
  1030. end;
  1031. { memory reference }
  1032. i:=input.ref^.index;
  1033. b:=input.ref^.base;
  1034. s:=input.ref^.scalefactor;
  1035. o:=input.ref^.offset;
  1036. sym:=input.ref^.symbol;
  1037. { it's direct address }
  1038. if (b=R_NO) and (i=R_NO) then
  1039. begin
  1040. { it's a pure offset }
  1041. output.sib_present:=false;
  1042. output.bytes:=4;
  1043. output.modrm:=5 or (rfield shl 3);
  1044. end
  1045. else
  1046. { it's an indirection }
  1047. begin
  1048. { 16 bit address? }
  1049. if not((i in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) and
  1050. (b in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI])) then
  1051. Message(asmw_e_16bit_not_supported);
  1052. {$ifdef OPTEA}
  1053. { make single reg base }
  1054. if (b=R_NO) and (s=1) then
  1055. begin
  1056. b:=i;
  1057. i:=R_NO;
  1058. end;
  1059. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1060. if (b=R_NO) and
  1061. (((s=2) and (i<>R_ESP)) or
  1062. (s=3) or (s=5) or (s=9)) then
  1063. begin
  1064. b:=i;
  1065. dec(s);
  1066. end;
  1067. { swap ESP into base if scalefactor is 1 }
  1068. if (s=1) and (i=R_ESP) then
  1069. begin
  1070. i:=b;
  1071. b:=R_ESP;
  1072. end;
  1073. {$endif}
  1074. { wrong, for various reasons }
  1075. if (i=R_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (i<>R_NO)) then
  1076. exit;
  1077. { base }
  1078. case b of
  1079. R_EAX : base:=0;
  1080. R_ECX : base:=1;
  1081. R_EDX : base:=2;
  1082. R_EBX : base:=3;
  1083. R_ESP : base:=4;
  1084. R_NO,
  1085. R_EBP : base:=5;
  1086. R_ESI : base:=6;
  1087. R_EDI : base:=7;
  1088. else
  1089. exit;
  1090. end;
  1091. { index }
  1092. case i of
  1093. R_EAX : index:=0;
  1094. R_ECX : index:=1;
  1095. R_EDX : index:=2;
  1096. R_EBX : index:=3;
  1097. R_NO : index:=4;
  1098. R_EBP : index:=5;
  1099. R_ESI : index:=6;
  1100. R_EDI : index:=7;
  1101. else
  1102. exit;
  1103. end;
  1104. case s of
  1105. 0,
  1106. 1 : scalefactor:=0;
  1107. 2 : scalefactor:=1;
  1108. 4 : scalefactor:=2;
  1109. 8 : scalefactor:=3;
  1110. else
  1111. exit;
  1112. end;
  1113. if (b=R_NO) or
  1114. ((b<>R_EBP) and (o=0) and (sym=nil)) then
  1115. md:=0
  1116. else
  1117. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1118. md:=1
  1119. else
  1120. md:=2;
  1121. if (b=R_NO) or (md=2) then
  1122. output.bytes:=4
  1123. else
  1124. output.bytes:=md;
  1125. { SIB needed ? }
  1126. if (i=R_NO) and (b<>R_ESP) then
  1127. begin
  1128. output.sib_present:=false;
  1129. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1130. end
  1131. else
  1132. begin
  1133. output.sib_present:=true;
  1134. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1135. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1136. end;
  1137. end;
  1138. if output.sib_present then
  1139. output.size:=2+output.bytes
  1140. else
  1141. output.size:=1+output.bytes;
  1142. process_ea:=true;
  1143. end;
  1144. function taicpu.calcsize(p:PInsEntry):longint;
  1145. var
  1146. codes : pchar;
  1147. c : byte;
  1148. len : longint;
  1149. ea_data : ea;
  1150. begin
  1151. len:=0;
  1152. codes:=@p^.code;
  1153. repeat
  1154. c:=ord(codes^);
  1155. inc(codes);
  1156. case c of
  1157. 0 :
  1158. break;
  1159. 1,2,3 :
  1160. begin
  1161. inc(codes,c);
  1162. inc(len,c);
  1163. end;
  1164. 8,9,10 :
  1165. begin
  1166. inc(codes);
  1167. inc(len);
  1168. end;
  1169. 4,5,6,7 :
  1170. begin
  1171. if opsize=S_W then
  1172. inc(len,2)
  1173. else
  1174. inc(len);
  1175. end;
  1176. 15,
  1177. 12,13,14,
  1178. 16,17,18,
  1179. 20,21,22,
  1180. 40,41,42 :
  1181. inc(len);
  1182. 24,25,26,
  1183. 31,
  1184. 48,49,50 :
  1185. inc(len,2);
  1186. 28,29,30, { we don't have 16 bit immediates code }
  1187. 32,33,34,
  1188. 52,53,54,
  1189. 56,57,58 :
  1190. inc(len,4);
  1191. 192,193,194 :
  1192. if NeedAddrPrefix(c-192) then
  1193. inc(len);
  1194. 208 :
  1195. inc(len);
  1196. 200,
  1197. 201,
  1198. 202,
  1199. 209,
  1200. 210,
  1201. 217,218,219 : ;
  1202. 216 :
  1203. begin
  1204. inc(codes);
  1205. inc(len);
  1206. end;
  1207. 224,225,226 :
  1208. begin
  1209. InternalError(777002);
  1210. end;
  1211. else
  1212. begin
  1213. if (c>=64) and (c<=191) then
  1214. begin
  1215. if not process_ea(oper[(c shr 3) and 7], ea_data, 0) then
  1216. Message(asmw_e_invalid_effective_address)
  1217. else
  1218. inc(len,ea_data.size);
  1219. end
  1220. else
  1221. InternalError(777003);
  1222. end;
  1223. end;
  1224. until false;
  1225. calcsize:=len;
  1226. end;
  1227. procedure taicpu.GenCode;
  1228. {
  1229. * the actual codes (C syntax, i.e. octal):
  1230. * \0 - terminates the code. (Unless it's a literal of course.)
  1231. * \1, \2, \3 - that many literal bytes follow in the code stream
  1232. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1233. * (POP is never used for CS) depending on operand 0
  1234. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1235. * on operand 0
  1236. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1237. * to the register value of operand 0, 1 or 2
  1238. * \17 - encodes the literal byte 0. (Some compilers don't take
  1239. * kindly to a zero byte in the _middle_ of a compile time
  1240. * string constant, so I had to put this hack in.)
  1241. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1242. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1243. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1244. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1245. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1246. * assembly mode or the address-size override on the operand
  1247. * \37 - a word constant, from the _segment_ part of operand 0
  1248. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1249. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1250. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1251. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1252. * assembly mode or the address-size override on the operand
  1253. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1254. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1255. * field the register value of operand b.
  1256. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1257. * field equal to digit b.
  1258. * \30x - might be an 0x67 byte, depending on the address size of
  1259. * the memory reference in operand x.
  1260. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1261. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1262. * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1263. * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1264. * \322 - indicates that this instruction is only valid when the
  1265. * operand size is the default (instruction to disassembler,
  1266. * generates no code in the assembler)
  1267. * \330 - a literal byte follows in the code stream, to be added
  1268. * to the condition code value of the instruction.
  1269. * \340 - reserve <operand 0> bytes of uninitialised storage.
  1270. * Operand 0 had better be a segmentless constant.
  1271. }
  1272. var
  1273. currval : longint;
  1274. currsym : pasmsymbol;
  1275. procedure getvalsym(opidx:longint);
  1276. begin
  1277. case oper[opidx].typ of
  1278. top_ref :
  1279. begin
  1280. currval:=oper[opidx].ref^.offset;
  1281. currsym:=oper[opidx].ref^.symbol;
  1282. end;
  1283. top_const :
  1284. begin
  1285. currval:=oper[opidx].val;
  1286. currsym:=nil;
  1287. end;
  1288. top_symbol :
  1289. begin
  1290. currval:=oper[opidx].symofs;
  1291. currsym:=oper[opidx].sym;
  1292. end;
  1293. else
  1294. Message(asmw_e_immediate_or_reference_expected);
  1295. end;
  1296. end;
  1297. const
  1298. CondVal:array[TAsmCond] of byte=($0,
  1299. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1300. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1301. $0, $A, $A, $B, $8, $4);
  1302. var
  1303. c : byte;
  1304. pb,
  1305. codes : pchar;
  1306. bytes : array[0..3] of byte;
  1307. rfield,
  1308. data,s,opidx : longint;
  1309. ea_data : ea;
  1310. begin
  1311. codes:=insentry^.code;
  1312. { Force word push/pop for registers }
  1313. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1314. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1315. begin
  1316. bytes[0]:=$66;
  1317. objectdata.writebytes(bytes,1);
  1318. end;
  1319. repeat
  1320. c:=ord(codes^);
  1321. inc(codes);
  1322. case c of
  1323. 0 :
  1324. break;
  1325. 1,2,3 :
  1326. begin
  1327. objectdata.writebytes(codes^,c);
  1328. inc(codes,c);
  1329. end;
  1330. 4,6 :
  1331. begin
  1332. case oper[0].reg of
  1333. R_CS :
  1334. begin
  1335. if c=4 then
  1336. bytes[0]:=$f
  1337. else
  1338. bytes[0]:=$e;
  1339. end;
  1340. R_NO,
  1341. R_DS :
  1342. begin
  1343. if c=4 then
  1344. bytes[0]:=$1f
  1345. else
  1346. bytes[0]:=$1e;
  1347. end;
  1348. R_ES :
  1349. begin
  1350. if c=4 then
  1351. bytes[0]:=$7
  1352. else
  1353. bytes[0]:=$6;
  1354. end;
  1355. R_SS :
  1356. begin
  1357. if c=4 then
  1358. bytes[0]:=$17
  1359. else
  1360. bytes[0]:=$16;
  1361. end;
  1362. else
  1363. InternalError(777004);
  1364. end;
  1365. objectdata.writebytes(bytes,1);
  1366. end;
  1367. 5,7 :
  1368. begin
  1369. case oper[0].reg of
  1370. R_FS :
  1371. begin
  1372. if c=5 then
  1373. bytes[0]:=$a1
  1374. else
  1375. bytes[0]:=$a0;
  1376. end;
  1377. R_GS :
  1378. begin
  1379. if c=5 then
  1380. bytes[0]:=$a9
  1381. else
  1382. bytes[0]:=$a8;
  1383. end;
  1384. else
  1385. InternalError(777005);
  1386. end;
  1387. objectdata.writebytes(bytes,1);
  1388. end;
  1389. 8,9,10 :
  1390. begin
  1391. bytes[0]:=ord(codes^)+regval(oper[c-8].reg);
  1392. inc(codes);
  1393. objectdata.writebytes(bytes,1);
  1394. end;
  1395. 15 :
  1396. begin
  1397. bytes[0]:=0;
  1398. objectdata.writebytes(bytes,1);
  1399. end;
  1400. 12,13,14 :
  1401. begin
  1402. getvalsym(c-12);
  1403. if (currval<-128) or (currval>127) then
  1404. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1405. if assigned(currsym) then
  1406. objectdata.writereloc(currval,1,currsym,relative_false)
  1407. else
  1408. objectdata.writebytes(currval,1);
  1409. end;
  1410. 16,17,18 :
  1411. begin
  1412. getvalsym(c-16);
  1413. if (currval<-256) or (currval>255) then
  1414. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1415. if assigned(currsym) then
  1416. objectdata.writereloc(currval,1,currsym,relative_false)
  1417. else
  1418. objectdata.writebytes(currval,1);
  1419. end;
  1420. 20,21,22 :
  1421. begin
  1422. getvalsym(c-20);
  1423. if (currval<0) or (currval>255) then
  1424. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1425. if assigned(currsym) then
  1426. objectdata.writereloc(currval,1,currsym,relative_false)
  1427. else
  1428. objectdata.writebytes(currval,1);
  1429. end;
  1430. 24,25,26 :
  1431. begin
  1432. getvalsym(c-24);
  1433. if (currval<-65536) or (currval>65535) then
  1434. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1435. if assigned(currsym) then
  1436. objectdata.writereloc(currval,2,currsym,relative_false)
  1437. else
  1438. objectdata.writebytes(currval,2);
  1439. end;
  1440. 28,29,30 :
  1441. begin
  1442. getvalsym(c-28);
  1443. if assigned(currsym) then
  1444. objectdata.writereloc(currval,4,currsym,relative_false)
  1445. else
  1446. objectdata.writebytes(currval,4);
  1447. end;
  1448. 32,33,34 :
  1449. begin
  1450. getvalsym(c-32);
  1451. if assigned(currsym) then
  1452. objectdata.writereloc(currval,4,currsym,relative_false)
  1453. else
  1454. objectdata.writebytes(currval,4);
  1455. end;
  1456. 40,41,42 :
  1457. begin
  1458. getvalsym(c-40);
  1459. data:=currval-insend;
  1460. if assigned(currsym) then
  1461. inc(data,currsym^.address);
  1462. if (data>127) or (data<-128) then
  1463. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1464. objectdata.writebytes(data,1);
  1465. end;
  1466. 52,53,54 :
  1467. begin
  1468. getvalsym(c-52);
  1469. if assigned(currsym) then
  1470. objectdata.writereloc(currval,4,currsym,relative_true)
  1471. else
  1472. objectdata.writereloc(currval-insend,4,nil,relative_false)
  1473. end;
  1474. 56,57,58 :
  1475. begin
  1476. getvalsym(c-56);
  1477. if assigned(currsym) then
  1478. objectdata.writereloc(currval,4,currsym,relative_true)
  1479. else
  1480. objectdata.writereloc(currval-insend,4,nil,relative_false)
  1481. end;
  1482. 192,193,194 :
  1483. begin
  1484. if NeedAddrPrefix(c-192) then
  1485. begin
  1486. bytes[0]:=$67;
  1487. objectdata.writebytes(bytes,1);
  1488. end;
  1489. end;
  1490. 200 :
  1491. begin
  1492. bytes[0]:=$67;
  1493. objectdata.writebytes(bytes,1);
  1494. end;
  1495. 208 :
  1496. begin
  1497. bytes[0]:=$66;
  1498. objectdata.writebytes(bytes,1);
  1499. end;
  1500. 216 :
  1501. begin
  1502. bytes[0]:=ord(codes^)+condval[condition];
  1503. inc(codes);
  1504. objectdata.writebytes(bytes,1);
  1505. end;
  1506. 201,
  1507. 202,
  1508. 209,
  1509. 210,
  1510. 217,218,219 :
  1511. begin
  1512. { these are dissambler hints or 32 bit prefixes which
  1513. are not needed }
  1514. end;
  1515. 31,
  1516. 48,49,50,
  1517. 224,225,226 :
  1518. begin
  1519. InternalError(777006);
  1520. end
  1521. else
  1522. begin
  1523. if (c>=64) and (c<=191) then
  1524. begin
  1525. if (c<127) then
  1526. begin
  1527. if (oper[c and 7].typ=top_reg) then
  1528. rfield:=regval(oper[c and 7].reg)
  1529. else
  1530. rfield:=regval(oper[c and 7].ref^.base);
  1531. end
  1532. else
  1533. rfield:=c and 7;
  1534. opidx:=(c shr 3) and 7;
  1535. if not process_ea(oper[opidx], ea_data, rfield) then
  1536. Message(asmw_e_invalid_effective_address);
  1537. pb:=@bytes;
  1538. pb^:=chr(ea_data.modrm);
  1539. inc(pb);
  1540. if ea_data.sib_present then
  1541. begin
  1542. pb^:=chr(ea_data.sib);
  1543. inc(pb);
  1544. end;
  1545. s:=pb-pchar(@bytes);
  1546. objectdata.writebytes(bytes,s);
  1547. case ea_data.bytes of
  1548. 0 : ;
  1549. 1 :
  1550. begin
  1551. if (oper[opidx].ot and OT_MEMORY)=OT_MEMORY then
  1552. objectdata.writereloc(oper[opidx].ref^.offset,1,oper[opidx].ref^.symbol,relative_false)
  1553. else
  1554. begin
  1555. bytes[0]:=oper[opidx].ref^.offset;
  1556. objectdata.writebytes(bytes,1);
  1557. end;
  1558. inc(s);
  1559. end;
  1560. 2,4 :
  1561. begin
  1562. objectdata.writereloc(oper[opidx].ref^.offset,ea_data.bytes,
  1563. oper[opidx].ref^.symbol,relative_false);
  1564. inc(s,ea_data.bytes);
  1565. end;
  1566. end;
  1567. end
  1568. else
  1569. InternalError(777007);
  1570. end;
  1571. end;
  1572. until false;
  1573. end;
  1574. {$endif NOAG386BIN}
  1575. end.
  1576. {
  1577. $Log$
  1578. Revision 1.9 2001-01-12 19:18:42 peter
  1579. * check for valid asm instructions
  1580. Revision 1.8 2001/01/07 15:48:56 jonas
  1581. * references to symbols were only decreased in taicpu.done for jmps, fixed
  1582. Revision 1.7 2000/12/26 15:56:17 peter
  1583. * unrolled loops in taicpu.destroy
  1584. Revision 1.6 2000/12/25 00:07:31 peter
  1585. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1586. tlinkedlist objects)
  1587. Revision 1.5 2000/12/23 19:59:35 peter
  1588. * object to class for ow/og objects
  1589. * split objectdata from objectoutput
  1590. Revision 1.4 2000/12/07 17:19:45 jonas
  1591. * new constant handling: from now on, hex constants >$7fffffff are
  1592. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1593. and became $ffffffff80000000), all constants in the longint range
  1594. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1595. are cardinals and the rest are int64's.
  1596. * added lots of longint typecast to prevent range check errors in the
  1597. compiler and rtl
  1598. * type casts of symbolic ordinal constants are now preserved
  1599. * fixed bug where the original resulttype wasn't restored correctly
  1600. after doing a 64bit rangecheck
  1601. Revision 1.3 2000/11/12 22:20:37 peter
  1602. * create generic toutputsection for binary writers
  1603. Revision 1.2 2000/10/15 10:50:46 florian
  1604. * fixed xmm register access
  1605. Revision 1.1 2000/10/15 09:39:37 peter
  1606. * moved cpu*.pas to i386/
  1607. * renamed n386 to common cpunode
  1608. Revision 1.5 2000/09/24 15:06:14 peter
  1609. * use defines.inc
  1610. Revision 1.4 2000/08/27 16:11:50 peter
  1611. * moved some util functions from globals,cobjects to cutils
  1612. * splitted files into finput,fmodule
  1613. Revision 1.3 2000/07/13 12:08:25 michael
  1614. + patched to 1.1.0 with former 1.09patch from peter
  1615. Revision 1.2 2000/07/13 11:32:38 michael
  1616. + removed logs
  1617. }