rax86.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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. { opcode adding }
  50. function ConcatInstruction(p : TAsmList) : tai;override;
  51. end;
  52. const
  53. AsmPrefixes = 6;
  54. AsmPrefix : array[0..AsmPrefixes-1] of TasmOP =(
  55. A_LOCK,A_REP,A_REPE,A_REPNE,A_REPNZ,A_REPZ
  56. );
  57. AsmOverrides = 6;
  58. AsmOverride : array[0..AsmOverrides-1] of TasmOP =(
  59. A_SEGCS,A_SEGES,A_SEGDS,A_SEGFS,A_SEGGS,A_SEGSS
  60. );
  61. CondAsmOps=3;
  62. CondAsmOp:array[0..CondAsmOps-1] of TasmOp=(
  63. A_CMOVcc, A_Jcc, A_SETcc
  64. );
  65. CondAsmOpStr:array[0..CondAsmOps-1] of string[4]=(
  66. 'CMOV','J','SET'
  67. );
  68. implementation
  69. uses
  70. globtype,globals,systems,verbose,
  71. procinfo,
  72. cpuinfo,cgbase,cgutils,
  73. itcpugas,cgx86;
  74. {*****************************************************************************
  75. Parser Helpers
  76. *****************************************************************************}
  77. function is_prefix(t:tasmop):boolean;
  78. var
  79. i : longint;
  80. Begin
  81. is_prefix:=false;
  82. for i:=1 to AsmPrefixes do
  83. if t=AsmPrefix[i-1] then
  84. begin
  85. is_prefix:=true;
  86. exit;
  87. end;
  88. end;
  89. function is_override(t:tasmop):boolean;
  90. var
  91. i : longint;
  92. Begin
  93. is_override:=false;
  94. for i:=1 to AsmOverrides do
  95. if t=AsmOverride[i-1] then
  96. begin
  97. is_override:=true;
  98. exit;
  99. end;
  100. end;
  101. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  102. { Checks if the prefix is valid with the following opcode }
  103. { return false if not, otherwise true }
  104. Begin
  105. CheckPrefix := TRUE;
  106. (* Case prefix of
  107. A_REP,A_REPNE,A_REPE:
  108. Case opcode Of
  109. A_SCASB,A_SCASW,A_SCASD,
  110. A_INS,A_OUTS,A_MOVS,A_CMPS,A_LODS,A_STOS:;
  111. Else
  112. Begin
  113. CheckPrefix := FALSE;
  114. exit;
  115. end;
  116. end; { case }
  117. A_LOCK:
  118. Case opcode Of
  119. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,A_ADC,A_SBB,A_AND,A_SUB,
  120. A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  121. Else
  122. Begin
  123. CheckPrefix := FALSE;
  124. Exit;
  125. end;
  126. end; { case }
  127. A_NONE: exit; { no prefix here }
  128. else
  129. CheckPrefix := FALSE;
  130. end; { end case } *)
  131. end;
  132. Function CheckOverride(overrideop,op:tasmop): Boolean;
  133. { Check if the override is valid, and if so then }
  134. { update the instr variable accordingly. }
  135. Begin
  136. CheckOverride := true;
  137. { Case instr.getinstruction of
  138. A_MOVS,A_XLAT,A_CMPS:
  139. Begin
  140. CheckOverride := TRUE;
  141. Message(assem_e_segment_override_not_supported);
  142. end
  143. end }
  144. end;
  145. Procedure FWaitWarning;
  146. begin
  147. if (target_info.system=system_i386_GO32V2) and (cs_fp_emulation in current_settings.moduleswitches) then
  148. Message(asmr_w_fwait_emu_prob);
  149. end;
  150. {*****************************************************************************
  151. TX86Operand
  152. *****************************************************************************}
  153. Procedure Tx86Operand.SetSize(_size:longint;force:boolean);
  154. begin
  155. inherited SetSize(_size,force);
  156. { OS_64 will be set to S_L and be fixed later
  157. in SetCorrectSize }
  158. opsize:=TCGSize2Opsize[size];
  159. end;
  160. Procedure Tx86Operand.SetCorrectSize(opcode:tasmop);
  161. begin
  162. if gas_needsuffix[opcode]=attsufFPU then
  163. begin
  164. case size of
  165. OS_32 : opsize:=S_FS;
  166. OS_64 : opsize:=S_FL;
  167. end;
  168. end
  169. else if gas_needsuffix[opcode]=attsufFPUint then
  170. begin
  171. case size of
  172. OS_16 : opsize:=S_IS;
  173. OS_32 : opsize:=S_IL;
  174. OS_64 : opsize:=S_IQ;
  175. end;
  176. end
  177. else
  178. begin
  179. if size=OS_64 then
  180. opsize:=S_Q;
  181. end;
  182. end;
  183. Function Tx86Operand.CheckOperand: boolean;
  184. begin
  185. result:=true;
  186. if (opr.typ=OPR_Reference) then
  187. begin
  188. if not hasvar then
  189. begin
  190. if (getsupreg(opr.ref.base)=RS_EBP) and (opr.ref.offset>0) then
  191. begin
  192. if current_procinfo.procdef.proccalloption=pocall_register then
  193. message(asmr_w_no_direct_ebp_for_parameter)
  194. else
  195. message(asmr_w_direct_ebp_for_parameter_regcall);
  196. end
  197. else if (getsupreg(opr.ref.base)=RS_EBP) and (opr.ref.offset<0) then
  198. message(asmr_w_direct_ebp_neg_offset)
  199. else if (getsupreg(opr.ref.base)=RS_ESP) and (opr.ref.offset<0) then
  200. message(asmr_w_direct_esp_neg_offset);
  201. end;
  202. if (cs_create_pic in current_settings.moduleswitches) and
  203. assigned(opr.ref.symbol) and
  204. not assigned(opr.ref.relsymbol) and
  205. not(opr.ref.refaddr in [addr_pic,addr_pic_no_got]) then
  206. begin
  207. if (opr.ref.symbol.name <> '_GLOBAL_OFFSET_TABLE_') then
  208. begin
  209. message(asmr_e_need_pic_ref);
  210. result:=false;
  211. end
  212. else
  213. opr.ref.refaddr:=addr_pic;
  214. end;
  215. end;
  216. end;
  217. {*****************************************************************************
  218. T386Instruction
  219. *****************************************************************************}
  220. constructor Tx86Instruction.Create(optype : tcoperand);
  221. begin
  222. inherited Create(optype);
  223. Opsize:=S_NO;
  224. end;
  225. procedure Tx86Instruction.SwapOperands;
  226. begin
  227. Inherited SwapOperands;
  228. { mark the correct order }
  229. if OpOrder=op_intel then
  230. OpOrder:=op_att
  231. else
  232. OpOrder:=op_intel;
  233. end;
  234. procedure Tx86Instruction.AddReferenceSizes;
  235. { this will add the sizes for references like [esi] which do not
  236. have the size set yet, it will take only the size if the other
  237. operand is a register }
  238. var
  239. operand2,i : longint;
  240. s : tasmsymbol;
  241. so : aint;
  242. begin
  243. for i:=1 to ops do
  244. begin
  245. operands[i].SetCorrectSize(opcode);
  246. if tx86operand(operands[i]).opsize=S_NO then
  247. begin
  248. {$ifdef x86_64}
  249. if (opcode=A_MOVQ) and
  250. (ops=2) and
  251. (operands[1].opr.typ=OPR_CONSTANT) then
  252. opsize:=S_Q
  253. else
  254. {$endif x86_64}
  255. case operands[i].Opr.Typ of
  256. OPR_LOCAL,
  257. OPR_REFERENCE :
  258. begin
  259. { for 3-operand opcodes, operand #1 (in ATT order) is always an immediate,
  260. don't consider it. }
  261. if i=ops then
  262. operand2:=i-1
  263. else
  264. operand2:=i+1;
  265. if operand2>0 then
  266. begin
  267. { Only allow register as operand to take the size from }
  268. if operands[operand2].opr.typ=OPR_REGISTER then
  269. begin
  270. if ((opcode<>A_MOVD) and
  271. (opcode<>A_CVTSI2SS)) then
  272. tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize;
  273. end
  274. else
  275. begin
  276. { if no register then take the opsize (which is available with ATT),
  277. if not availble then give an error }
  278. if opsize<>S_NO then
  279. tx86operand(operands[i]).opsize:=opsize
  280. else
  281. begin
  282. if (m_delphi in current_settings.modeswitches) then
  283. Message(asmr_w_unable_to_determine_reference_size_using_dword)
  284. else
  285. Message(asmr_e_unable_to_determine_reference_size);
  286. { recovery }
  287. tx86operand(operands[i]).opsize:=S_L;
  288. end;
  289. end;
  290. end
  291. else
  292. begin
  293. if opsize<>S_NO then
  294. tx86operand(operands[i]).opsize:=opsize
  295. end;
  296. end;
  297. OPR_SYMBOL :
  298. begin
  299. { Fix lea which need a reference }
  300. if opcode=A_LEA then
  301. begin
  302. s:=operands[i].opr.symbol;
  303. so:=operands[i].opr.symofs;
  304. operands[i].opr.typ:=OPR_REFERENCE;
  305. Fillchar(operands[i].opr.ref,sizeof(treference),0);
  306. operands[i].opr.ref.symbol:=s;
  307. operands[i].opr.ref.offset:=so;
  308. end;
  309. {$ifdef x86_64}
  310. tx86operand(operands[i]).opsize:=S_Q;
  311. {$else x86_64}
  312. tx86operand(operands[i]).opsize:=S_L;
  313. {$endif x86_64}
  314. end;
  315. end;
  316. end;
  317. end;
  318. end;
  319. procedure Tx86Instruction.SetInstructionOpsize;
  320. begin
  321. if opsize<>S_NO then
  322. exit;
  323. if (OpOrder=op_intel) then
  324. SwapOperands;
  325. case ops of
  326. 0 : ;
  327. 1 :
  328. begin
  329. { "push es" must be stored as a long PM }
  330. if ((opcode=A_PUSH) or
  331. (opcode=A_POP)) and
  332. (operands[1].opr.typ=OPR_REGISTER) and
  333. is_segment_reg(operands[1].opr.reg) then
  334. opsize:=S_L
  335. else
  336. opsize:=tx86operand(operands[1]).opsize;
  337. end;
  338. 2 :
  339. begin
  340. case opcode of
  341. A_MOVZX,A_MOVSX :
  342. begin
  343. if tx86operand(operands[1]).opsize=S_NO then
  344. begin
  345. tx86operand(operands[1]).opsize:=S_B;
  346. if (m_delphi in current_settings.modeswitches) then
  347. Message(asmr_w_unable_to_determine_reference_size_using_byte)
  348. else
  349. Message(asmr_e_unable_to_determine_reference_size);
  350. end;
  351. case tx86operand(operands[1]).opsize of
  352. S_W :
  353. case tx86operand(operands[2]).opsize of
  354. S_L :
  355. opsize:=S_WL;
  356. end;
  357. S_B :
  358. begin
  359. case tx86operand(operands[2]).opsize of
  360. S_W :
  361. opsize:=S_BW;
  362. S_L :
  363. opsize:=S_BL;
  364. end;
  365. end;
  366. end;
  367. end;
  368. A_MOVD : { movd is a move from a mmx register to a
  369. 32 bit register or memory, so no opsize is correct here PM }
  370. exit;
  371. A_MOVQ :
  372. opsize:=S_IQ;
  373. A_OUT :
  374. opsize:=tx86operand(operands[1]).opsize;
  375. else
  376. opsize:=tx86operand(operands[2]).opsize;
  377. end;
  378. end;
  379. 3 :
  380. opsize:=tx86operand(operands[3]).opsize;
  381. end;
  382. end;
  383. procedure Tx86Instruction.CheckOperandSizes;
  384. var
  385. sizeerr : boolean;
  386. i : longint;
  387. begin
  388. { Check only the most common opcodes here, the others are done in
  389. the assembler pass }
  390. case opcode of
  391. A_PUSH,A_POP,A_DEC,A_INC,A_NOT,A_NEG,
  392. A_CMP,A_MOV,
  393. A_ADD,A_SUB,A_ADC,A_SBB,
  394. A_AND,A_OR,A_TEST,A_XOR: ;
  395. else
  396. exit;
  397. end;
  398. { Handle the BW,BL,WL separatly }
  399. sizeerr:=false;
  400. { special push/pop selector case }
  401. if ((opcode=A_PUSH) or
  402. (opcode=A_POP)) and
  403. (operands[1].opr.typ=OPR_REGISTER) and
  404. is_segment_reg(operands[1].opr.reg) then
  405. exit;
  406. if opsize in [S_BW,S_BL,S_WL] then
  407. begin
  408. if ops<>2 then
  409. sizeerr:=true
  410. else
  411. begin
  412. case opsize of
  413. S_BW :
  414. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_W);
  415. S_BL :
  416. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_L);
  417. S_WL :
  418. sizeerr:=(tx86operand(operands[1]).opsize<>S_W) or (tx86operand(operands[2]).opsize<>S_L);
  419. end;
  420. end;
  421. end
  422. else
  423. begin
  424. for i:=1 to ops do
  425. begin
  426. if (operands[i].opr.typ<>OPR_CONSTANT) and
  427. (tx86operand(operands[i]).opsize in [S_B,S_W,S_L]) and
  428. (tx86operand(operands[i]).opsize<>opsize) then
  429. sizeerr:=true;
  430. end;
  431. end;
  432. if sizeerr then
  433. begin
  434. { if range checks are on then generate an error }
  435. if (cs_compilesystem in current_settings.moduleswitches) or
  436. not (cs_check_range in current_settings.localswitches) then
  437. Message(asmr_w_size_suffix_and_dest_dont_match)
  438. else
  439. Message(asmr_e_size_suffix_and_dest_dont_match);
  440. end;
  441. end;
  442. { This check must be done with the operand in ATT order
  443. i.e.after swapping in the intel reader
  444. but before swapping in the NASM and TASM writers PM }
  445. procedure Tx86Instruction.CheckNonCommutativeOpcodes;
  446. begin
  447. if (OpOrder=op_intel) then
  448. SwapOperands;
  449. if (
  450. (ops=2) and
  451. (operands[1].opr.typ=OPR_REGISTER) and
  452. (operands[2].opr.typ=OPR_REGISTER) and
  453. { if the first is ST and the second is also a register
  454. it is necessarily ST1 .. ST7 }
  455. ((operands[1].opr.reg=NR_ST) or
  456. (operands[1].opr.reg=NR_ST0))
  457. ) or
  458. (ops=0) then
  459. if opcode=A_FSUBR then
  460. opcode:=A_FSUB
  461. else if opcode=A_FSUB then
  462. opcode:=A_FSUBR
  463. else if opcode=A_FDIVR then
  464. opcode:=A_FDIV
  465. else if opcode=A_FDIV then
  466. opcode:=A_FDIVR
  467. else if opcode=A_FSUBRP then
  468. opcode:=A_FSUBP
  469. else if opcode=A_FSUBP then
  470. opcode:=A_FSUBRP
  471. else if opcode=A_FDIVRP then
  472. opcode:=A_FDIVP
  473. else if opcode=A_FDIVP then
  474. opcode:=A_FDIVRP;
  475. if (
  476. (ops=1) and
  477. (operands[1].opr.typ=OPR_REGISTER) and
  478. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  479. (operands[1].opr.reg<>NR_ST) and
  480. (operands[1].opr.reg<>NR_ST0)
  481. ) then
  482. if opcode=A_FSUBRP then
  483. opcode:=A_FSUBP
  484. else if opcode=A_FSUBP then
  485. opcode:=A_FSUBRP
  486. else if opcode=A_FDIVRP then
  487. opcode:=A_FDIVP
  488. else if opcode=A_FDIVP then
  489. opcode:=A_FDIVRP;
  490. end;
  491. {*****************************************************************************
  492. opcode Adding
  493. *****************************************************************************}
  494. function Tx86Instruction.ConcatInstruction(p : TAsmList) : tai;
  495. var
  496. siz : topsize;
  497. i,asize : longint;
  498. ai : taicpu;
  499. begin
  500. if (OpOrder=op_intel) then
  501. SwapOperands;
  502. ai:=nil;
  503. for i:=1 to Ops do
  504. if not operands[i].CheckOperand then
  505. exit;
  506. { Get Opsize }
  507. if (opsize<>S_NO) or (Ops=0) then
  508. siz:=opsize
  509. else
  510. begin
  511. if (Ops=2) and (operands[1].opr.typ=OPR_REGISTER) then
  512. siz:=tx86operand(operands[1]).opsize
  513. else
  514. siz:=tx86operand(operands[Ops]).opsize;
  515. { MOVD should be of size S_LQ or S_QL, but these do not exist PM }
  516. if (ops=2) and
  517. (tx86operand(operands[1]).opsize<>S_NO) and
  518. (tx86operand(operands[2]).opsize<>S_NO) and
  519. (tx86operand(operands[1]).opsize<>tx86operand(operands[2]).opsize) then
  520. siz:=S_NO;
  521. end;
  522. if ((opcode=A_MOVD)or
  523. (opcode=A_CVTSI2SS)) and
  524. ((tx86operand(operands[1]).opsize=S_NO) or
  525. (tx86operand(operands[2]).opsize=S_NO)) then
  526. siz:=S_NO;
  527. { NASM does not support FADD without args
  528. as alias of FADDP
  529. and GNU AS interprets FADD without operand differently
  530. for version 2.9.1 and 2.9.5 !! }
  531. if (ops=0) and
  532. ((opcode=A_FADD) or
  533. (opcode=A_FMUL) or
  534. (opcode=A_FSUB) or
  535. (opcode=A_FSUBR) or
  536. (opcode=A_FDIV) or
  537. (opcode=A_FDIVR)) then
  538. begin
  539. if opcode=A_FADD then
  540. opcode:=A_FADDP
  541. else if opcode=A_FMUL then
  542. opcode:=A_FMULP
  543. else if opcode=A_FSUB then
  544. opcode:=A_FSUBP
  545. else if opcode=A_FSUBR then
  546. opcode:=A_FSUBRP
  547. else if opcode=A_FDIV then
  548. opcode:=A_FDIVP
  549. else if opcode=A_FDIVR then
  550. opcode:=A_FDIVRP;
  551. message1(asmr_w_fadd_to_faddp,std_op2str[opcode]);
  552. end;
  553. {It is valid to specify some instructions without operand size.}
  554. if siz=S_NO then
  555. begin
  556. if (ops=1) and (opcode=A_INT) then
  557. siz:=S_B;
  558. if (ops=1) and (opcode=A_RET) or (opcode=A_RETN) or (opcode=A_RETF) then
  559. siz:=S_W;
  560. if (ops=1) and (opcode=A_PUSH) then
  561. begin
  562. {We are a 32 compiler, assume 32-bit by default. This is Delphi
  563. compatible but bad coding practise.}
  564. siz:=S_L;
  565. message(asmr_w_unable_to_determine_reference_size_using_dword);
  566. end;
  567. if (opcode=A_JMP) or (opcode=A_JCC) or (opcode=A_CALL) then
  568. if ops=1 then
  569. siz:=S_NEAR
  570. else
  571. siz:=S_FAR;
  572. end;
  573. {$ifdef x86_64}
  574. { Convert movq with at least one general registers or constant to a mov instruction }
  575. if (opcode=A_MOVQ) and
  576. (ops=2) and
  577. (
  578. (operands[1].opr.typ=OPR_REGISTER) or
  579. (operands[2].opr.typ=OPR_REGISTER) or
  580. (operands[1].opr.typ=OPR_CONSTANT)
  581. ) then
  582. opcode:=A_MOV;
  583. {$endif x86_64}
  584. { GNU AS interprets FDIV without operand differently
  585. for version 2.9.1 and 2.10
  586. we add explicit args to it !! }
  587. if (ops=0) and
  588. ((opcode=A_FSUBP) or
  589. (opcode=A_FSUBRP) or
  590. (opcode=A_FDIVP) or
  591. (opcode=A_FDIVRP) or
  592. (opcode=A_FSUB) or
  593. (opcode=A_FSUBR) or
  594. (opcode=A_FADD) or
  595. (opcode=A_FADDP) or
  596. (opcode=A_FDIV) or
  597. (opcode=A_FDIVR)) then
  598. begin
  599. message1(asmr_w_adding_explicit_args_fXX,std_op2str[opcode]);
  600. ops:=2;
  601. operands[1].opr.typ:=OPR_REGISTER;
  602. operands[2].opr.typ:=OPR_REGISTER;
  603. operands[1].opr.reg:=NR_ST0;
  604. operands[2].opr.reg:=NR_ST1;
  605. end;
  606. if (ops=1) and
  607. (
  608. (operands[1].opr.typ=OPR_REGISTER) and
  609. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  610. (operands[1].opr.reg<>NR_ST) and
  611. (operands[1].opr.reg<>NR_ST0)
  612. ) and
  613. (
  614. (opcode=A_FSUBP) or
  615. (opcode=A_FSUBRP) or
  616. (opcode=A_FDIVP) or
  617. (opcode=A_FDIVRP) or
  618. (opcode=A_FADDP) or
  619. (opcode=A_FMULP)
  620. ) then
  621. begin
  622. message1(asmr_w_adding_explicit_first_arg_fXX,std_op2str[opcode]);
  623. ops:=2;
  624. operands[2].opr.typ:=OPR_REGISTER;
  625. operands[2].opr.reg:=operands[1].opr.reg;
  626. operands[1].opr.reg:=NR_ST0;
  627. end;
  628. if (ops=1) and
  629. (
  630. (operands[1].opr.typ=OPR_REGISTER) and
  631. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  632. (operands[1].opr.reg<>NR_ST) and
  633. (operands[1].opr.reg<>NR_ST0)
  634. ) and
  635. (
  636. (opcode=A_FSUB) or
  637. (opcode=A_FSUBR) or
  638. (opcode=A_FDIV) or
  639. (opcode=A_FDIVR) or
  640. (opcode=A_FADD) or
  641. (opcode=A_FMUL)
  642. ) then
  643. begin
  644. message1(asmr_w_adding_explicit_second_arg_fXX,std_op2str[opcode]);
  645. ops:=2;
  646. operands[2].opr.typ:=OPR_REGISTER;
  647. operands[2].opr.reg:=NR_ST0;
  648. end;
  649. { I tried to convince Linus Torvalds to add
  650. code to support ENTER instruction
  651. (when raising a stack page fault)
  652. but he replied that ENTER is a bad instruction and
  653. Linux does not need to support it
  654. So I think its at least a good idea to add a warning
  655. if someone uses this in assembler code
  656. FPC itself does not use it at all PM }
  657. if (opcode=A_ENTER) and
  658. (target_info.system in [system_i386_linux,system_i386_FreeBSD]) then
  659. Message(asmr_w_enter_not_supported_by_linux);
  660. ai:=taicpu.op_none(opcode,siz);
  661. ai.SetOperandOrder(OpOrder);
  662. ai.Ops:=Ops;
  663. ai.Allocate_oper(Ops);
  664. for i:=1 to Ops do
  665. case operands[i].opr.typ of
  666. OPR_CONSTANT :
  667. ai.loadconst(i-1,operands[i].opr.val);
  668. OPR_REGISTER:
  669. ai.loadreg(i-1,operands[i].opr.reg);
  670. OPR_SYMBOL:
  671. ai.loadsymbol(i-1,operands[i].opr.symbol,operands[i].opr.symofs);
  672. OPR_LOCAL :
  673. with operands[i].opr do
  674. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  675. localscale,localgetoffset,localforceref);
  676. OPR_REFERENCE:
  677. begin
  678. ai.loadref(i-1,operands[i].opr.ref);
  679. if operands[i].size<>OS_NO then
  680. begin
  681. asize:=0;
  682. case operands[i].size of
  683. OS_8,OS_S8 :
  684. asize:=OT_BITS8;
  685. OS_16,OS_S16 :
  686. asize:=OT_BITS16;
  687. OS_32,OS_S32,OS_F32 :
  688. asize:=OT_BITS32;
  689. OS_64,OS_S64:
  690. begin
  691. { Only FPU operations know about 64bit values, for all
  692. integer operations it is seen as 32bit
  693. this applies only to i386, see tw16622}
  694. if gas_needsuffix[opcode] in [attsufFPU,attsufFPUint] then
  695. asize:=OT_BITS64
  696. {$ifdef i386}
  697. else
  698. asize:=OT_BITS32
  699. {$endif i386}
  700. ;
  701. end;
  702. OS_F64,OS_C64 :
  703. asize:=OT_BITS64;
  704. OS_F80 :
  705. asize:=OT_BITS80;
  706. end;
  707. if asize<>0 then
  708. ai.oper[i-1]^.ot:=(ai.oper[i-1]^.ot and not OT_SIZE_MASK) or asize;
  709. end;
  710. end;
  711. end;
  712. { Condition ? }
  713. if condition<>C_None then
  714. ai.SetCondition(condition);
  715. { Set is_jmp, it enables asmwriter to emit short jumps if appropriate }
  716. if (opcode=A_JMP) or (opcode=A_JCC) then
  717. ai.is_jmp := True;
  718. { Concat the opcode or give an error }
  719. if assigned(ai) then
  720. p.concat(ai)
  721. else
  722. Message(asmr_e_invalid_opcode_and_operand);
  723. result:=ai;
  724. end;
  725. end.