rax86.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. end;
  178. Function Tx86Operand.CheckOperand: boolean;
  179. begin
  180. result:=true;
  181. if (opr.typ=OPR_Reference) then
  182. begin
  183. if not hasvar then
  184. begin
  185. if (getsupreg(opr.ref.base)=RS_EBP) and (opr.ref.offset>0) then
  186. begin
  187. if current_procinfo.procdef.proccalloption=pocall_register then
  188. message(asmr_w_no_direct_ebp_for_parameter)
  189. else
  190. message(asmr_w_direct_ebp_for_parameter_regcall);
  191. end
  192. else if (getsupreg(opr.ref.base)=RS_EBP) and (opr.ref.offset<0) then
  193. message(asmr_w_direct_ebp_neg_offset)
  194. else if (getsupreg(opr.ref.base)=RS_ESP) and (opr.ref.offset<0) then
  195. message(asmr_w_direct_esp_neg_offset);
  196. end;
  197. if (cs_create_pic in current_settings.moduleswitches) and
  198. assigned(opr.ref.symbol) and
  199. not assigned(opr.ref.relsymbol) and
  200. (opr.ref.refaddr<>addr_pic) then
  201. begin
  202. message(asmr_e_need_pic_ref);
  203. result:=false;
  204. end;
  205. end;
  206. end;
  207. {*****************************************************************************
  208. T386Instruction
  209. *****************************************************************************}
  210. constructor Tx86Instruction.Create(optype : tcoperand);
  211. begin
  212. inherited Create(optype);
  213. Opsize:=S_NO;
  214. end;
  215. procedure Tx86Instruction.SwapOperands;
  216. begin
  217. Inherited SwapOperands;
  218. { mark the correct order }
  219. if OpOrder=op_intel then
  220. OpOrder:=op_att
  221. else
  222. OpOrder:=op_intel;
  223. end;
  224. procedure Tx86Instruction.AddReferenceSizes;
  225. { this will add the sizes for references like [esi] which do not
  226. have the size set yet, it will take only the size if the other
  227. operand is a register }
  228. var
  229. operand2,i : longint;
  230. s : tasmsymbol;
  231. so : aint;
  232. begin
  233. for i:=1 to ops do
  234. begin
  235. operands[i].SetCorrectSize(opcode);
  236. if tx86operand(operands[i]).opsize=S_NO then
  237. begin
  238. {$ifdef x86_64}
  239. if (opcode=A_MOVQ) and
  240. (ops=2) and
  241. (operands[1].opr.typ=OPR_CONSTANT) then
  242. opsize:=S_Q
  243. else
  244. {$endif x86_64}
  245. case operands[i].Opr.Typ of
  246. OPR_LOCAL,
  247. OPR_REFERENCE :
  248. begin
  249. if i=2 then
  250. operand2:=1
  251. else
  252. operand2:=2;
  253. if operand2<ops then
  254. begin
  255. { Only allow register as operand to take the size from }
  256. if operands[operand2].opr.typ=OPR_REGISTER then
  257. begin
  258. if ((opcode<>A_MOVD) and
  259. (opcode<>A_CVTSI2SS)) then
  260. tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize;
  261. end
  262. else
  263. begin
  264. { if no register then take the opsize (which is available with ATT),
  265. if not availble then give an error }
  266. if opsize<>S_NO then
  267. tx86operand(operands[i]).opsize:=opsize
  268. else
  269. begin
  270. if (m_delphi in current_settings.modeswitches) then
  271. Message(asmr_w_unable_to_determine_reference_size_using_dword)
  272. else
  273. Message(asmr_e_unable_to_determine_reference_size);
  274. { recovery }
  275. tx86operand(operands[i]).opsize:=S_L;
  276. end;
  277. end;
  278. end
  279. else
  280. begin
  281. if opsize<>S_NO then
  282. tx86operand(operands[i]).opsize:=opsize
  283. end;
  284. end;
  285. OPR_SYMBOL :
  286. begin
  287. { Fix lea which need a reference }
  288. if opcode=A_LEA then
  289. begin
  290. s:=operands[i].opr.symbol;
  291. so:=operands[i].opr.symofs;
  292. operands[i].opr.typ:=OPR_REFERENCE;
  293. Fillchar(operands[i].opr.ref,sizeof(treference),0);
  294. operands[i].opr.ref.symbol:=s;
  295. operands[i].opr.ref.offset:=so;
  296. end;
  297. {$ifdef x86_64}
  298. tx86operand(operands[i]).opsize:=S_Q;
  299. {$else x86_64}
  300. tx86operand(operands[i]).opsize:=S_L;
  301. {$endif x86_64}
  302. end;
  303. end;
  304. end;
  305. end;
  306. end;
  307. procedure Tx86Instruction.SetInstructionOpsize;
  308. begin
  309. if opsize<>S_NO then
  310. exit;
  311. if (OpOrder=op_intel) then
  312. SwapOperands;
  313. case ops of
  314. 0 : ;
  315. 1 :
  316. begin
  317. { "push es" must be stored as a long PM }
  318. if ((opcode=A_PUSH) or
  319. (opcode=A_POP)) and
  320. (operands[1].opr.typ=OPR_REGISTER) and
  321. is_segment_reg(operands[1].opr.reg) then
  322. opsize:=S_L
  323. else
  324. opsize:=tx86operand(operands[1]).opsize;
  325. end;
  326. 2 :
  327. begin
  328. case opcode of
  329. A_MOVZX,A_MOVSX :
  330. begin
  331. if tx86operand(operands[1]).opsize=S_NO then
  332. begin
  333. tx86operand(operands[1]).opsize:=S_B;
  334. if (m_delphi in current_settings.modeswitches) then
  335. Message(asmr_w_unable_to_determine_reference_size_using_byte)
  336. else
  337. Message(asmr_e_unable_to_determine_reference_size);
  338. end;
  339. case tx86operand(operands[1]).opsize of
  340. S_W :
  341. case tx86operand(operands[2]).opsize of
  342. S_L :
  343. opsize:=S_WL;
  344. end;
  345. S_B :
  346. begin
  347. case tx86operand(operands[2]).opsize of
  348. S_W :
  349. opsize:=S_BW;
  350. S_L :
  351. opsize:=S_BL;
  352. end;
  353. end;
  354. end;
  355. end;
  356. A_MOVD : { movd is a move from a mmx register to a
  357. 32 bit register or memory, so no opsize is correct here PM }
  358. exit;
  359. A_MOVQ :
  360. opsize:=S_IQ;
  361. A_OUT :
  362. opsize:=tx86operand(operands[1]).opsize;
  363. else
  364. opsize:=tx86operand(operands[2]).opsize;
  365. end;
  366. end;
  367. 3 :
  368. opsize:=tx86operand(operands[3]).opsize;
  369. end;
  370. end;
  371. procedure Tx86Instruction.CheckOperandSizes;
  372. var
  373. sizeerr : boolean;
  374. i : longint;
  375. begin
  376. { Check only the most common opcodes here, the others are done in
  377. the assembler pass }
  378. case opcode of
  379. A_PUSH,A_POP,A_DEC,A_INC,A_NOT,A_NEG,
  380. A_CMP,A_MOV,
  381. A_ADD,A_SUB,A_ADC,A_SBB,
  382. A_AND,A_OR,A_TEST,A_XOR: ;
  383. else
  384. exit;
  385. end;
  386. { Handle the BW,BL,WL separatly }
  387. sizeerr:=false;
  388. { special push/pop selector case }
  389. if ((opcode=A_PUSH) or
  390. (opcode=A_POP)) and
  391. (operands[1].opr.typ=OPR_REGISTER) and
  392. is_segment_reg(operands[1].opr.reg) then
  393. exit;
  394. if opsize in [S_BW,S_BL,S_WL] then
  395. begin
  396. if ops<>2 then
  397. sizeerr:=true
  398. else
  399. begin
  400. case opsize of
  401. S_BW :
  402. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_W);
  403. S_BL :
  404. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_L);
  405. S_WL :
  406. sizeerr:=(tx86operand(operands[1]).opsize<>S_W) or (tx86operand(operands[2]).opsize<>S_L);
  407. end;
  408. end;
  409. end
  410. else
  411. begin
  412. for i:=1 to ops do
  413. begin
  414. if (operands[i].opr.typ<>OPR_CONSTANT) and
  415. (tx86operand(operands[i]).opsize in [S_B,S_W,S_L]) and
  416. (tx86operand(operands[i]).opsize<>opsize) then
  417. sizeerr:=true;
  418. end;
  419. end;
  420. if sizeerr then
  421. begin
  422. { if range checks are on then generate an error }
  423. if (cs_compilesystem in current_settings.moduleswitches) or
  424. not (cs_check_range in current_settings.localswitches) then
  425. Message(asmr_w_size_suffix_and_dest_dont_match)
  426. else
  427. Message(asmr_e_size_suffix_and_dest_dont_match);
  428. end;
  429. end;
  430. { This check must be done with the operand in ATT order
  431. i.e.after swapping in the intel reader
  432. but before swapping in the NASM and TASM writers PM }
  433. procedure Tx86Instruction.CheckNonCommutativeOpcodes;
  434. begin
  435. if (OpOrder=op_intel) then
  436. SwapOperands;
  437. if (
  438. (ops=2) and
  439. (operands[1].opr.typ=OPR_REGISTER) and
  440. (operands[2].opr.typ=OPR_REGISTER) and
  441. { if the first is ST and the second is also a register
  442. it is necessarily ST1 .. ST7 }
  443. ((operands[1].opr.reg=NR_ST) or
  444. (operands[1].opr.reg=NR_ST0))
  445. ) or
  446. (ops=0) then
  447. if opcode=A_FSUBR then
  448. opcode:=A_FSUB
  449. else if opcode=A_FSUB then
  450. opcode:=A_FSUBR
  451. else if opcode=A_FDIVR then
  452. opcode:=A_FDIV
  453. else if opcode=A_FDIV then
  454. opcode:=A_FDIVR
  455. else if opcode=A_FSUBRP then
  456. opcode:=A_FSUBP
  457. else if opcode=A_FSUBP then
  458. opcode:=A_FSUBRP
  459. else if opcode=A_FDIVRP then
  460. opcode:=A_FDIVP
  461. else if opcode=A_FDIVP then
  462. opcode:=A_FDIVRP;
  463. if (
  464. (ops=1) and
  465. (operands[1].opr.typ=OPR_REGISTER) and
  466. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  467. (operands[1].opr.reg<>NR_ST) and
  468. (operands[1].opr.reg<>NR_ST0)
  469. ) then
  470. if opcode=A_FSUBRP then
  471. opcode:=A_FSUBP
  472. else if opcode=A_FSUBP then
  473. opcode:=A_FSUBRP
  474. else if opcode=A_FDIVRP then
  475. opcode:=A_FDIVP
  476. else if opcode=A_FDIVP then
  477. opcode:=A_FDIVRP;
  478. end;
  479. {*****************************************************************************
  480. opcode Adding
  481. *****************************************************************************}
  482. function Tx86Instruction.ConcatInstruction(p : TAsmList) : tai;
  483. var
  484. siz : topsize;
  485. i,asize : longint;
  486. ai : taicpu;
  487. begin
  488. if (OpOrder=op_intel) then
  489. SwapOperands;
  490. ai:=nil;
  491. for i:=1 to Ops do
  492. if not operands[i].CheckOperand then
  493. exit;
  494. { Get Opsize }
  495. if (opsize<>S_NO) or (Ops=0) then
  496. siz:=opsize
  497. else
  498. begin
  499. if (Ops=2) and (operands[1].opr.typ=OPR_REGISTER) then
  500. siz:=tx86operand(operands[1]).opsize
  501. else
  502. siz:=tx86operand(operands[Ops]).opsize;
  503. { MOVD should be of size S_LQ or S_QL, but these do not exist PM }
  504. if (ops=2) and
  505. (tx86operand(operands[1]).opsize<>S_NO) and
  506. (tx86operand(operands[2]).opsize<>S_NO) and
  507. (tx86operand(operands[1]).opsize<>tx86operand(operands[2]).opsize) then
  508. siz:=S_NO;
  509. end;
  510. if ((opcode=A_MOVD)or
  511. (opcode=A_CVTSI2SS)) and
  512. ((tx86operand(operands[1]).opsize=S_NO) or
  513. (tx86operand(operands[2]).opsize=S_NO)) then
  514. siz:=S_NO;
  515. { NASM does not support FADD without args
  516. as alias of FADDP
  517. and GNU AS interprets FADD without operand differently
  518. for version 2.9.1 and 2.9.5 !! }
  519. if (ops=0) and
  520. ((opcode=A_FADD) or
  521. (opcode=A_FMUL) or
  522. (opcode=A_FSUB) or
  523. (opcode=A_FSUBR) or
  524. (opcode=A_FDIV) or
  525. (opcode=A_FDIVR)) then
  526. begin
  527. if opcode=A_FADD then
  528. opcode:=A_FADDP
  529. else if opcode=A_FMUL then
  530. opcode:=A_FMULP
  531. else if opcode=A_FSUB then
  532. opcode:=A_FSUBP
  533. else if opcode=A_FSUBR then
  534. opcode:=A_FSUBRP
  535. else if opcode=A_FDIV then
  536. opcode:=A_FDIVP
  537. else if opcode=A_FDIVR then
  538. opcode:=A_FDIVRP;
  539. message1(asmr_w_fadd_to_faddp,std_op2str[opcode]);
  540. end;
  541. {It is valid to specify some instructions without operand size.}
  542. if siz=S_NO then
  543. begin
  544. if (ops=1) and (opcode=A_INT) then
  545. siz:=S_B;
  546. if (ops=1) and (opcode=A_RET) or (opcode=A_RETN) or (opcode=A_RETF) then
  547. siz:=S_W;
  548. if (ops=1) and (opcode=A_PUSH) then
  549. begin
  550. {We are a 32 compiler, assume 32-bit by default. This is Delphi
  551. compatible but bad coding practise.}
  552. siz:=S_L;
  553. message(asmr_w_unable_to_determine_reference_size_using_dword);
  554. end;
  555. if (opcode=A_JMP) or (opcode=A_JCC) or (opcode=A_CALL) then
  556. if ops=1 then
  557. siz:=S_NEAR
  558. else
  559. siz:=S_FAR;
  560. end;
  561. {$ifdef x86_64}
  562. { Convert movq with at least one general registers or constant to a mov instruction }
  563. if (opcode=A_MOVQ) and
  564. (ops=2) and
  565. (
  566. (operands[1].opr.typ=OPR_REGISTER) or
  567. (operands[2].opr.typ=OPR_REGISTER) or
  568. (operands[1].opr.typ=OPR_CONSTANT)
  569. ) then
  570. opcode:=A_MOV;
  571. {$endif x86_64}
  572. { GNU AS interprets FDIV without operand differently
  573. for version 2.9.1 and 2.10
  574. we add explicit args to it !! }
  575. if (ops=0) and
  576. ((opcode=A_FSUBP) or
  577. (opcode=A_FSUBRP) or
  578. (opcode=A_FDIVP) or
  579. (opcode=A_FDIVRP) or
  580. (opcode=A_FSUB) or
  581. (opcode=A_FSUBR) or
  582. (opcode=A_FADD) or
  583. (opcode=A_FADDP) or
  584. (opcode=A_FDIV) or
  585. (opcode=A_FDIVR)) then
  586. begin
  587. message1(asmr_w_adding_explicit_args_fXX,std_op2str[opcode]);
  588. ops:=2;
  589. operands[1].opr.typ:=OPR_REGISTER;
  590. operands[2].opr.typ:=OPR_REGISTER;
  591. operands[1].opr.reg:=NR_ST0;
  592. operands[2].opr.reg:=NR_ST1;
  593. end;
  594. if (ops=1) and
  595. (
  596. (operands[1].opr.typ=OPR_REGISTER) and
  597. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  598. (operands[1].opr.reg<>NR_ST) and
  599. (operands[1].opr.reg<>NR_ST0)
  600. ) and
  601. (
  602. (opcode=A_FSUBP) or
  603. (opcode=A_FSUBRP) or
  604. (opcode=A_FDIVP) or
  605. (opcode=A_FDIVRP) or
  606. (opcode=A_FADDP) or
  607. (opcode=A_FMULP)
  608. ) then
  609. begin
  610. message1(asmr_w_adding_explicit_first_arg_fXX,std_op2str[opcode]);
  611. ops:=2;
  612. operands[2].opr.typ:=OPR_REGISTER;
  613. operands[2].opr.reg:=operands[1].opr.reg;
  614. operands[1].opr.reg:=NR_ST0;
  615. end;
  616. if (ops=1) and
  617. (
  618. (operands[1].opr.typ=OPR_REGISTER) and
  619. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  620. (operands[1].opr.reg<>NR_ST) and
  621. (operands[1].opr.reg<>NR_ST0)
  622. ) and
  623. (
  624. (opcode=A_FSUB) or
  625. (opcode=A_FSUBR) or
  626. (opcode=A_FDIV) or
  627. (opcode=A_FDIVR) or
  628. (opcode=A_FADD) or
  629. (opcode=A_FMUL)
  630. ) then
  631. begin
  632. message1(asmr_w_adding_explicit_second_arg_fXX,std_op2str[opcode]);
  633. ops:=2;
  634. operands[2].opr.typ:=OPR_REGISTER;
  635. operands[2].opr.reg:=NR_ST0;
  636. end;
  637. { I tried to convince Linus Torvalds to add
  638. code to support ENTER instruction
  639. (when raising a stack page fault)
  640. but he replied that ENTER is a bad instruction and
  641. Linux does not need to support it
  642. So I think its at least a good idea to add a warning
  643. if someone uses this in assembler code
  644. FPC itself does not use it at all PM }
  645. if (opcode=A_ENTER) and
  646. (target_info.system in [system_i386_linux,system_i386_FreeBSD]) then
  647. Message(asmr_w_enter_not_supported_by_linux);
  648. ai:=taicpu.op_none(opcode,siz);
  649. ai.SetOperandOrder(OpOrder);
  650. ai.Ops:=Ops;
  651. ai.Allocate_oper(Ops);
  652. for i:=1 to Ops do
  653. case operands[i].opr.typ of
  654. OPR_CONSTANT :
  655. ai.loadconst(i-1,operands[i].opr.val);
  656. OPR_REGISTER:
  657. ai.loadreg(i-1,operands[i].opr.reg);
  658. OPR_SYMBOL:
  659. ai.loadsymbol(i-1,operands[i].opr.symbol,operands[i].opr.symofs);
  660. OPR_LOCAL :
  661. with operands[i].opr do
  662. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  663. localscale,localgetoffset,localforceref);
  664. OPR_REFERENCE:
  665. begin
  666. ai.loadref(i-1,operands[i].opr.ref);
  667. if operands[i].size<>OS_NO then
  668. begin
  669. asize:=0;
  670. case operands[i].size of
  671. OS_8,OS_S8 :
  672. asize:=OT_BITS8;
  673. OS_16,OS_S16 :
  674. asize:=OT_BITS16;
  675. OS_32,OS_S32,OS_F32 :
  676. asize:=OT_BITS32;
  677. OS_64,OS_S64:
  678. begin
  679. { Only FPU operations know about 64bit values, for all
  680. integer operations it is seen as 32bit }
  681. if gas_needsuffix[opcode] in [attsufFPU,attsufFPUint] then
  682. asize:=OT_BITS64
  683. else
  684. asize:=OT_BITS32;
  685. end;
  686. OS_F64,OS_C64 :
  687. asize:=OT_BITS64;
  688. OS_F80 :
  689. asize:=OT_BITS80;
  690. end;
  691. if asize<>0 then
  692. ai.oper[i-1]^.ot:=(ai.oper[i-1]^.ot and not OT_SIZE_MASK) or asize;
  693. end;
  694. end;
  695. end;
  696. { Condition ? }
  697. if condition<>C_None then
  698. ai.SetCondition(condition);
  699. { Set is_jmp, it enables asmwriter to emit short jumps if appropriate }
  700. if (opcode=A_JMP) or (opcode=A_JCC) then
  701. ai.is_jmp := True;
  702. { Concat the opcode or give an error }
  703. if assigned(ai) then
  704. p.concat(ai)
  705. else
  706. Message(asmr_e_invalid_opcode_and_operand);
  707. result:=ai;
  708. end;
  709. end.