cpuasm.pas 43 KB

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