aasmcpu.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. {******************************************************************************
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl and Peter Vreman
  4. * This code was inspired by the NASM sources
  5. The Netwide Assembler is copyright (C) 1996 Simon Tatham and
  6. Julian Hall. All rights reserved.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************}
  19. unit aasmcpu;
  20. {$INCLUDE fpcdefs.inc}
  21. interface
  22. uses
  23. cclasses,globals,verbose,
  24. cpuinfo,cpubase,
  25. symppu,
  26. aasmbase,aasmtai;
  27. const
  28. MaxPrefixes=4;
  29. type
  30. TOperandOrder=(op_intel,op_att);
  31. { alignment for operator }
  32. tai_align=class(tai_align_abstract)
  33. reg:tregister;
  34. constructor create(b:byte);
  35. constructor create_op(b:byte; _op:byte);
  36. function getfillbuf:pchar;override;
  37. end;
  38. taicpu = class(taicpu_abstract)
  39. opsize:topsize;
  40. constructor op_none(op:tasmop;_size:topsize);
  41. constructor op_reg(op:tasmop;_size:topsize;_op1:tregister);
  42. constructor op_const(op:tasmop;_size:topsize;_op1:aword);
  43. constructor op_ref(op:tasmop;_size:topsize;const _op1:treference);
  44. constructor op_reg_reg(op:tasmop;_size:topsize;_op1,_op2:tregister);
  45. constructor op_reg_ref(op:tasmop;_size:topsize;_op1:tregister;const _op2:treference);
  46. constructor op_reg_const(op:tasmop; _size:topsize; _op1:tregister; _op2:aword);
  47. constructor op_const_reg(op:tasmop;_size:topsize;_op1:aword;_op2:tregister);
  48. constructor op_const_const(op:tasmop;_size:topsize;_op1,_op2:aword);
  49. constructor op_const_ref(op:tasmop;_size:topsize;_op1:aword;const _op2:treference);
  50. constructor op_ref_reg(op:tasmop;_size:topsize;const _op1:treference;_op2:tregister);
  51. { this is only allowed if _op1 is an int value (_op1^.isintvalue=true) }
  52. constructor op_ref_ref(op:tasmop;_size:topsize;const _op1,_op2:treference);
  53. constructor op_reg_reg_reg(op:tasmop;_size:topsize;_op1,_op2,_op3:tregister);
  54. constructor op_reg_const_reg(op:tasmop;_size:topsize;_op1:TRegister;_op2:aWord;_op3:tregister);
  55. constructor op_const_ref_reg(op:tasmop;_size:topsize;_op1:aword;const _op2:treference;_op3:tregister);
  56. constructor op_reg_reg_ref(op:tasmop;_size:topsize;_op1,_op2:tregister; const _op3:treference);
  57. constructor op_const_reg_ref(op:tasmop;_size:topsize;_op1:aword;_op2:tregister;const _op3:treference);
  58. { this is for Jmp instructions }
  59. constructor op_cond_sym(op:tasmop;cond:TAsmCond;_size:topsize;_op1:tasmsymbol);
  60. constructor op_sym(op:tasmop;_size:topsize;_op1:tasmsymbol);
  61. constructor op_sym_ofs(op:tasmop;_size:topsize;_op1:tasmsymbol;_op1ofs:longint);
  62. constructor op_sym_ofs_reg(op:tasmop;_size:topsize;_op1:tasmsymbol;_op1ofs:longint;_op2:tregister);
  63. constructor op_sym_ofs_ref(op:tasmop;_size:topsize;_op1:tasmsymbol;_op1ofs:longint;const _op2:treference);
  64. procedure changeopsize(siz:topsize);
  65. function GetString:string;
  66. procedure CheckNonCommutativeOpcodes;
  67. private
  68. FOperandOrder:TOperandOrder;
  69. procedure init(_size:topsize);{this need to be called by all constructor}
  70. public
  71. { the next will reset all instructions that can change in pass 2 }
  72. procedure ResetPass1;
  73. procedure ResetPass2;
  74. function CheckIfValid:boolean;
  75. function Pass1(offset:longint):longint;virtual;
  76. procedure SetOperandOrder(order:TOperandOrder);
  77. private
  78. { next fields are filled in pass1, so pass2 is faster }
  79. insentry : PInsEntry;
  80. insoffset,
  81. inssize : longint;
  82. LastInsOffset : longint; { need to be public to be reset }
  83. function InsEnd:longint;
  84. procedure create_ot;
  85. function Matches(p:PInsEntry):longint;
  86. function calcsize(p:PInsEntry):longint;
  87. function NeedAddrPrefix(opidx:byte):boolean;
  88. procedure Swatoperands;
  89. end;
  90. PROCEDURE DoneAsm;
  91. PROCEDURE InitAsm;
  92. implementation
  93. uses
  94. cutils,
  95. CpuGas;
  96. {****************************************************************************
  97. TAI_ALIGN
  98. ****************************************************************************}
  99. constructor tai_align.create(b:byte);
  100. begin
  101. inherited create(b);
  102. reg:= R_NONE;
  103. end;
  104. constructor tai_align.create_op(b:byte; _op:byte);
  105. begin
  106. inherited create_op(b,_op);
  107. reg:= R_NONE;
  108. end;
  109. function tai_align.getfillbuf:pchar;
  110. const
  111. alignarray:array[0..5] of string[8]=(
  112. #$8D#$B4#$26#$00#$00#$00#$00,
  113. #$8D#$B6#$00#$00#$00#$00,
  114. #$8D#$74#$26#$00,
  115. #$8D#$76#$00,
  116. #$89#$F6,
  117. #$90
  118. );
  119. var
  120. bufptr:pchar;
  121. j:longint;
  122. begin
  123. if not use_op then
  124. begin
  125. bufptr:=@buf;
  126. while (fillsize>0) do
  127. begin
  128. for j:=0 to 5 do
  129. if (fillsize>=length(alignarray[j])) then
  130. break;
  131. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  132. inc(bufptr,length(alignarray[j]));
  133. dec(fillsize,length(alignarray[j]));
  134. end;
  135. end;
  136. getfillbuf:=pchar(@buf);
  137. end;
  138. {*****************************************************************************
  139. Taicpu Constructors
  140. *****************************************************************************}
  141. procedure taicpu.changeopsize(siz:topsize);
  142. begin
  143. opsize:=siz;
  144. end;
  145. procedure taicpu.init(_size:topsize);
  146. begin
  147. { default order is att }
  148. FOperandOrder:=op_att;
  149. {segprefix:=R_NONE;}{This may be only for I386 architecture!}
  150. opsize:=_size;
  151. end;
  152. constructor taicpu.op_none(op:tasmop;_size:topsize);
  153. begin
  154. inherited create(op);
  155. init(_size);
  156. end;
  157. constructor taicpu.op_reg(op:tasmop;_size:topsize;_op1:tregister);
  158. begin
  159. inherited create(op);
  160. init(_size);
  161. ops:=1;
  162. loadreg(0,_op1);
  163. end;
  164. constructor taicpu.op_const(op:tasmop;_size:topsize;_op1:aword);
  165. begin
  166. inherited create(op);
  167. init(_size);
  168. ops:=1;
  169. loadconst(0,_op1);
  170. end;
  171. constructor taicpu.op_ref(op:tasmop;_size:topsize;const _op1:treference);
  172. begin
  173. inherited create(op);
  174. init(_size);
  175. ops:=1;
  176. loadref(0,_op1);
  177. end;
  178. constructor taicpu.op_reg_reg(op:tasmop;_size:topsize;_op1,_op2:tregister);
  179. begin
  180. inherited create(op);
  181. init(_size);
  182. ops:=2;
  183. loadreg(0,_op1);
  184. loadreg(1,_op2);
  185. end;
  186. constructor taicpu.op_reg_const(op:tasmop; _size:topsize; _op1:tregister; _op2:aword);
  187. begin
  188. inherited create(op);
  189. init(_size);
  190. ops:=2;
  191. loadreg(0,_op1);
  192. loadconst(1,_op2);
  193. end;
  194. constructor taicpu.op_reg_ref(op:tasmop;_size:topsize;_op1:tregister;const _op2:treference);
  195. begin
  196. inherited create(op);
  197. init(_size);
  198. ops:=2;
  199. loadreg(0,_op1);
  200. loadref(1,_op2);
  201. end;
  202. constructor taicpu.op_const_reg(op:tasmop;_size:topsize;_op1:aword;_op2:tregister);
  203. begin
  204. inherited create(op);
  205. init(_size);
  206. ops:=2;
  207. loadconst(0,_op1);
  208. loadreg(1,_op2);
  209. end;
  210. constructor taicpu.op_const_const(op:tasmop;_size:topsize;_op1,_op2:aword);
  211. begin
  212. inherited create(op);
  213. init(_size);
  214. ops:=2;
  215. loadconst(0,_op1);
  216. loadconst(1,_op2);
  217. end;
  218. constructor taicpu.op_const_ref(op:tasmop;_size:topsize;_op1:aword;const _op2:treference);
  219. begin
  220. inherited create(op);
  221. init(_size);
  222. ops:=2;
  223. loadconst(0,_op1);
  224. loadref(1,_op2);
  225. end;
  226. constructor taicpu.op_ref_reg(op:tasmop;_size:topsize;const _op1:treference;_op2:tregister);
  227. begin
  228. inherited create(op);
  229. init(_size);
  230. ops:=2;
  231. loadref(0,_op1);
  232. loadreg(1,_op2);
  233. end;
  234. constructor taicpu.op_ref_ref(op:tasmop;_size:topsize;const _op1,_op2:treference);
  235. begin
  236. inherited create(op);
  237. init(_size);
  238. ops:=2;
  239. loadref(0,_op1);
  240. loadref(1,_op2);
  241. end;
  242. constructor taicpu.op_reg_reg_reg(op:tasmop;_size:topsize;_op1,_op2,_op3:tregister);
  243. begin
  244. inherited create(op);
  245. init(_size);
  246. ops:=3;
  247. loadreg(0,_op1);
  248. loadreg(1,_op2);
  249. loadreg(2,_op3);
  250. end;
  251. CONSTRUCTOR taicpu.op_reg_const_reg(op:tasmop;_size:topsize;_op1:TRegister;_op2:aWord;_op3:TRegister);
  252. BEGIN
  253. INHERITED create(op);
  254. init(_size);
  255. ops:=3;
  256. LoadReg(0,_op1);
  257. LoadConst(1,_op2);
  258. LoadReg(2,_op3);
  259. END;
  260. constructor taicpu.op_reg_reg_ref(op:tasmop;_size:topsize;_op1,_op2:tregister;const _op3:treference);
  261. begin
  262. inherited create(op);
  263. init(_size);
  264. ops:=3;
  265. loadreg(0,_op1);
  266. loadreg(1,_op2);
  267. loadref(2,_op3);
  268. end;
  269. constructor taicpu.op_const_ref_reg(op:tasmop;_size:topsize;_op1:aword;const _op2:treference;_op3:tregister);
  270. begin
  271. inherited create(op);
  272. init(_size);
  273. ops:=3;
  274. loadconst(0,_op1);
  275. loadref(1,_op2);
  276. loadreg(2,_op3);
  277. end;
  278. constructor taicpu.op_const_reg_ref(op:tasmop;_size:topsize;_op1:aword;_op2:tregister;const _op3:treference);
  279. begin
  280. inherited create(op);
  281. init(_size);
  282. ops:=3;
  283. loadconst(0,_op1);
  284. loadreg(1,_op2);
  285. loadref(2,_op3);
  286. end;
  287. constructor taicpu.op_cond_sym(op:tasmop;cond:TAsmCond;_size:topsize;_op1:tasmsymbol);
  288. begin
  289. inherited create(op);
  290. init(_size);
  291. condition:=cond;
  292. ops:=1;
  293. loadsymbol(0,_op1,0);
  294. end;
  295. constructor taicpu.op_sym(op:tasmop;_size:topsize;_op1:tasmsymbol);
  296. begin
  297. inherited create(op);
  298. init(_size);
  299. ops:=1;
  300. loadsymbol(0,_op1,0);
  301. end;
  302. constructor taicpu.op_sym_ofs(op:tasmop;_size:topsize;_op1:tasmsymbol;_op1ofs:longint);
  303. begin
  304. inherited create(op);
  305. init(_size);
  306. ops:=1;
  307. loadsymbol(0,_op1,_op1ofs);
  308. end;
  309. constructor taicpu.op_sym_ofs_reg(op:tasmop;_size:topsize;_op1:tasmsymbol;_op1ofs:longint;_op2:tregister);
  310. begin
  311. inherited create(op);
  312. init(_size);
  313. ops:=2;
  314. loadsymbol(0,_op1,_op1ofs);
  315. loadreg(1,_op2);
  316. end;
  317. constructor taicpu.op_sym_ofs_ref(op:tasmop;_size:topsize;_op1:tasmsymbol;_op1ofs:longint;const _op2:treference);
  318. begin
  319. inherited create(op);
  320. init(_size);
  321. ops:=2;
  322. loadsymbol(0,_op1,_op1ofs);
  323. loadref(1,_op2);
  324. end;
  325. function taicpu.GetString:string;
  326. var
  327. i:longint;
  328. s:string;
  329. addsize:boolean;
  330. begin
  331. s:='['+std_op2str[opcode];
  332. for i:=1to ops do
  333. begin
  334. if i=1 then
  335. s:=s+' '
  336. else
  337. s:=s+',';
  338. { type }
  339. addsize:=false;
  340. if (oper[i-1].ot and OT_XMMREG)=OT_XMMREG then
  341. s:=s+'xmmreg'
  342. else
  343. if (oper[i-1].ot and OT_MMXREG)=OT_MMXREG then
  344. s:=s+'mmxreg'
  345. else
  346. if (oper[i-1].ot and OT_FPUREG)=OT_FPUREG then
  347. s:=s+'fpureg'
  348. else
  349. if (oper[i-1].ot and OT_REGISTER)=OT_REGISTER then
  350. begin
  351. s:=s+'reg';
  352. addsize:=true;
  353. end
  354. else
  355. if (oper[i-1].ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  356. begin
  357. s:=s+'imm';
  358. addsize:=true;
  359. end
  360. else
  361. if (oper[i-1].ot and OT_MEMORY)=OT_MEMORY then
  362. begin
  363. s:=s+'mem';
  364. addsize:=true;
  365. end
  366. else
  367. s:=s+'???';
  368. { size }
  369. if addsize then
  370. begin
  371. if (oper[i-1].ot and OT_BITS8)<>0 then
  372. s:=s+'8'
  373. else
  374. if (oper[i-1].ot and OT_BITS16)<>0 then
  375. s:=s+'16'
  376. else
  377. if (oper[i-1].ot and OT_BITS32)<>0 then
  378. s:=s+'32'
  379. else
  380. s:=s+'??';
  381. { signed }
  382. if (oper[i-1].ot and OT_SIGNED)<>0 then
  383. s:=s+'s';
  384. end;
  385. end;
  386. GetString:=s+']';
  387. end;
  388. procedure taicpu.Swatoperands;
  389. var
  390. p:TOper;
  391. begin
  392. { Fix the operands which are in AT&T style and we need them in Intel style }
  393. case ops of
  394. 2:begin
  395. { 0,1 -> 1,0 }
  396. p:=oper[0];
  397. oper[0]:=oper[1];
  398. oper[1]:=p;
  399. end;
  400. 3:begin
  401. { 0,1,2 -> 2,1,0 }
  402. p:=oper[0];
  403. oper[0]:=oper[2];
  404. oper[2]:=p;
  405. end;
  406. end;
  407. end;
  408. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  409. begin
  410. if FOperandOrder<>order then
  411. begin
  412. Swatoperands;
  413. FOperandOrder:=order;
  414. end;
  415. end;
  416. { This check must be done with the operand in ATT order
  417. i.e.after swapping in the intel reader
  418. but before swapping in the NASM and TASM writers PM }
  419. procedure taicpu.CheckNonCommutativeOpcodes;
  420. begin
  421. { if ((ops=2) and
  422. (oper[0].typ=top_reg) and
  423. (oper[1].typ=top_reg) and
  424. (oper[0].reg IN [R_F0..RF31])) or
  425. (ops=0) then
  426. if opcode=A_FSUBR then
  427. opcode:=A_FSUB
  428. else if opcode=A_FSUB then
  429. opcode:=A_FSUBR
  430. else if opcode=A_FDIVR then
  431. opcode:=A_FDIV
  432. else if opcode=A_FDIV then
  433. opcode:=A_FDIVR
  434. else if opcode=A_FSUBRP then
  435. opcode:=A_FSUBP
  436. else if opcode=A_FSUBP then
  437. opcode:=A_FSUBRP
  438. else if opcode=A_FDIVRP then
  439. opcode:=A_FDIVP
  440. else if opcode=A_FDIVP then
  441. opcode:=A_FDIVRP;
  442. if ((ops=1) and
  443. (oper[0].typ=top_reg) and
  444. (oper[0].reg in [R_ST1..R_ST7])) then
  445. if opcode=A_FSUBRP then
  446. opcode:=A_FSUBP
  447. else if opcode=A_FSUBP then
  448. opcode:=A_FSUBRP
  449. else if opcode=A_FDIVRP then
  450. opcode:=A_FDIVP
  451. else if opcode=A_FDIVP then
  452. opcode:=A_FDIVRP;}
  453. end;
  454. {*****************************************************************************
  455. Assembler
  456. *****************************************************************************}
  457. type
  458. ea=packed record
  459. sib_present:boolean;
  460. bytes:byte;
  461. size:byte;
  462. modrm:byte;
  463. sib:byte;
  464. end;
  465. procedure taicpu.create_ot;
  466. {
  467. this function will also fix some other fields which only needs to be once
  468. }
  469. var
  470. i,l,relsize:longint;
  471. begin
  472. if ops=0 then
  473. exit;
  474. { update oper[].ot field }
  475. for i:=0 to ops-1 do
  476. with oper[i] do
  477. begin
  478. case typ of
  479. top_reg:
  480. {ot:=reg2type[reg]};
  481. top_ref:
  482. begin
  483. { create ot field }
  484. if (ot and OT_SIZE_MASK)=0 then
  485. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  486. else
  487. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  488. if (ref^.base=R_NONE) and (ref^.index=R_NONE) then
  489. ot:=ot or OT_MEM_OFFS;
  490. { fix scalefactor }
  491. if (ref^.index=R_NONE) then
  492. ref^.scalefactor:=0
  493. else
  494. if (ref^.scalefactor=0) then
  495. ref^.scalefactor:=1;
  496. end;
  497. top_const:
  498. begin
  499. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  500. ot:=OT_IMM8 or OT_SIGNED
  501. else
  502. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  503. end;
  504. top_symbol:
  505. begin
  506. if LastInsOffset=-1 then
  507. l:=0
  508. else
  509. l:=InsOffset-LastInsOffset;
  510. inc(l,symofs);
  511. if assigned(sym) then
  512. inc(l,sym.address);
  513. { instruction size will then always become 2 (PFV) }
  514. relsize:=(InsOffset+2)-l;
  515. if (not assigned(sym) or
  516. ((sym.currbind<>AB_EXTERNAL) and (sym.address<>0))) and
  517. (relsize>=-128) and (relsize<=127) then
  518. ot:=OT_IMM32 or OT_SHORT
  519. else
  520. ot:=OT_IMM32 or OT_NEAR;
  521. end;
  522. end;
  523. end;
  524. end;
  525. function taicpu.InsEnd:longint;
  526. begin
  527. InsEnd:=InsOffset+InsSize;
  528. end;
  529. function taicpu.Matches(p:PInsEntry):longint;
  530. { * IF_SM stands for Size Match:any operand whose size is not
  531. * explicitly specified by the template is `really' intended to be
  532. * the same size as the first size-specified operand.
  533. * Non-specification is tolerated in the input instruction, but
  534. * _wrong_ specification is not.
  535. *
  536. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  537. * three-operand instructions such as SHLD:it implies that the
  538. * first two operands must match in size, but that the third is
  539. * required to be _unspecified_.
  540. *
  541. * IF_SB invokes Size Byte:operands with unspecified size in the
  542. * template are really bytes, and so no non-byte specification in
  543. * the input instruction will be tolerated. IF_SW similarly invokes
  544. * Size Word, and IF_SD invokes Size Doubleword.
  545. *
  546. * (The default state if neither IF_SM nor IF_SM2 is specified is
  547. * that any operand with unspecified size in the template is
  548. * required to have unspecified size in the instruction too...)
  549. }
  550. var
  551. i,j,asize,oprs:longint;
  552. siz:array[0..2] of longint;
  553. begin
  554. Matches:=100;
  555. { Check the opcode and operands }
  556. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  557. begin
  558. Matches:=0;
  559. exit;
  560. end;
  561. { Check that no spurious colons or TOs are present }
  562. for i:=0 to p^.ops-1 do
  563. if (oper[i].ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  564. begin
  565. Matches:=0;
  566. exit;
  567. end;
  568. { Check that the operand flags all match up }
  569. for i:=0 to p^.ops-1 do
  570. begin
  571. if ((p^.optypes[i] and (not oper[i].ot)) or
  572. ((p^.optypes[i] and OT_SIZE_MASK) and
  573. ((p^.optypes[i] xor oper[i].ot) and OT_SIZE_MASK)))<>0 then
  574. begin
  575. if ((p^.optypes[i] and (not oper[i].ot) and OT_NON_SIZE) or
  576. (oper[i].ot and OT_SIZE_MASK))<>0 then
  577. begin
  578. Matches:=0;
  579. exit;
  580. end
  581. else
  582. Matches:=1;
  583. end;
  584. end;
  585. { Check operand sizes }
  586. { as default an untyped size can get all the sizes, this is different
  587. from nasm, but else we need to do a lot checking which opcodes want
  588. size or not with the automatic size generation }
  589. asize:=longint($ffffffff);
  590. if (p^.flags and IF_SB)<>0 then
  591. asize:=OT_BITS8
  592. else if (p^.flags and IF_SW)<>0 then
  593. asize:=OT_BITS16
  594. else if (p^.flags and IF_SD)<>0 then
  595. asize:=OT_BITS32;
  596. if (p^.flags and IF_ARMASK)<>0 then
  597. begin
  598. siz[0]:=0;
  599. siz[1]:=0;
  600. siz[2]:=0;
  601. if (p^.flags and IF_AR0)<>0 then
  602. siz[0]:=asize
  603. else if (p^.flags and IF_AR1)<>0 then
  604. siz[1]:=asize
  605. else if (p^.flags and IF_AR2)<>0 then
  606. siz[2]:=asize;
  607. end
  608. else
  609. begin
  610. { we can leave because the size for all operands is forced to be
  611. the same
  612. but not if IF_SB IF_SW or IF_SD is set PM }
  613. if asize=-1 then
  614. exit;
  615. siz[0]:=asize;
  616. siz[1]:=asize;
  617. siz[2]:=asize;
  618. end;
  619. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  620. begin
  621. if (p^.flags and IF_SM2)<>0 then
  622. oprs:=2
  623. else
  624. oprs:=p^.ops;
  625. for i:=0 to oprs-1 do
  626. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  627. begin
  628. for j:=0 to oprs-1 do
  629. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  630. break;
  631. end;
  632. end
  633. else
  634. oprs:=2;
  635. { Check operand sizes }
  636. for i:=0 to p^.ops-1 do
  637. begin
  638. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  639. ((oper[i].ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  640. { Immediates can always include smaller size }
  641. ((oper[i].ot and OT_IMMEDIATE)=0) and
  642. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i].ot and OT_SIZE_MASK)) then
  643. Matches:=2;
  644. end;
  645. end;
  646. procedure taicpu.ResetPass1;
  647. begin
  648. { we need to reset everything here, because the choosen insentry
  649. can be invalid for a new situation where the previously optimized
  650. insentry is not correct }
  651. InsEntry:=nil;
  652. InsSize:=0;
  653. LastInsOffset:=-1;
  654. end;
  655. procedure taicpu.ResetPass2;
  656. begin
  657. { we are here in a second pass, check if the instruction can be optimized }
  658. if assigned(InsEntry) and
  659. ((InsEntry^.flags and IF_PASS2)<>0) then
  660. begin
  661. InsEntry:=nil;
  662. InsSize:=0;
  663. end;
  664. LastInsOffset:=-1;
  665. end;
  666. function taicpu.CheckIfValid:boolean;
  667. var
  668. m,i:longint;
  669. begin
  670. CheckIfValid:=false;
  671. { Things which may only be done once, not when a second pass is done to
  672. optimize }
  673. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  674. begin
  675. { We need intel style operands }
  676. SetOperandOrder(op_intel);
  677. { create the .ot fields }
  678. create_ot;
  679. { set the file postion }
  680. aktfilepos:=fileinfo;
  681. end
  682. else
  683. begin
  684. { we've already an insentry so it's valid }
  685. CheckIfValid:=true;
  686. exit;
  687. end;
  688. { Lookup opcode in the table }
  689. InsSize:=-1;
  690. i:=instabcache^[opcode];
  691. if i=-1 then
  692. begin
  693. {$ifdef TP}
  694. Message1(asmw_e_opcode_not_in_table,'');
  695. {$else}
  696. Message1(asmw_e_opcode_not_in_table,std_op2str[opcode]);
  697. {$endif}
  698. exit;
  699. end;
  700. // insentry:=@instab[i];
  701. while (insentry^.opcode=opcode) do
  702. begin
  703. m:=matches(insentry);
  704. if m=100 then
  705. begin
  706. InsSize:=calcsize(insentry);
  707. {if (segprefix<>R_NONE) then
  708. inc(InsSize);}{No segprefix!}
  709. { For opsize if size if forced }
  710. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  711. begin
  712. if (insentry^.flags and IF_ARMASK)=0 then
  713. begin
  714. if (insentry^.flags and IF_SB)<>0 then
  715. begin
  716. if opsize=S_NO then
  717. opsize:=S_B;
  718. end
  719. else if (insentry^.flags and IF_SW)<>0 then
  720. begin
  721. if opsize=S_NO then
  722. opsize:=S_W;
  723. end
  724. else if (insentry^.flags and IF_SD)<>0 then
  725. begin
  726. if opsize=S_NO then
  727. opsize:=S_L;
  728. end;
  729. end;
  730. end;
  731. CheckIfValid:=true;
  732. exit;
  733. end;
  734. inc(i);
  735. // insentry:=@instab[i];
  736. end;
  737. if insentry^.opcode<>opcode then
  738. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  739. { No instruction found, set insentry to nil and inssize to -1 }
  740. insentry:=nil;
  741. inssize:=-1;
  742. end;
  743. function taicpu.Pass1(offset:longint):longint;
  744. begin
  745. Pass1:=0;
  746. { Save the old offset and set the new offset }
  747. InsOffset:=Offset;
  748. { Things which may only be done once, not when a second pass is done to
  749. optimize }
  750. if Insentry=nil then
  751. begin
  752. { Check if error last time then InsSize=-1 }
  753. if InsSize=-1 then
  754. exit;
  755. { set the file postion }
  756. aktfilepos:=fileinfo;
  757. end
  758. else
  759. begin
  760. {$ifdef PASS2FLAG}
  761. { we are here in a second pass, check if the instruction can be optimized }
  762. if (InsEntry^.flags and IF_PASS2)=0 then
  763. begin
  764. Pass1:=InsSize;
  765. exit;
  766. end;
  767. { update the .ot fields, some top_const can be updated }
  768. create_ot;
  769. {$endif}
  770. end;
  771. { Check if it's a valid instruction }
  772. if CheckIfValid then
  773. begin
  774. LastInsOffset:=InsOffset;
  775. Pass1:=InsSize;
  776. exit;
  777. end;
  778. LastInsOffset:=-1;
  779. end;
  780. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  781. var
  782. i,b:tregister;
  783. begin
  784. { if (OT_MEMORY and (not oper[opidx].ot))=0 then
  785. begin
  786. i:=oper[opidx].ref^.index;
  787. b:=oper[opidx].ref^.base;
  788. if not(i in [R_NONE,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) or
  789. not(b in [R_NONE,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) then
  790. begin
  791. NeedAddrPrefix:=true;
  792. exit;
  793. end;
  794. end;}
  795. NeedAddrPrefix:=false;
  796. end;
  797. function regval(r:tregister):byte;
  798. begin
  799. {case r of
  800. R_EAX,R_AX,R_AL,R_ES,R_CR0,R_DR0,R_ST,R_ST0,R_MM0,R_XMM0:
  801. regval:=0;
  802. R_ECX,R_CX,R_CL,R_CS,R_DR1,R_ST1,R_MM1,R_XMM1:
  803. regval:=1;
  804. R_EDX,R_DX,R_DL,R_SS,R_CR2,R_DR2,R_ST2,R_MM2,R_XMM2:
  805. regval:=2;
  806. R_EBX,R_BX,R_BL,R_DS,R_CR3,R_DR3,R_TR3,R_ST3,R_MM3,R_XMM3:
  807. regval:=3;
  808. R_ESP,R_SP,R_AH,R_FS,R_CR4,R_TR4,R_ST4,R_MM4,R_XMM4:
  809. regval:=4;
  810. R_EBP,R_BP,R_CH,R_GS,R_TR5,R_ST5,R_MM5,R_XMM5:
  811. regval:=5;
  812. R_ESI,R_SI,R_DH,R_DR6,R_TR6,R_ST6,R_MM6,R_XMM6:
  813. regval:=6;
  814. R_EDI,R_DI,R_BH,R_DR7,R_TR7,R_ST7,R_MM7,R_XMM7:
  815. regval:=7;
  816. else}
  817. begin
  818. internalerror(777001);
  819. regval:=0;
  820. end;
  821. { end;}
  822. end;
  823. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  824. {const
  825. regs:array[0..63] of tregister=(
  826. R_MM0, R_EAX, R_AX, R_AL, R_XMM0, R_NONE, R_NONE, R_NONE,
  827. R_MM1, R_ECX, R_CX, R_CL, R_XMM1, R_NONE, R_NONE, R_NONE,
  828. R_MM2, R_EDX, R_DX, R_DL, R_XMM2, R_NONE, R_NONE, R_NONE,
  829. R_MM3, R_EBX, R_BX, R_BL, R_XMM3, R_NONE, R_NONE, R_NONE,
  830. R_MM4, R_ESP, R_SP, R_AH, R_XMM4, R_NONE, R_NONE, R_NONE,
  831. R_MM5, R_EBP, R_BP, R_CH, R_XMM5, R_NONE, R_NONE, R_NONE,
  832. R_MM6, R_ESI, R_SI, R_DH, R_XMM6, R_NONE, R_NONE, R_NONE,
  833. R_MM7, R_EDI, R_DI, R_BH, R_XMM7, R_NONE, R_NONE, R_NONE
  834. );}
  835. var
  836. j:longint;
  837. i,b:tregister;
  838. sym:tasmsymbol;
  839. md,s:byte;
  840. base,index,scalefactor,
  841. o:longint;
  842. begin
  843. process_ea:=false;
  844. { register ? }
  845. { if (input.typ=top_reg) then
  846. begin
  847. j:=0;
  848. while (j<=high(regs)) do
  849. begin
  850. if input.reg=regs[j] then
  851. break;
  852. inc(j);
  853. end;
  854. if j<=high(regs) then
  855. begin
  856. output.sib_present:=false;
  857. output.bytes:=0;
  858. output.modrm:=$c0 or (rfield shl 3) or (j shr 3);
  859. output.size:=1;
  860. process_ea:=true;
  861. end;
  862. exit;
  863. end;}
  864. { memory reference }
  865. i:=input.ref^.index;
  866. b:=input.ref^.base;
  867. s:=input.ref^.scalefactor;
  868. o:=input.ref^.offset+input.ref^.offsetfixup;
  869. sym:=input.ref^.symbol;
  870. { it's direct address }
  871. if (b=R_NONE) and (i=R_NONE) then
  872. begin
  873. { it's a pure offset }
  874. output.sib_present:=false;
  875. output.bytes:=4;
  876. output.modrm:=5 or (rfield shl 3);
  877. end
  878. else
  879. { it's an indirection }
  880. begin
  881. { 16 bit address? }
  882. { if not((i in [R_NONE,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) and
  883. (b in [R_NONE,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI])) then
  884. Message(asmw_e_16bit_not_supported);}
  885. {$ifdef OPTEA}
  886. { make single reg base }
  887. if (b=R_NONE) and (s=1) then
  888. begin
  889. b:=i;
  890. i:=R_NONE;
  891. end;
  892. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  893. { if (b=R_NONE) and
  894. (((s=2) and (i<>R_ESP)) or
  895. (s=3) or (s=5) or (s=9)) then
  896. begin
  897. b:=i;
  898. dec(s);
  899. end;}
  900. { swap ESP into base if scalefactor is 1 }
  901. { if (s=1) and (i=R_ESP) then
  902. begin
  903. i:=b;
  904. b:=R_ESP;
  905. end;}
  906. {$endif}
  907. { wrong, for various reasons }
  908. { if (i=R_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (i<>R_NONE)) then
  909. exit;}
  910. { base }
  911. { case b of
  912. R_EAX:base:=0;
  913. R_ECX:base:=1;
  914. R_EDX:base:=2;
  915. R_EBX:base:=3;
  916. R_ESP:base:=4;
  917. R_NONE,
  918. R_EBP:base:=5;
  919. R_ESI:base:=6;
  920. R_EDI:base:=7;
  921. else
  922. exit;
  923. end;}
  924. { index }
  925. { case i of
  926. R_EAX:index:=0;
  927. R_ECX:index:=1;
  928. R_EDX:index:=2;
  929. R_EBX:index:=3;
  930. R_NONE:index:=4;
  931. R_EBP:index:=5;
  932. R_ESI:index:=6;
  933. R_EDI:index:=7;
  934. else
  935. exit;
  936. end;
  937. case s of
  938. 0,
  939. 1:scalefactor:=0;
  940. 2:scalefactor:=1;
  941. 4:scalefactor:=2;
  942. 8:scalefactor:=3;
  943. else
  944. exit;
  945. end;
  946. if (b=R_NONE) or
  947. ((b<>R_EBP) and (o=0) and (sym=nil)) then
  948. md:=0
  949. else
  950. if ((o>=-128) and (o<=127) and (sym=nil)) then
  951. md:=1
  952. else
  953. md:=2;
  954. if (b=R_NONE) or (md=2) then
  955. output.bytes:=4
  956. else
  957. output.bytes:=md;}
  958. { SIB needed ? }
  959. { if (i=R_NONE) and (b<>R_ESP) then
  960. begin
  961. output.sib_present:=false;
  962. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  963. end
  964. else
  965. begin
  966. output.sib_present:=true;
  967. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  968. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  969. end;}
  970. end;
  971. if output.sib_present then
  972. output.size:=2+output.bytes
  973. else
  974. output.size:=1+output.bytes;
  975. process_ea:=true;
  976. end;
  977. function taicpu.calcsize(p:PInsEntry):longint;
  978. var
  979. codes:pchar;
  980. c:byte;
  981. len:longint;
  982. ea_data:ea;
  983. begin
  984. len:=0;
  985. codes:=@p^.code;
  986. repeat
  987. c:=ord(codes^);
  988. inc(codes);
  989. case c of
  990. 0:
  991. break;
  992. 1,2,3:
  993. begin
  994. inc(codes,c);
  995. inc(len,c);
  996. end;
  997. 8,9,10:
  998. begin
  999. inc(codes);
  1000. inc(len);
  1001. end;
  1002. 4,5,6,7:
  1003. begin
  1004. if opsize=S_W then
  1005. inc(len,2)
  1006. else
  1007. inc(len);
  1008. end;
  1009. 15,
  1010. 12,13,14,
  1011. 16,17,18,
  1012. 20,21,22,
  1013. 40,41,42:
  1014. inc(len);
  1015. 24,25,26,
  1016. 31,
  1017. 48,49,50:
  1018. inc(len,2);
  1019. 28,29,30, { we don't have 16 bit immediates code }
  1020. 32,33,34,
  1021. 52,53,54,
  1022. 56,57,58:
  1023. inc(len,4);
  1024. 192,193,194:
  1025. if NeedAddrPrefix(c-192) then
  1026. inc(len);
  1027. 208:
  1028. inc(len);
  1029. 200,
  1030. 201,
  1031. 202,
  1032. 209,
  1033. 210,
  1034. 217,218,219:;
  1035. 216:
  1036. begin
  1037. inc(codes);
  1038. inc(len);
  1039. end;
  1040. 224,225,226:
  1041. begin
  1042. InternalError(777002);
  1043. end;
  1044. else
  1045. begin
  1046. if (c>=64) and (c<=191) then
  1047. begin
  1048. if not process_ea(oper[(c shr 3) and 7], ea_data, 0) then
  1049. Message(asmw_e_invalid_effective_address)
  1050. else
  1051. inc(len,ea_data.size);
  1052. end
  1053. else
  1054. InternalError(777003);
  1055. end;
  1056. end;
  1057. until false;
  1058. calcsize:=len;
  1059. end;
  1060. procedure DoneAsm;
  1061. begin
  1062. end;
  1063. procedure InitAsm;
  1064. begin
  1065. end;
  1066. end.
  1067. {
  1068. $Log$
  1069. Revision 1.5 2002-10-15 09:00:28 mazen
  1070. * sone coding style modified
  1071. Revision 1.4 2002/10/13 21:46:07 mazen
  1072. * assembler output format fixed
  1073. }