rax86.pas 23 KB

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