2
0

rax86.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. Handles the common x86 assembler reader routines
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {
  18. Contains the common x86 (i386 and x86-64) assembler reader routines.
  19. }
  20. unit rax86;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. aasmbase,aasmtai,aasmdata,aasmcpu,
  25. cpubase,rautils,cclasses;
  26. { Parser helpers }
  27. function is_prefix(t:tasmop):boolean;
  28. function is_override(t:tasmop):boolean;
  29. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  30. Function CheckOverride(overrideop,op:tasmop): Boolean;
  31. Procedure FWaitWarning;
  32. type
  33. Tx86Operand=class(TOperand)
  34. opsize : topsize;
  35. Procedure SetSize(_size:longint;force:boolean);override;
  36. Procedure SetCorrectSize(opcode:tasmop);override;
  37. Function CheckOperand: boolean; override;
  38. end;
  39. Tx86Instruction=class(TInstruction)
  40. OpOrder : TOperandOrder;
  41. opsize : topsize;
  42. constructor Create(optype : tcoperand);override;
  43. { Operand sizes }
  44. procedure AddReferenceSizes;
  45. procedure SetInstructionOpsize;
  46. procedure CheckOperandSizes;
  47. procedure CheckNonCommutativeOpcodes;
  48. procedure SwapOperands;
  49. { Additional actions required by specific reader }
  50. procedure FixupOpcode;virtual;
  51. { opcode adding }
  52. function ConcatInstruction(p : TAsmList) : tai;override;
  53. end;
  54. const
  55. AsmPrefixes = 6;
  56. AsmPrefix : array[0..AsmPrefixes-1] of TasmOP =(
  57. A_LOCK,A_REP,A_REPE,A_REPNE,A_REPNZ,A_REPZ
  58. );
  59. AsmOverrides = 6;
  60. AsmOverride : array[0..AsmOverrides-1] of TasmOP =(
  61. A_SEGCS,A_SEGES,A_SEGDS,A_SEGFS,A_SEGGS,A_SEGSS
  62. );
  63. CondAsmOps=3;
  64. CondAsmOp:array[0..CondAsmOps-1] of TasmOp=(
  65. A_CMOVcc, A_Jcc, A_SETcc
  66. );
  67. CondAsmOpStr:array[0..CondAsmOps-1] of string[4]=(
  68. 'CMOV','J','SET'
  69. );
  70. implementation
  71. uses
  72. globtype,globals,systems,verbose,
  73. procinfo,
  74. cpuinfo,cgbase,cgutils,
  75. itcpugas,cgx86, symsym, cutils;
  76. {*****************************************************************************
  77. Parser Helpers
  78. *****************************************************************************}
  79. function is_prefix(t:tasmop):boolean;
  80. var
  81. i : longint;
  82. Begin
  83. is_prefix:=false;
  84. for i:=1 to AsmPrefixes do
  85. if t=AsmPrefix[i-1] then
  86. begin
  87. is_prefix:=true;
  88. exit;
  89. end;
  90. end;
  91. function is_override(t:tasmop):boolean;
  92. var
  93. i : longint;
  94. Begin
  95. is_override:=false;
  96. for i:=1 to AsmOverrides do
  97. if t=AsmOverride[i-1] then
  98. begin
  99. is_override:=true;
  100. exit;
  101. end;
  102. end;
  103. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  104. { Checks if the prefix is valid with the following opcode }
  105. { return false if not, otherwise true }
  106. Begin
  107. CheckPrefix := TRUE;
  108. (* Case prefix of
  109. A_REP,A_REPNE,A_REPE:
  110. Case opcode Of
  111. A_SCASB,A_SCASW,A_SCASD,
  112. A_INS,A_OUTS,A_MOVS,A_CMPS,A_LODS,A_STOS:;
  113. Else
  114. Begin
  115. CheckPrefix := FALSE;
  116. exit;
  117. end;
  118. end; { case }
  119. A_LOCK:
  120. Case opcode Of
  121. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,A_ADC,A_SBB,A_AND,A_SUB,
  122. A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  123. Else
  124. Begin
  125. CheckPrefix := FALSE;
  126. Exit;
  127. end;
  128. end; { case }
  129. A_NONE: exit; { no prefix here }
  130. else
  131. CheckPrefix := FALSE;
  132. end; { end case } *)
  133. end;
  134. Function CheckOverride(overrideop,op:tasmop): Boolean;
  135. { Check if the override is valid, and if so then }
  136. { update the instr variable accordingly. }
  137. Begin
  138. CheckOverride := true;
  139. { Case instr.getinstruction of
  140. A_MOVS,A_XLAT,A_CMPS:
  141. Begin
  142. CheckOverride := TRUE;
  143. Message(assem_e_segment_override_not_supported);
  144. end
  145. end }
  146. end;
  147. Procedure FWaitWarning;
  148. begin
  149. if (target_info.system=system_i386_GO32V2) and (cs_fp_emulation in current_settings.moduleswitches) then
  150. Message(asmr_w_fwait_emu_prob);
  151. end;
  152. {*****************************************************************************
  153. TX86Operand
  154. *****************************************************************************}
  155. Procedure Tx86Operand.SetSize(_size:longint;force:boolean);
  156. begin
  157. inherited SetSize(_size,force);
  158. { OS_64 will be set to S_L and be fixed later
  159. in SetCorrectSize }
  160. // multimedia register
  161. case _size of
  162. 16: size := OS_M128;
  163. 32: size := OS_M256;
  164. end;
  165. opsize:=TCGSize2Opsize[size];
  166. end;
  167. Procedure Tx86Operand.SetCorrectSize(opcode:tasmop);
  168. begin
  169. if gas_needsuffix[opcode]=attsufFPU then
  170. begin
  171. case size of
  172. OS_32 : opsize:=S_FS;
  173. OS_64 : opsize:=S_FL;
  174. end;
  175. end
  176. else if gas_needsuffix[opcode]=attsufFPUint then
  177. begin
  178. case size of
  179. OS_16 : opsize:=S_IS;
  180. OS_32 : opsize:=S_IL;
  181. OS_64 : opsize:=S_IQ;
  182. end;
  183. end
  184. else if gas_needsuffix[opcode]=AttSufMM then
  185. begin
  186. if (opr.typ=OPR_Reference) then
  187. begin
  188. case size of
  189. OS_32 : size := OS_M32;
  190. OS_64 : size := OS_M64;
  191. end;
  192. end;
  193. end
  194. else
  195. begin
  196. if size=OS_64 then
  197. opsize:=S_Q;
  198. end;
  199. end;
  200. Function Tx86Operand.CheckOperand: boolean;
  201. begin
  202. result:=true;
  203. if (opr.typ=OPR_Reference) then
  204. begin
  205. if not hasvar then
  206. begin
  207. if (getsupreg(opr.ref.base)=RS_EBP) and (opr.ref.offset>0) then
  208. begin
  209. if current_procinfo.procdef.proccalloption=pocall_register then
  210. message(asmr_w_no_direct_ebp_for_parameter)
  211. else
  212. message(asmr_w_direct_ebp_for_parameter_regcall);
  213. end
  214. else if (getsupreg(opr.ref.base)=RS_EBP) and (opr.ref.offset<0) then
  215. message(asmr_w_direct_ebp_neg_offset)
  216. else if (getsupreg(opr.ref.base)=RS_ESP) and (opr.ref.offset<0) then
  217. message(asmr_w_direct_esp_neg_offset);
  218. end;
  219. if (cs_create_pic in current_settings.moduleswitches) and
  220. assigned(opr.ref.symbol) and
  221. not assigned(opr.ref.relsymbol) then
  222. begin
  223. if not(opr.ref.refaddr in [addr_pic,addr_pic_no_got]) then
  224. begin
  225. if (opr.ref.symbol.name <> '_GLOBAL_OFFSET_TABLE_') then
  226. begin
  227. message(asmr_e_need_pic_ref);
  228. result:=false;
  229. end
  230. else
  231. opr.ref.refaddr:=addr_pic;
  232. end
  233. else
  234. begin
  235. {$ifdef x86_64}
  236. { should probably be extended to i386, but there the situation
  237. is more complex and ELF-style PIC still need to be
  238. tested/debugged }
  239. if (opr.ref.symbol.bind in [AB_LOCAL,AB_PRIVATE_EXTERN]) and
  240. (opr.ref.refaddr=addr_pic) then
  241. message(asmr_w_useless_got_for_local)
  242. else if (opr.ref.symbol.bind in [AB_GLOBAL,AB_EXTERNAL,AB_COMMON,AB_WEAK_EXTERNAL]) and
  243. (opr.ref.refaddr=addr_pic_no_got) then
  244. message(asmr_w_global_access_without_got);
  245. {$endif x86_64}
  246. end;
  247. end;
  248. end;
  249. end;
  250. {*****************************************************************************
  251. T386Instruction
  252. *****************************************************************************}
  253. constructor Tx86Instruction.Create(optype : tcoperand);
  254. begin
  255. inherited Create(optype);
  256. Opsize:=S_NO;
  257. end;
  258. procedure Tx86Instruction.SwapOperands;
  259. begin
  260. Inherited SwapOperands;
  261. { mark the correct order }
  262. if OpOrder=op_intel then
  263. OpOrder:=op_att
  264. else
  265. OpOrder:=op_intel;
  266. end;
  267. const
  268. {$ifdef x86_64}
  269. topsize2memsize: array[topsize] of integer =
  270. (0, 8,16,32,64,8,8,16,8,16,32,
  271. 16,32,64,
  272. 16,32,64,0,0,
  273. 64,
  274. 0,0,0,
  275. 80,
  276. 128,
  277. 256
  278. );
  279. {$else}
  280. topsize2memsize: array[topsize] of integer =
  281. (0, 8,16,32,64,8,8,16,
  282. 16,32,64,
  283. 16,32,64,0,0,
  284. 64,
  285. 0,0,0,
  286. 80,
  287. 128,
  288. 256
  289. );
  290. {$endif}
  291. procedure Tx86Instruction.AddReferenceSizes;
  292. { this will add the sizes for references like [esi] which do not
  293. have the size set yet, it will take only the size if the other
  294. operand is a register }
  295. var
  296. operand2,i,j : longint;
  297. s : tasmsymbol;
  298. so : aint;
  299. ExistsMemRefNoSize: boolean;
  300. ExistsMemRef: boolean;
  301. ExistsConstNoSize: boolean;
  302. ExistsLocalSymSize: boolean;
  303. memrefsize: integer;
  304. memopsize: integer;
  305. memoffset: asizeint;
  306. s1: string;
  307. begin
  308. ExistsMemRefNoSize := false;
  309. ExistsMemRef := false;
  310. ExistsConstNoSize := false;
  311. ExistsLocalSymSize := false;
  312. for i := 1 to ops do
  313. begin
  314. if operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL] then
  315. begin
  316. ExistsMemRef := true;
  317. if (tx86operand(operands[i]).opsize = S_NO) then
  318. begin
  319. ExistsMemRefNoSize := true;
  320. case operands[i].opr.Typ of
  321. OPR_LOCAL: ExistsLocalSymSize := tx86operand(operands[i]).opr.localsym.getsize > 0;
  322. OPR_REFERENCE: ExistsLocalSymSize := true;
  323. end;
  324. end;
  325. end
  326. else if operands[i].Opr.Typ in [OPR_CONSTANT] then
  327. begin
  328. ExistsConstNoSize := tx86operand(operands[i]).opsize = S_NO;
  329. end;
  330. end;
  331. if (ExistsMemRef) and
  332. (MemRefInfo(opcode).ExistsSSEAVX) then
  333. begin
  334. if (not(ExistsMemRefNoSize)) or
  335. (ExistsLocalSymSize) then
  336. begin
  337. // - validate memory-reference-size
  338. for i := 1 to ops do
  339. begin
  340. //if (operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL]) and
  341. // (tx86operand(operands[i]).opsize <> S_NO) then
  342. if (operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL]) then
  343. begin
  344. memrefsize := -1;
  345. case MemRefInfo(opcode).MemRefSize of
  346. msiMem8: memrefsize := 8;
  347. msiMem16: memrefsize := 16;
  348. msiMem32: memrefsize := 32;
  349. msiMem64: memrefsize := 64;
  350. msiMem128: memrefsize := 128;
  351. msiMem256: memrefsize := 256;
  352. msiMemRegSize
  353. : for j := 1 to ops do
  354. begin
  355. if operands[j].Opr.Typ = OPR_REGISTER then
  356. begin
  357. if (tx86operand(operands[j]).opsize <> S_NO) and
  358. (tx86operand(operands[j]).size <> OS_NO) then
  359. begin
  360. case tx86operand(operands[j]).opsize of
  361. S_B : memrefsize := 8;
  362. S_W : memrefsize := 16;
  363. S_L : memrefsize := 32;
  364. S_Q : memrefsize := 64;
  365. S_XMM : memrefsize := 128;
  366. S_YMM : memrefsize := 256;
  367. else Internalerror(777200);
  368. end;
  369. break;
  370. end;
  371. end;
  372. end;
  373. end;
  374. if memrefsize > -1 then
  375. begin
  376. memopsize := 0;
  377. case operands[i].opr.typ of
  378. OPR_LOCAL: memopsize := operands[i].opr.localvarsize * 8;
  379. OPR_REFERENCE: memopsize := operands[i].opr.varsize * 8;
  380. end;
  381. if memopsize = 0 then memopsize := topsize2memsize[tx86operand(operands[i]).opsize];
  382. if (memopsize > 0) and
  383. (memrefsize > 0) then
  384. begin
  385. memoffset := 0;
  386. case operands[i].opr.typ of
  387. OPR_LOCAL:
  388. memoffset := operands[i].opr.localconstoffset;
  389. OPR_REFERENCE:
  390. memoffset := operands[i].opr.constoffset;
  391. end;
  392. if memoffset < 0 then
  393. begin
  394. Message2(asmr_w_check_mem_operand_negative_offset,
  395. std_op2str[opcode],
  396. ToStr(memoffset));
  397. end
  398. else if (memopsize < (memrefsize + memoffset * 8)) then
  399. begin
  400. if memoffset = 0 then
  401. begin
  402. Message3(asmr_w_check_mem_operand_size3,
  403. std_op2str[opcode],
  404. ToStr(memopsize),
  405. ToStr(memrefsize)
  406. );
  407. end
  408. else
  409. begin
  410. Message4(asmr_w_check_mem_operand_size_offset,
  411. std_op2str[opcode],
  412. ToStr(memopsize),
  413. ToStr(memrefsize),
  414. ToStr(memoffset)
  415. );
  416. end;
  417. end;
  418. end;
  419. end;
  420. end;
  421. end;
  422. end;
  423. end;
  424. if (ExistsMemRefNoSize or ExistsConstNoSize) and
  425. (MemRefInfo(opcode).ExistsSSEAVX) then
  426. begin
  427. for i := 1 to ops do
  428. begin
  429. if (tx86operand(operands[i]).opsize = S_NO) then
  430. begin
  431. case operands[i].Opr.Typ of
  432. OPR_REFERENCE:
  433. case MemRefInfo(opcode).MemRefSize of
  434. msiMem8: begin
  435. tx86operand(operands[i]).opsize := S_B;
  436. tx86operand(operands[i]).size := OS_8;
  437. end;
  438. msiMem16: begin
  439. tx86operand(operands[i]).opsize := S_W;
  440. tx86operand(operands[i]).size := OS_16;
  441. end;
  442. msiMem32: begin
  443. tx86operand(operands[i]).opsize := S_L;
  444. tx86operand(operands[i]).size := OS_32;
  445. end;
  446. msiMem64: begin
  447. tx86operand(operands[i]).opsize := S_Q;
  448. tx86operand(operands[i]).size := OS_M64;
  449. end;
  450. msiMem128: begin
  451. tx86operand(operands[i]).opsize := S_XMM;
  452. tx86operand(operands[i]).size := OS_M128;
  453. end;
  454. msiMem256: begin
  455. tx86operand(operands[i]).opsize := S_YMM;
  456. tx86operand(operands[i]).size := OS_M256;
  457. opsize := S_YMM;
  458. end;
  459. msiMemRegSize:
  460. begin
  461. // mem-ref-size = register size
  462. for j := 1 to ops do
  463. begin
  464. if operands[j].Opr.Typ = OPR_REGISTER then
  465. begin
  466. if (tx86operand(operands[j]).opsize <> S_NO) and
  467. (tx86operand(operands[j]).size <> OS_NO) then
  468. begin
  469. tx86operand(operands[i]).opsize := tx86operand(operands[j]).opsize;
  470. tx86operand(operands[i]).size := tx86operand(operands[j]).size;
  471. break;
  472. end
  473. else Message(asmr_e_unable_to_determine_reference_size);
  474. end;
  475. end;
  476. end;
  477. msiMemRegx64y128:
  478. begin
  479. for j := 1 to ops do
  480. begin
  481. if operands[j].Opr.Typ = OPR_REGISTER then
  482. begin
  483. case getsubreg(operands[j].opr.reg) of
  484. R_SUBMMX: begin
  485. tx86operand(operands[i]).opsize := S_Q;
  486. tx86operand(operands[i]).size := OS_M64;
  487. break;
  488. end;
  489. R_SUBMMY: begin
  490. tx86operand(operands[i]).opsize := S_XMM;
  491. tx86operand(operands[i]).size := OS_M128;
  492. break;
  493. end;
  494. else Message(asmr_e_unable_to_determine_reference_size);
  495. end;
  496. end;
  497. end;
  498. end;
  499. msiMemRegx64y256:
  500. begin
  501. for j := 1 to ops do
  502. begin
  503. if operands[j].Opr.Typ = OPR_REGISTER then
  504. begin
  505. case getsubreg(operands[j].opr.reg) of
  506. R_SUBMMX: begin
  507. tx86operand(operands[i]).opsize := S_Q;
  508. tx86operand(operands[i]).size := OS_M64;
  509. break;
  510. end;
  511. R_SUBMMY: begin
  512. tx86operand(operands[i]).opsize := S_YMM;
  513. tx86operand(operands[i]).size := OS_M256;
  514. break;
  515. end;
  516. else Message(asmr_e_unable_to_determine_reference_size);
  517. end;
  518. end;
  519. end;
  520. end;
  521. msiNoSize: ; // all memory-sizes are ok
  522. msiMultiple: Message(asmr_e_unable_to_determine_reference_size); // TODO individual message
  523. end;
  524. OPR_CONSTANT:
  525. case MemRefInfo(opcode).ConstSize of
  526. csiMem8: begin
  527. tx86operand(operands[i]).opsize := S_B;
  528. tx86operand(operands[i]).size := OS_8;
  529. end;
  530. csiMem16: begin
  531. tx86operand(operands[i]).opsize := S_W;
  532. tx86operand(operands[i]).size := OS_16;
  533. end;
  534. csiMem32: begin
  535. tx86operand(operands[i]).opsize := S_L;
  536. tx86operand(operands[i]).size := OS_32;
  537. end;
  538. end;
  539. end;
  540. end;
  541. end;
  542. end;
  543. for i:=1 to ops do
  544. begin
  545. operands[i].SetCorrectSize(opcode);
  546. if tx86operand(operands[i]).opsize=S_NO then
  547. begin
  548. {$ifdef x86_64}
  549. if (opcode=A_MOVQ) and
  550. (ops=2) and
  551. (operands[1].opr.typ=OPR_CONSTANT) then
  552. opsize:=S_Q
  553. else
  554. {$endif x86_64}
  555. case operands[i].Opr.Typ of
  556. OPR_LOCAL,
  557. OPR_REFERENCE :
  558. begin
  559. { for 3-operand opcodes, operand #1 (in ATT order) is always an immediate,
  560. don't consider it. }
  561. if i=ops then
  562. operand2:=i-1
  563. else
  564. operand2:=i+1;
  565. if operand2>0 then
  566. begin
  567. { Only allow register as operand to take the size from }
  568. if operands[operand2].opr.typ=OPR_REGISTER then
  569. begin
  570. if ((opcode<>A_MOVD) and
  571. (opcode<>A_CVTSI2SS)) then
  572. begin
  573. //tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize;
  574. // torsten - 31.01.2012
  575. // old: xmm/ymm-register operands have a opsize = "S_NO"
  576. // new: xmm/ymm-register operands have a opsize = "S_XMM/S_YMM"
  577. // any SSE- and AVX-opcodes have mixed operand sizes (e.g. cvtsd2ss xmmreg, xmmreg/m32)
  578. // in this case is we need the old handling ("S_NO")
  579. // =>> ignore
  580. if (tx86operand(operands[operand2]).opsize <> S_XMM) and
  581. (tx86operand(operands[operand2]).opsize <> S_YMM) then
  582. tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize
  583. else tx86operand(operands[operand2]).opsize := S_NO;
  584. end;
  585. end
  586. else
  587. begin
  588. { if no register then take the opsize (which is available with ATT),
  589. if not availble then give an error }
  590. if opsize<>S_NO then
  591. tx86operand(operands[i]).opsize:=opsize
  592. else
  593. begin
  594. if (m_delphi in current_settings.modeswitches) then
  595. Message(asmr_w_unable_to_determine_reference_size_using_dword)
  596. else
  597. Message(asmr_e_unable_to_determine_reference_size);
  598. { recovery }
  599. tx86operand(operands[i]).opsize:=S_L;
  600. end;
  601. end;
  602. end
  603. else
  604. begin
  605. if opsize<>S_NO then
  606. tx86operand(operands[i]).opsize:=opsize
  607. end;
  608. end;
  609. OPR_SYMBOL :
  610. begin
  611. { Fix lea which need a reference }
  612. if opcode=A_LEA then
  613. begin
  614. s:=operands[i].opr.symbol;
  615. so:=operands[i].opr.symofs;
  616. operands[i].opr.typ:=OPR_REFERENCE;
  617. Fillchar(operands[i].opr.ref,sizeof(treference),0);
  618. operands[i].opr.ref.symbol:=s;
  619. operands[i].opr.ref.offset:=so;
  620. end;
  621. {$ifdef x86_64}
  622. tx86operand(operands[i]).opsize:=S_Q;
  623. {$else x86_64}
  624. tx86operand(operands[i]).opsize:=S_L;
  625. {$endif x86_64}
  626. end;
  627. end;
  628. end;
  629. end;
  630. end;
  631. procedure Tx86Instruction.SetInstructionOpsize;
  632. begin
  633. if opsize<>S_NO then
  634. exit;
  635. if (OpOrder=op_intel) then
  636. SwapOperands;
  637. case ops of
  638. 0 : ;
  639. 1 :
  640. begin
  641. { "push es" must be stored as a long PM }
  642. if ((opcode=A_PUSH) or
  643. (opcode=A_POP)) and
  644. (operands[1].opr.typ=OPR_REGISTER) and
  645. is_segment_reg(operands[1].opr.reg) then
  646. opsize:=S_L
  647. else
  648. opsize:=tx86operand(operands[1]).opsize;
  649. end;
  650. 2 :
  651. begin
  652. case opcode of
  653. A_MOVZX,A_MOVSX :
  654. begin
  655. if tx86operand(operands[1]).opsize=S_NO then
  656. begin
  657. tx86operand(operands[1]).opsize:=S_B;
  658. if (m_delphi in current_settings.modeswitches) then
  659. Message(asmr_w_unable_to_determine_reference_size_using_byte)
  660. else
  661. Message(asmr_e_unable_to_determine_reference_size);
  662. end;
  663. case tx86operand(operands[1]).opsize of
  664. S_W :
  665. case tx86operand(operands[2]).opsize of
  666. S_L :
  667. opsize:=S_WL;
  668. end;
  669. S_B :
  670. begin
  671. case tx86operand(operands[2]).opsize of
  672. S_W :
  673. opsize:=S_BW;
  674. S_L :
  675. opsize:=S_BL;
  676. end;
  677. end;
  678. end;
  679. end;
  680. A_MOVD : { movd is a move from a mmx register to a
  681. 32 bit register or memory, so no opsize is correct here PM }
  682. exit;
  683. A_MOVQ :
  684. opsize:=S_IQ;
  685. A_OUT :
  686. opsize:=tx86operand(operands[1]).opsize;
  687. else
  688. opsize:=tx86operand(operands[2]).opsize;
  689. end;
  690. end;
  691. 3,4 :
  692. opsize:=tx86operand(operands[ops]).opsize;
  693. end;
  694. end;
  695. procedure Tx86Instruction.CheckOperandSizes;
  696. var
  697. sizeerr : boolean;
  698. i : longint;
  699. begin
  700. { Check only the most common opcodes here, the others are done in
  701. the assembler pass }
  702. case opcode of
  703. A_PUSH,A_POP,A_DEC,A_INC,A_NOT,A_NEG,
  704. A_CMP,A_MOV,
  705. A_ADD,A_SUB,A_ADC,A_SBB,
  706. A_AND,A_OR,A_TEST,A_XOR: ;
  707. else
  708. exit;
  709. end;
  710. { Handle the BW,BL,WL separatly }
  711. sizeerr:=false;
  712. { special push/pop selector case }
  713. if ((opcode=A_PUSH) or
  714. (opcode=A_POP)) and
  715. (operands[1].opr.typ=OPR_REGISTER) and
  716. is_segment_reg(operands[1].opr.reg) then
  717. exit;
  718. if opsize in [S_BW,S_BL,S_WL] then
  719. begin
  720. if ops<>2 then
  721. sizeerr:=true
  722. else
  723. begin
  724. case opsize of
  725. S_BW :
  726. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_W);
  727. S_BL :
  728. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_L);
  729. S_WL :
  730. sizeerr:=(tx86operand(operands[1]).opsize<>S_W) or (tx86operand(operands[2]).opsize<>S_L);
  731. end;
  732. end;
  733. end
  734. else
  735. begin
  736. for i:=1 to ops do
  737. begin
  738. if (operands[i].opr.typ<>OPR_CONSTANT) and
  739. (tx86operand(operands[i]).opsize in [S_B,S_W,S_L]) and
  740. (tx86operand(operands[i]).opsize<>opsize) then
  741. sizeerr:=true;
  742. end;
  743. end;
  744. if sizeerr then
  745. begin
  746. { if range checks are on then generate an error }
  747. if (cs_compilesystem in current_settings.moduleswitches) or
  748. not (cs_check_range in current_settings.localswitches) then
  749. Message(asmr_w_size_suffix_and_dest_dont_match)
  750. else
  751. Message(asmr_e_size_suffix_and_dest_dont_match);
  752. end;
  753. end;
  754. { This check must be done with the operand in ATT order
  755. i.e.after swapping in the intel reader
  756. but before swapping in the NASM and TASM writers PM }
  757. procedure Tx86Instruction.CheckNonCommutativeOpcodes;
  758. begin
  759. if (OpOrder=op_intel) then
  760. SwapOperands;
  761. if (
  762. (ops=2) and
  763. (operands[1].opr.typ=OPR_REGISTER) and
  764. (operands[2].opr.typ=OPR_REGISTER) and
  765. { if the first is ST and the second is also a register
  766. it is necessarily ST1 .. ST7 }
  767. ((operands[1].opr.reg=NR_ST) or
  768. (operands[1].opr.reg=NR_ST0))
  769. ) or
  770. (ops=0) then
  771. if opcode=A_FSUBR then
  772. opcode:=A_FSUB
  773. else if opcode=A_FSUB then
  774. opcode:=A_FSUBR
  775. else if opcode=A_FDIVR then
  776. opcode:=A_FDIV
  777. else if opcode=A_FDIV then
  778. opcode:=A_FDIVR
  779. else if opcode=A_FSUBRP then
  780. opcode:=A_FSUBP
  781. else if opcode=A_FSUBP then
  782. opcode:=A_FSUBRP
  783. else if opcode=A_FDIVRP then
  784. opcode:=A_FDIVP
  785. else if opcode=A_FDIVP then
  786. opcode:=A_FDIVRP;
  787. if (
  788. (ops=1) and
  789. (operands[1].opr.typ=OPR_REGISTER) and
  790. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  791. (operands[1].opr.reg<>NR_ST) and
  792. (operands[1].opr.reg<>NR_ST0)
  793. ) then
  794. if opcode=A_FSUBRP then
  795. opcode:=A_FSUBP
  796. else if opcode=A_FSUBP then
  797. opcode:=A_FSUBRP
  798. else if opcode=A_FDIVRP then
  799. opcode:=A_FDIVP
  800. else if opcode=A_FDIVP then
  801. opcode:=A_FDIVRP;
  802. end;
  803. procedure Tx86Instruction.FixupOpcode;
  804. begin
  805. { does nothing by default }
  806. end;
  807. {*****************************************************************************
  808. opcode Adding
  809. *****************************************************************************}
  810. function Tx86Instruction.ConcatInstruction(p : TAsmList) : tai;
  811. var
  812. siz : topsize;
  813. i,asize : longint;
  814. ai : taicpu;
  815. begin
  816. if (OpOrder=op_intel) then
  817. SwapOperands;
  818. ai:=nil;
  819. for i:=1 to Ops do
  820. if not operands[i].CheckOperand then
  821. exit;
  822. { Get Opsize }
  823. if (opsize<>S_NO) or (Ops=0) then
  824. siz:=opsize
  825. else
  826. begin
  827. if (Ops=2) and (operands[1].opr.typ=OPR_REGISTER) then
  828. siz:=tx86operand(operands[1]).opsize
  829. else
  830. siz:=tx86operand(operands[Ops]).opsize;
  831. { MOVD should be of size S_LQ or S_QL, but these do not exist PM }
  832. if (ops=2) and
  833. (tx86operand(operands[1]).opsize<>S_NO) and
  834. (tx86operand(operands[2]).opsize<>S_NO) and
  835. (tx86operand(operands[1]).opsize<>tx86operand(operands[2]).opsize) then
  836. siz:=S_NO;
  837. end;
  838. if ((opcode=A_MOVD)or
  839. (opcode=A_CVTSI2SS)) and
  840. ((tx86operand(operands[1]).opsize=S_NO) or
  841. (tx86operand(operands[2]).opsize=S_NO)) then
  842. siz:=S_NO;
  843. { NASM does not support FADD without args
  844. as alias of FADDP
  845. and GNU AS interprets FADD without operand differently
  846. for version 2.9.1 and 2.9.5 !! }
  847. if (ops=0) and
  848. ((opcode=A_FADD) or
  849. (opcode=A_FMUL) or
  850. (opcode=A_FSUB) or
  851. (opcode=A_FSUBR) or
  852. (opcode=A_FDIV) or
  853. (opcode=A_FDIVR)) then
  854. begin
  855. if opcode=A_FADD then
  856. opcode:=A_FADDP
  857. else if opcode=A_FMUL then
  858. opcode:=A_FMULP
  859. else if opcode=A_FSUB then
  860. opcode:=A_FSUBP
  861. else if opcode=A_FSUBR then
  862. opcode:=A_FSUBRP
  863. else if opcode=A_FDIV then
  864. opcode:=A_FDIVP
  865. else if opcode=A_FDIVR then
  866. opcode:=A_FDIVRP;
  867. message1(asmr_w_fadd_to_faddp,std_op2str[opcode]);
  868. end;
  869. {It is valid to specify some instructions without operand size.}
  870. if siz=S_NO then
  871. begin
  872. if (ops=1) and (opcode=A_INT) then
  873. siz:=S_B;
  874. if (ops=1) and (opcode=A_RET) or (opcode=A_RETN) or (opcode=A_RETF) then
  875. siz:=S_W;
  876. if (ops=1) and (opcode=A_PUSH) then
  877. begin
  878. {We are a 32 compiler, assume 32-bit by default. This is Delphi
  879. compatible but bad coding practise.}
  880. siz:=S_L;
  881. message(asmr_w_unable_to_determine_reference_size_using_dword);
  882. end;
  883. if (opcode=A_JMP) or (opcode=A_JCC) or (opcode=A_CALL) then
  884. if ops=1 then
  885. siz:=S_NEAR
  886. else
  887. siz:=S_FAR;
  888. end;
  889. { GNU AS interprets FDIV without operand differently
  890. for version 2.9.1 and 2.10
  891. we add explicit args to it !! }
  892. if (ops=0) and
  893. ((opcode=A_FSUBP) or
  894. (opcode=A_FSUBRP) or
  895. (opcode=A_FDIVP) or
  896. (opcode=A_FDIVRP) or
  897. (opcode=A_FSUB) or
  898. (opcode=A_FSUBR) or
  899. (opcode=A_FADD) or
  900. (opcode=A_FADDP) or
  901. (opcode=A_FDIV) or
  902. (opcode=A_FDIVR)) then
  903. begin
  904. message1(asmr_w_adding_explicit_args_fXX,std_op2str[opcode]);
  905. ops:=2;
  906. operands[1].opr.typ:=OPR_REGISTER;
  907. operands[2].opr.typ:=OPR_REGISTER;
  908. operands[1].opr.reg:=NR_ST0;
  909. operands[2].opr.reg:=NR_ST1;
  910. end;
  911. if (ops=1) and
  912. (
  913. (operands[1].opr.typ=OPR_REGISTER) and
  914. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  915. (operands[1].opr.reg<>NR_ST) and
  916. (operands[1].opr.reg<>NR_ST0)
  917. ) and
  918. (
  919. (opcode=A_FSUBP) or
  920. (opcode=A_FSUBRP) or
  921. (opcode=A_FDIVP) or
  922. (opcode=A_FDIVRP) or
  923. (opcode=A_FADDP) or
  924. (opcode=A_FMULP)
  925. ) then
  926. begin
  927. message1(asmr_w_adding_explicit_first_arg_fXX,std_op2str[opcode]);
  928. ops:=2;
  929. operands[2].opr.typ:=OPR_REGISTER;
  930. operands[2].opr.reg:=operands[1].opr.reg;
  931. operands[1].opr.reg:=NR_ST0;
  932. end;
  933. if (ops=1) and
  934. (
  935. (operands[1].opr.typ=OPR_REGISTER) and
  936. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  937. (operands[1].opr.reg<>NR_ST) and
  938. (operands[1].opr.reg<>NR_ST0)
  939. ) and
  940. (
  941. (opcode=A_FSUB) or
  942. (opcode=A_FSUBR) or
  943. (opcode=A_FDIV) or
  944. (opcode=A_FDIVR) or
  945. (opcode=A_FADD) or
  946. (opcode=A_FMUL)
  947. ) then
  948. begin
  949. message1(asmr_w_adding_explicit_second_arg_fXX,std_op2str[opcode]);
  950. ops:=2;
  951. operands[2].opr.typ:=OPR_REGISTER;
  952. operands[2].opr.reg:=NR_ST0;
  953. end;
  954. { I tried to convince Linus Torvalds to add
  955. code to support ENTER instruction
  956. (when raising a stack page fault)
  957. but he replied that ENTER is a bad instruction and
  958. Linux does not need to support it
  959. So I think its at least a good idea to add a warning
  960. if someone uses this in assembler code
  961. FPC itself does not use it at all PM }
  962. if (opcode=A_ENTER) and
  963. (target_info.system in [system_i386_linux,system_i386_FreeBSD]) then
  964. Message(asmr_w_enter_not_supported_by_linux);
  965. ai:=taicpu.op_none(opcode,siz);
  966. ai.fileinfo:=filepos;
  967. ai.SetOperandOrder(OpOrder);
  968. ai.Ops:=Ops;
  969. ai.Allocate_oper(Ops);
  970. for i:=1 to Ops do
  971. case operands[i].opr.typ of
  972. OPR_CONSTANT :
  973. ai.loadconst(i-1,operands[i].opr.val);
  974. OPR_REGISTER:
  975. ai.loadreg(i-1,operands[i].opr.reg);
  976. OPR_SYMBOL:
  977. ai.loadsymbol(i-1,operands[i].opr.symbol,operands[i].opr.symofs);
  978. OPR_LOCAL :
  979. with operands[i].opr do
  980. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  981. localscale,localgetoffset,localforceref);
  982. OPR_REFERENCE:
  983. begin
  984. ai.loadref(i-1,operands[i].opr.ref);
  985. if operands[i].size<>OS_NO then
  986. begin
  987. asize:=0;
  988. case operands[i].size of
  989. OS_8,OS_S8 :
  990. asize:=OT_BITS8;
  991. OS_16,OS_S16 :
  992. asize:=OT_BITS16;
  993. OS_32,OS_S32,OS_F32,OS_M32 :
  994. asize:=OT_BITS32;
  995. OS_64,OS_S64:
  996. begin
  997. { Only FPU operations know about 64bit values, for all
  998. integer operations it is seen as 32bit
  999. this applies only to i386, see tw16622}
  1000. if gas_needsuffix[opcode] in [attsufFPU,attsufFPUint] then
  1001. asize:=OT_BITS64
  1002. {$ifdef i386}
  1003. else
  1004. asize:=OT_BITS32
  1005. {$endif i386}
  1006. ;
  1007. end;
  1008. OS_F64,OS_C64, OS_M64 :
  1009. asize:=OT_BITS64;
  1010. OS_F80 :
  1011. asize:=OT_BITS80;
  1012. OS_128,OS_M128,OS_MS128:
  1013. asize := OT_BITS128;
  1014. OS_M256,OS_MS256:
  1015. asize := OT_BITS256;
  1016. end;
  1017. if asize<>0 then
  1018. ai.oper[i-1]^.ot:=(ai.oper[i-1]^.ot and not OT_SIZE_MASK) or asize;
  1019. end;
  1020. end;
  1021. end;
  1022. { Condition ? }
  1023. if condition<>C_None then
  1024. ai.SetCondition(condition);
  1025. { Set is_jmp, it enables asmwriter to emit short jumps if appropriate }
  1026. if (opcode=A_JMP) or (opcode=A_JCC) then
  1027. ai.is_jmp := True;
  1028. { Concat the opcode or give an error }
  1029. if assigned(ai) then
  1030. p.concat(ai)
  1031. else
  1032. Message(asmr_e_invalid_opcode_and_operand);
  1033. result:=ai;
  1034. end;
  1035. end.