rax86.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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. { handles the @Code symbol }
  39. Procedure SetupCode;
  40. { handles the @Data symbol }
  41. Procedure SetupData;
  42. end;
  43. { Operands are always in AT&T order.
  44. Intel reader attaches them right-to-left, then shifts to start with 1 }
  45. Tx86Instruction=class(TInstruction)
  46. opsize : topsize;
  47. constructor Create(optype : tcoperand);override;
  48. { Operand sizes }
  49. procedure AddReferenceSizes; virtual;
  50. procedure SetInstructionOpsize;
  51. procedure CheckOperandSizes;
  52. procedure CheckNonCommutativeOpcodes;
  53. { Additional actions required by specific reader }
  54. procedure FixupOpcode;virtual;
  55. { opcode adding }
  56. function ConcatInstruction(p : TAsmList) : tai;override;
  57. end;
  58. const
  59. AsmPrefixes = 8{$ifdef i8086}+2{$endif i8086};
  60. AsmPrefix : array[0..AsmPrefixes-1] of TasmOP =(
  61. A_LOCK,A_REP,A_REPE,A_REPNE,A_REPNZ,A_REPZ,A_XACQUIRE,A_XRELEASE{$ifdef i8086},A_REPC,A_REPNC{$endif i8086}
  62. );
  63. AsmOverrides = 6;
  64. AsmOverride : array[0..AsmOverrides-1] of TasmOP =(
  65. A_SEGCS,A_SEGES,A_SEGDS,A_SEGFS,A_SEGGS,A_SEGSS
  66. );
  67. CondAsmOps=3;
  68. CondAsmOp:array[0..CondAsmOps-1] of TasmOp=(
  69. A_CMOVcc, A_Jcc, A_SETcc
  70. );
  71. CondAsmOpStr:array[0..CondAsmOps-1] of string[4]=(
  72. 'CMOV','J','SET'
  73. );
  74. implementation
  75. uses
  76. globtype,globals,systems,verbose,
  77. procinfo,
  78. cgbase,cgutils,
  79. itcpugas,cgx86, cutils;
  80. {*****************************************************************************
  81. Parser Helpers
  82. *****************************************************************************}
  83. function is_prefix(t:tasmop):boolean;
  84. var
  85. i : longint;
  86. Begin
  87. is_prefix:=false;
  88. for i:=1 to AsmPrefixes do
  89. if t=AsmPrefix[i-1] then
  90. begin
  91. is_prefix:=true;
  92. exit;
  93. end;
  94. end;
  95. function is_override(t:tasmop):boolean;
  96. var
  97. i : longint;
  98. Begin
  99. is_override:=false;
  100. for i:=1 to AsmOverrides do
  101. if t=AsmOverride[i-1] then
  102. begin
  103. is_override:=true;
  104. exit;
  105. end;
  106. end;
  107. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  108. { Checks if the prefix is valid with the following opcode }
  109. { return false if not, otherwise true }
  110. Begin
  111. CheckPrefix := TRUE;
  112. (* Case prefix of
  113. A_REP,A_REPNE,A_REPE:
  114. Case opcode Of
  115. A_SCASB,A_SCASW,A_SCASD,
  116. A_INS,A_OUTS,A_MOVS,A_CMPS,A_LODS,A_STOS:;
  117. Else
  118. Begin
  119. CheckPrefix := FALSE;
  120. exit;
  121. end;
  122. end; { case }
  123. A_LOCK:
  124. Case opcode Of
  125. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,A_ADC,A_SBB,A_AND,A_SUB,
  126. A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  127. Else
  128. Begin
  129. CheckPrefix := FALSE;
  130. Exit;
  131. end;
  132. end; { case }
  133. A_NONE: exit; { no prefix here }
  134. else
  135. CheckPrefix := FALSE;
  136. end; { end case } *)
  137. end;
  138. Function CheckOverride(overrideop,op:tasmop): Boolean;
  139. { Check if the override is valid, and if so then }
  140. { update the instr variable accordingly. }
  141. Begin
  142. CheckOverride := true;
  143. { Case instr.getinstruction of
  144. A_MOVS,A_XLAT,A_CMPS:
  145. Begin
  146. CheckOverride := TRUE;
  147. Message(assem_e_segment_override_not_supported);
  148. end
  149. end }
  150. end;
  151. Procedure FWaitWarning;
  152. begin
  153. if (target_info.system=system_i386_GO32V2) and (cs_fp_emulation in current_settings.moduleswitches) then
  154. Message(asmr_w_fwait_emu_prob);
  155. end;
  156. {*****************************************************************************
  157. TX86Operand
  158. *****************************************************************************}
  159. Procedure Tx86Operand.SetSize(_size:longint;force:boolean);
  160. begin
  161. inherited SetSize(_size,force);
  162. { OS_64 will be set to S_L and be fixed later
  163. in SetCorrectSize }
  164. // multimedia register
  165. case _size of
  166. 16: size := OS_M128;
  167. 32: size := OS_M256;
  168. end;
  169. {$ifdef i8086}
  170. { allows e.g. using 32-bit registers in i8086 inline asm }
  171. if size in [OS_32,OS_S32] then
  172. opsize:=S_L
  173. else
  174. {$endif i8086}
  175. opsize:=TCGSize2Opsize[size];
  176. end;
  177. Procedure Tx86Operand.SetCorrectSize(opcode:tasmop);
  178. begin
  179. if gas_needsuffix[opcode]=attsufFPU then
  180. begin
  181. case size of
  182. OS_32 : opsize:=S_FS;
  183. OS_64 : opsize:=S_FL;
  184. else
  185. ;
  186. end;
  187. end
  188. else if gas_needsuffix[opcode]=attsufFPUint then
  189. begin
  190. case size of
  191. OS_16 : opsize:=S_IS;
  192. OS_32 : opsize:=S_IL;
  193. OS_64 : opsize:=S_IQ;
  194. else
  195. ;
  196. end;
  197. end
  198. else if gas_needsuffix[opcode]=AttSufMM then
  199. begin
  200. if (opr.typ=OPR_Reference) then
  201. begin
  202. case size of
  203. OS_32 : size := OS_M32;
  204. OS_64 : size := OS_M64;
  205. else
  206. ;
  207. end;
  208. end;
  209. end
  210. else
  211. begin
  212. if size=OS_64 then
  213. opsize:=S_Q;
  214. end;
  215. end;
  216. Function Tx86Operand.CheckOperand: boolean;
  217. begin
  218. result:=true;
  219. if (opr.typ=OPR_Reference) then
  220. begin
  221. if not hasvar then
  222. begin
  223. if (getsupreg(opr.ref.base)=RS_EBP) and (opr.ref.offset>0) then
  224. begin
  225. if current_procinfo.procdef.proccalloption=pocall_register then
  226. message(asmr_w_no_direct_ebp_for_parameter)
  227. else
  228. message(asmr_w_direct_ebp_for_parameter_regcall);
  229. end
  230. else if (getsupreg(opr.ref.base)=RS_EBP) and (opr.ref.offset<0) then
  231. message(asmr_w_direct_ebp_neg_offset)
  232. else if (getsupreg(opr.ref.base)=RS_ESP) and (opr.ref.offset<0) then
  233. message(asmr_w_direct_esp_neg_offset);
  234. end;
  235. if (cs_create_pic in current_settings.moduleswitches) and
  236. assigned(opr.ref.symbol) and
  237. not assigned(opr.ref.relsymbol) then
  238. begin
  239. if not(opr.ref.refaddr in [addr_pic,addr_pic_no_got]) then
  240. begin
  241. if (opr.ref.symbol.name <> '_GLOBAL_OFFSET_TABLE_') then
  242. begin
  243. message(asmr_e_need_pic_ref);
  244. result:=false;
  245. end
  246. else
  247. opr.ref.refaddr:=addr_pic;
  248. end
  249. else
  250. begin
  251. {$ifdef x86_64}
  252. { should probably be extended to i386, but there the situation
  253. is more complex and ELF-style PIC still need to be
  254. tested/debugged }
  255. if (opr.ref.symbol.bind in [AB_LOCAL,AB_PRIVATE_EXTERN]) and
  256. (opr.ref.refaddr=addr_pic) then
  257. message(asmr_w_useless_got_for_local)
  258. else if (opr.ref.symbol.bind in [AB_GLOBAL,AB_EXTERNAL,AB_COMMON,AB_WEAK_EXTERNAL]) and
  259. (opr.ref.refaddr=addr_pic_no_got) then
  260. message(asmr_w_global_access_without_got);
  261. {$endif x86_64}
  262. end;
  263. end;
  264. end;
  265. end;
  266. procedure Tx86Operand.SetupCode;
  267. begin
  268. {$ifdef i8086}
  269. opr.typ:=OPR_SYMBOL;
  270. opr.symofs:=0;
  271. opr.symbol:=current_asmdata.RefAsmSymbol(current_procinfo.procdef.mangledname,AT_FUNCTION);
  272. opr.symseg:=true;
  273. opr.sym_farproc_entry:=false;
  274. {$else i8086}
  275. Message(asmr_w_CODE_and_DATA_not_supported);
  276. {$endif i8086}
  277. end;
  278. procedure Tx86Operand.SetupData;
  279. begin
  280. {$ifdef i8086}
  281. InitRef;
  282. if current_settings.x86memorymodel=mm_huge then
  283. opr.ref.refaddr:=addr_fardataseg
  284. else
  285. opr.ref.refaddr:=addr_dgroup;
  286. {$else i8086}
  287. Message(asmr_w_CODE_and_DATA_not_supported);
  288. {$endif i8086}
  289. end;
  290. {*****************************************************************************
  291. T386Instruction
  292. *****************************************************************************}
  293. constructor Tx86Instruction.Create(optype : tcoperand);
  294. begin
  295. inherited Create(optype);
  296. Opsize:=S_NO;
  297. end;
  298. procedure Tx86Instruction.AddReferenceSizes;
  299. { this will add the sizes for references like [esi] which do not
  300. have the size set yet, it will take only the size if the other
  301. operand is a register }
  302. var
  303. operand2,i,j : longint;
  304. s : tasmsymbol;
  305. so : aint;
  306. ExistsMemRefNoSize: boolean;
  307. ExistsMemRef: boolean;
  308. ExistsConstNoSize: boolean;
  309. ExistsLocalSymSize: boolean;
  310. memrefsize: integer;
  311. memopsize: integer;
  312. memoffset: asizeint;
  313. begin
  314. ExistsMemRefNoSize := false;
  315. ExistsMemRef := false;
  316. ExistsConstNoSize := false;
  317. ExistsLocalSymSize := false;
  318. // EXIST A MEMORY- OR CONSTANT-OPERAND WITHOUT SIZE ?
  319. for i := 1 to ops do
  320. begin
  321. if operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL] then
  322. begin
  323. ExistsMemRef := true;
  324. if (tx86operand(operands[i]).opsize = S_NO) then
  325. begin
  326. ExistsMemRefNoSize := true;
  327. case operands[i].opr.Typ of
  328. OPR_LOCAL: ExistsLocalSymSize := tx86operand(operands[i]).opr.localsym.getsize > 0;
  329. OPR_REFERENCE: ExistsLocalSymSize := true;
  330. else
  331. ;
  332. end;
  333. end;
  334. end
  335. else if operands[i].Opr.Typ in [OPR_CONSTANT] then
  336. begin
  337. ExistsConstNoSize := tx86operand(operands[i]).opsize = S_NO;
  338. end;
  339. end;
  340. // ONLY SUPPORTED OPCODES WITH SSE- OR AVX-REGISTERS
  341. if (ExistsMemRef) and
  342. (MemRefInfo(opcode).ExistsSSEAVX) then
  343. begin
  344. // 1. WE HAVE AN SSE- OR AVX-OPCODE WITH MEMORY OPERAND
  345. if (not(ExistsMemRefNoSize)) or
  346. (ExistsLocalSymSize) then
  347. begin
  348. // 2. WE KNOWN THE MEMORYSIZE OF THE MEMORY-OPERAND OR WE CAN
  349. // CALC THE MEMORYSIZE
  350. // 3. CALC THE SIZE OF THE MEMORYOPERAND BY OPCODE-DEFINITION
  351. // 4. COMPARE THE SIZE FROM OPCODE-DEFINITION AND THE REAL MEMORY-OPERAND-SIZE
  352. // - validate memory-reference-size
  353. for i := 1 to ops do
  354. begin
  355. if (operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL]) then
  356. begin
  357. memrefsize := -1;
  358. case MemRefInfo(opcode).MemRefSize of
  359. msiMultiple8,
  360. msiMem8: memrefsize := 8;
  361. msiMultiple16,
  362. msiMem16: memrefsize := 16;
  363. msiXMem32,
  364. msiYMem32,
  365. msiMultiple32,
  366. msiMem32: memrefsize := 32;
  367. msiXMem64,
  368. msiYMem64,
  369. msiMultiple64,
  370. msiMem64: memrefsize := 64;
  371. msiMultiple128,
  372. msiMem128: memrefsize := 128;
  373. msiMultiple256,
  374. msiMem256: memrefsize := 256;
  375. msiMemRegx16y32:
  376. begin
  377. for j := 1 to ops do
  378. begin
  379. if operands[j].Opr.Typ = OPR_REGISTER then
  380. begin
  381. case getsubreg(operands[j].opr.reg) of
  382. R_SUBMMX: memrefsize := 16;
  383. R_SUBMMY: memrefsize := 32;
  384. else Message(asmr_e_unable_to_determine_reference_size);
  385. end;
  386. end;
  387. end;
  388. end;
  389. msiMemRegx32y64:
  390. begin
  391. for j := 1 to ops do
  392. begin
  393. if operands[j].Opr.Typ = OPR_REGISTER then
  394. begin
  395. case getsubreg(operands[j].opr.reg) of
  396. R_SUBMMX: memrefsize := 32;
  397. R_SUBMMY: memrefsize := 64;
  398. else Message(asmr_e_unable_to_determine_reference_size);
  399. end;
  400. end;
  401. end;
  402. end;
  403. msiMemRegx64y128:
  404. begin
  405. for j := 1 to ops do
  406. begin
  407. if operands[j].Opr.Typ = OPR_REGISTER then
  408. begin
  409. case getsubreg(operands[j].opr.reg) of
  410. R_SUBMMX: memrefsize := 64;
  411. R_SUBMMY: memrefsize := 128;
  412. else Message(asmr_e_unable_to_determine_reference_size);
  413. end;
  414. end;
  415. end;
  416. end;
  417. msiMemRegx64y256:
  418. begin
  419. for j := 1 to ops do
  420. begin
  421. if operands[j].Opr.Typ = OPR_REGISTER then
  422. begin
  423. case getsubreg(operands[j].opr.reg) of
  424. R_SUBMMX: memrefsize := 64;
  425. R_SUBMMY: memrefsize := 256;
  426. else Message(asmr_e_unable_to_determine_reference_size);
  427. end;
  428. end;
  429. end;
  430. end;
  431. msiMemRegSize
  432. : for j := 1 to ops do
  433. begin
  434. if operands[j].Opr.Typ = OPR_REGISTER then
  435. begin
  436. if (tx86operand(operands[j]).opsize <> S_NO) and
  437. (tx86operand(operands[j]).size <> OS_NO) then
  438. begin
  439. case tx86operand(operands[j]).opsize of
  440. S_B : memrefsize := 8;
  441. S_W : memrefsize := 16;
  442. S_L : memrefsize := 32;
  443. S_Q : memrefsize := 64;
  444. S_XMM : memrefsize := 128;
  445. S_YMM : memrefsize := 256;
  446. else Internalerror(777200);
  447. end;
  448. break;
  449. end;
  450. end;
  451. end;
  452. msiNoSize,
  453. msiUnkown,
  454. msiUnsupported,
  455. msiVMemMultiple,
  456. msiVMemRegSize,
  457. msiMultiple:
  458. ;
  459. end;
  460. if memrefsize > -1 then
  461. begin
  462. // CALC REAL-MEMORY-OPERAND-SIZE AND A POSSIBLE OFFSET
  463. // OFFSET:
  464. // e.g. PAND XMM0, [RAX + 16] =>> OFFSET = 16 BYTES
  465. // PAND XMM0, [RAX + a.b + 10] =>> OFFSET = 10 BYTES (a = record-variable)
  466. memopsize := 0;
  467. case operands[i].opr.typ of
  468. OPR_LOCAL: memopsize := operands[i].opr.localvarsize * 8;
  469. OPR_REFERENCE:
  470. if operands[i].opr.ref.refaddr = addr_pic then
  471. memopsize := sizeof(pint) * 8
  472. else
  473. memopsize := operands[i].opr.varsize * 8;
  474. else
  475. ;
  476. end;
  477. if memopsize = 0 then memopsize := topsize2memsize[tx86operand(operands[i]).opsize];
  478. if (memopsize > 0) and
  479. (memrefsize > 0) then
  480. begin
  481. memoffset := 0;
  482. case operands[i].opr.typ of
  483. OPR_LOCAL:
  484. memoffset := operands[i].opr.localconstoffset;
  485. OPR_REFERENCE:
  486. memoffset := operands[i].opr.constoffset;
  487. else
  488. ;
  489. end;
  490. if memoffset < 0 then
  491. begin
  492. Message2(asmr_w_check_mem_operand_negative_offset,
  493. std_op2str[opcode],
  494. ToStr(memoffset));
  495. end
  496. else if (memopsize < (memrefsize + memoffset * 8)) then
  497. begin
  498. if memoffset = 0 then
  499. begin
  500. Message3(asmr_w_check_mem_operand_size3,
  501. std_op2str[opcode],
  502. ToStr(memopsize),
  503. ToStr(memrefsize)
  504. );
  505. end
  506. else
  507. begin
  508. Message4(asmr_w_check_mem_operand_size_offset,
  509. std_op2str[opcode],
  510. ToStr(memopsize),
  511. ToStr(memrefsize),
  512. ToStr(memoffset)
  513. );
  514. end;
  515. end;
  516. end;
  517. end;
  518. end;
  519. end;
  520. end;
  521. end;
  522. if (ExistsMemRefNoSize or ExistsConstNoSize) and
  523. (MemRefInfo(opcode).ExistsSSEAVX) then
  524. begin
  525. for i := 1 to ops do
  526. begin
  527. if (tx86operand(operands[i]).opsize = S_NO) then
  528. begin
  529. case operands[i].Opr.Typ of
  530. OPR_REFERENCE:
  531. case MemRefInfo(opcode).MemRefSize of
  532. msiMem8:
  533. begin
  534. tx86operand(operands[i]).opsize := S_B;
  535. tx86operand(operands[i]).size := OS_8;
  536. end;
  537. msiMultiple8:
  538. begin
  539. tx86operand(operands[i]).opsize := S_B;
  540. tx86operand(operands[i]).size := OS_8;
  541. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"8 bit memory operand"');
  542. end;
  543. msiMem16:
  544. begin
  545. tx86operand(operands[i]).opsize := S_W;
  546. tx86operand(operands[i]).size := OS_16;
  547. end;
  548. msiMultiple16:
  549. begin
  550. tx86operand(operands[i]).opsize := S_W;
  551. tx86operand(operands[i]).size := OS_16;
  552. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"16 bit memory operand"');
  553. end;
  554. msiXMem32,
  555. msiYMem32,
  556. msiMem32:
  557. begin
  558. tx86operand(operands[i]).opsize := S_L;
  559. tx86operand(operands[i]).size := OS_32;
  560. end;
  561. msiMultiple32:
  562. begin
  563. tx86operand(operands[i]).opsize := S_L;
  564. tx86operand(operands[i]).size := OS_32;
  565. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"32 bit memory operand"');
  566. end;
  567. msiXMem64,
  568. msiYMem64,
  569. msiMem64:
  570. begin
  571. tx86operand(operands[i]).opsize := S_Q;
  572. tx86operand(operands[i]).size := OS_M64;
  573. end;
  574. msiMultiple64:
  575. begin
  576. tx86operand(operands[i]).opsize := S_Q;
  577. tx86operand(operands[i]).size := OS_M64;
  578. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"64 bit memory operand"');
  579. end;
  580. msiMem128:
  581. begin
  582. tx86operand(operands[i]).opsize := S_XMM;
  583. tx86operand(operands[i]).size := OS_M128;
  584. end;
  585. msiMultiple128:
  586. begin
  587. tx86operand(operands[i]).opsize := S_XMM;
  588. tx86operand(operands[i]).size := OS_M128;
  589. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"128 bit memory operand"');
  590. end;
  591. msiMem256:
  592. begin
  593. tx86operand(operands[i]).opsize := S_YMM;
  594. tx86operand(operands[i]).size := OS_M256;
  595. opsize := S_YMM;
  596. end;
  597. msiMultiple256:
  598. begin
  599. tx86operand(operands[i]).opsize := S_YMM;
  600. tx86operand(operands[i]).size := OS_M256;
  601. opsize := S_YMM;
  602. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"256 bit memory operand"');
  603. end;
  604. msiMemRegSize:
  605. begin
  606. // mem-ref-size = register size
  607. for j := 1 to ops do
  608. begin
  609. if operands[j].Opr.Typ = OPR_REGISTER then
  610. begin
  611. if (tx86operand(operands[j]).opsize <> S_NO) and
  612. (tx86operand(operands[j]).size <> OS_NO) then
  613. begin
  614. tx86operand(operands[i]).opsize := tx86operand(operands[j]).opsize;
  615. tx86operand(operands[i]).size := tx86operand(operands[j]).size;
  616. break;
  617. end
  618. else Message(asmr_e_unable_to_determine_reference_size);
  619. end;
  620. end;
  621. end;
  622. msiMemRegx16y32:
  623. begin
  624. for j := 1 to ops do
  625. begin
  626. if operands[j].Opr.Typ = OPR_REGISTER then
  627. begin
  628. case getsubreg(operands[j].opr.reg) of
  629. R_SUBMMX: begin
  630. tx86operand(operands[i]).opsize := S_L;
  631. tx86operand(operands[i]).size := OS_M16;
  632. break;
  633. end;
  634. R_SUBMMY: begin
  635. tx86operand(operands[i]).opsize := S_Q;
  636. tx86operand(operands[i]).size := OS_M32;
  637. break;
  638. end;
  639. else Message(asmr_e_unable_to_determine_reference_size);
  640. end;
  641. end;
  642. end;
  643. end;
  644. msiMemRegx32y64:
  645. begin
  646. for j := 1 to ops do
  647. begin
  648. if operands[j].Opr.Typ = OPR_REGISTER then
  649. begin
  650. case getsubreg(operands[j].opr.reg) of
  651. R_SUBMMX: begin
  652. tx86operand(operands[i]).opsize := S_L;
  653. tx86operand(operands[i]).size := OS_M32;
  654. break;
  655. end;
  656. R_SUBMMY: begin
  657. tx86operand(operands[i]).opsize := S_Q;
  658. tx86operand(operands[i]).size := OS_M64;
  659. break;
  660. end;
  661. else Message(asmr_e_unable_to_determine_reference_size);
  662. end;
  663. end;
  664. end;
  665. end;
  666. msiMemRegx64y128:
  667. begin
  668. for j := 1 to ops do
  669. begin
  670. if operands[j].Opr.Typ = OPR_REGISTER then
  671. begin
  672. case getsubreg(operands[j].opr.reg) of
  673. R_SUBMMX: begin
  674. tx86operand(operands[i]).opsize := S_Q;
  675. tx86operand(operands[i]).size := OS_M64;
  676. break;
  677. end;
  678. R_SUBMMY: begin
  679. tx86operand(operands[i]).opsize := S_XMM;
  680. tx86operand(operands[i]).size := OS_M128;
  681. break;
  682. end;
  683. else Message(asmr_e_unable_to_determine_reference_size);
  684. end;
  685. end;
  686. end;
  687. end;
  688. msiMemRegx64y256:
  689. begin
  690. for j := 1 to ops do
  691. begin
  692. if operands[j].Opr.Typ = OPR_REGISTER then
  693. begin
  694. case getsubreg(operands[j].opr.reg) of
  695. R_SUBMMX: begin
  696. tx86operand(operands[i]).opsize := S_Q;
  697. tx86operand(operands[i]).size := OS_M64;
  698. break;
  699. end;
  700. R_SUBMMY: begin
  701. tx86operand(operands[i]).opsize := S_YMM;
  702. tx86operand(operands[i]).size := OS_M256;
  703. break;
  704. end;
  705. else Message(asmr_e_unable_to_determine_reference_size);
  706. end;
  707. end;
  708. end;
  709. end;
  710. msiNoSize: ; // all memory-sizes are ok
  711. msiUnkown,
  712. msiUnsupported,
  713. msiVMemMultiple,
  714. msiVMemRegSize,
  715. msiMultiple: Message(asmr_e_unable_to_determine_reference_size); // TODO individual message
  716. end;
  717. OPR_CONSTANT:
  718. case MemRefInfo(opcode).ConstSize of
  719. csiMem8: begin
  720. tx86operand(operands[i]).opsize := S_B;
  721. tx86operand(operands[i]).size := OS_8;
  722. end;
  723. csiMem16: begin
  724. tx86operand(operands[i]).opsize := S_W;
  725. tx86operand(operands[i]).size := OS_16;
  726. end;
  727. csiMem32: begin
  728. tx86operand(operands[i]).opsize := S_L;
  729. tx86operand(operands[i]).size := OS_32;
  730. end;
  731. {$ifdef x86_64}
  732. csiMem64: begin
  733. tx86operand(operands[i]).opsize := S_Q;
  734. tx86operand(operands[i]).size := OS_64;
  735. end;
  736. {$else}
  737. csiMem64: begin
  738. internalerror(2019050910);
  739. end;
  740. {$endif}
  741. csiUnkown, csiMultiple, csiNoSize:
  742. ;
  743. end;
  744. else
  745. ;
  746. end;
  747. end;
  748. end;
  749. end;
  750. for i:=1 to ops do
  751. begin
  752. operands[i].SetCorrectSize(opcode);
  753. if tx86operand(operands[i]).opsize=S_NO then
  754. begin
  755. {$ifdef x86_64}
  756. if (opcode=A_MOVQ) and
  757. (ops=2) and
  758. (operands[1].opr.typ=OPR_CONSTANT) then
  759. opsize:=S_Q
  760. else
  761. {$endif x86_64}
  762. case operands[i].Opr.Typ of
  763. OPR_LOCAL,
  764. OPR_REFERENCE :
  765. begin
  766. { for 3-operand opcodes, operand #1 (in ATT order) is always an immediate,
  767. don't consider it. }
  768. if i=ops then
  769. operand2:=i-1
  770. else
  771. operand2:=i+1;
  772. if operand2>0 then
  773. begin
  774. { Only allow register as operand to take the size from }
  775. if operands[operand2].opr.typ=OPR_REGISTER then
  776. begin
  777. if ((opcode<>A_MOVD) and
  778. (opcode<>A_CVTSI2SS)) then
  779. begin
  780. //tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize;
  781. // torsten - 31.01.2012
  782. // old: xmm/ymm-register operands have a opsize = "S_NO"
  783. // new: xmm/ymm-register operands have a opsize = "S_XMM/S_YMM"
  784. // any SSE- and AVX-opcodes have mixed operand sizes (e.g. cvtsd2ss xmmreg, xmmreg/m32)
  785. // in this case is we need the old handling ("S_NO")
  786. // =>> ignore
  787. if (tx86operand(operands[operand2]).opsize <> S_XMM) and
  788. (tx86operand(operands[operand2]).opsize <> S_YMM) then
  789. tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize
  790. else tx86operand(operands[operand2]).opsize := S_NO;
  791. end;
  792. end
  793. else
  794. begin
  795. { if no register then take the opsize (which is available with ATT),
  796. if not availble then give an error }
  797. if opsize<>S_NO then
  798. tx86operand(operands[i]).opsize:=opsize
  799. else
  800. begin
  801. if (m_delphi in current_settings.modeswitches) then
  802. Message(asmr_w_unable_to_determine_reference_size_using_dword)
  803. else
  804. Message(asmr_e_unable_to_determine_reference_size);
  805. { recovery }
  806. tx86operand(operands[i]).opsize:=S_L;
  807. end;
  808. end;
  809. end
  810. else
  811. begin
  812. if opsize<>S_NO then
  813. tx86operand(operands[i]).opsize:=opsize
  814. end;
  815. end;
  816. OPR_SYMBOL :
  817. begin
  818. { Fix lea which need a reference }
  819. if opcode=A_LEA then
  820. begin
  821. s:=operands[i].opr.symbol;
  822. so:=operands[i].opr.symofs;
  823. operands[i].opr.typ:=OPR_REFERENCE;
  824. Fillchar(operands[i].opr.ref,sizeof(treference),0);
  825. operands[i].opr.ref.symbol:=s;
  826. operands[i].opr.ref.offset:=so;
  827. end;
  828. {$if defined(x86_64)}
  829. tx86operand(operands[i]).opsize:=S_Q;
  830. {$elseif defined(i386)}
  831. tx86operand(operands[i]).opsize:=S_L;
  832. {$elseif defined(i8086)}
  833. tx86operand(operands[i]).opsize:=S_W;
  834. {$endif}
  835. end;
  836. else
  837. ;
  838. end;
  839. end;
  840. end;
  841. end;
  842. procedure Tx86Instruction.SetInstructionOpsize;
  843. begin
  844. if opsize<>S_NO then
  845. exit;
  846. case ops of
  847. 0 : ;
  848. 1 :
  849. begin
  850. { "push es" must be stored as a long PM }
  851. if ((opcode=A_PUSH) or
  852. (opcode=A_POP)) and
  853. (operands[1].opr.typ=OPR_REGISTER) and
  854. is_segment_reg(operands[1].opr.reg) then
  855. {$ifdef i8086}
  856. opsize:=S_W
  857. {$else i8086}
  858. opsize:=S_L
  859. {$endif i8086}
  860. else
  861. opsize:=tx86operand(operands[1]).opsize;
  862. end;
  863. 2 :
  864. begin
  865. case opcode of
  866. A_MOVZX,A_MOVSX :
  867. begin
  868. if tx86operand(operands[1]).opsize=S_NO then
  869. begin
  870. tx86operand(operands[1]).opsize:=S_B;
  871. if (m_delphi in current_settings.modeswitches) then
  872. Message(asmr_w_unable_to_determine_reference_size_using_byte)
  873. else
  874. Message(asmr_e_unable_to_determine_reference_size);
  875. end;
  876. case tx86operand(operands[1]).opsize of
  877. S_W :
  878. case tx86operand(operands[2]).opsize of
  879. S_L :
  880. opsize:=S_WL;
  881. {$ifdef x86_64}
  882. S_Q :
  883. opsize:=S_WQ;
  884. {$endif}
  885. else
  886. ;
  887. end;
  888. S_B :
  889. begin
  890. case tx86operand(operands[2]).opsize of
  891. S_W :
  892. opsize:=S_BW;
  893. S_L :
  894. opsize:=S_BL;
  895. {$ifdef x86_64}
  896. S_Q :
  897. opsize:=S_BQ;
  898. {$endif}
  899. else
  900. ;
  901. end;
  902. end;
  903. else
  904. ;
  905. end;
  906. end;
  907. A_MOVSS,
  908. A_VMOVSS,
  909. A_MOVD : { movd is a move from a mmx register to a
  910. 32 bit register or memory, so no opsize is correct here PM }
  911. exit;
  912. A_MOVQ :
  913. opsize:=S_IQ;
  914. A_CVTSI2SS,
  915. A_CVTSI2SD,
  916. A_OUT :
  917. opsize:=tx86operand(operands[1]).opsize;
  918. else
  919. opsize:=tx86operand(operands[2]).opsize;
  920. end;
  921. end;
  922. 3 :
  923. begin
  924. case opcode of
  925. A_VCVTSI2SS,
  926. A_VCVTSI2SD:
  927. opsize:=tx86operand(operands[1]).opsize;
  928. else
  929. opsize:=tx86operand(operands[ops]).opsize;
  930. end;
  931. end;
  932. 4 :
  933. opsize:=tx86operand(operands[ops]).opsize;
  934. end;
  935. end;
  936. procedure Tx86Instruction.CheckOperandSizes;
  937. var
  938. sizeerr : boolean;
  939. i : longint;
  940. begin
  941. { Check only the most common opcodes here, the others are done in
  942. the assembler pass }
  943. case opcode of
  944. A_PUSH,A_POP,A_DEC,A_INC,A_NOT,A_NEG,
  945. A_CMP,A_MOV,
  946. A_ADD,A_SUB,A_ADC,A_SBB,
  947. A_AND,A_OR,A_TEST,A_XOR: ;
  948. else
  949. exit;
  950. end;
  951. { Handle the BW,BL,WL separatly }
  952. sizeerr:=false;
  953. { special push/pop selector case }
  954. if ((opcode=A_PUSH) or
  955. (opcode=A_POP)) and
  956. (operands[1].opr.typ=OPR_REGISTER) and
  957. is_segment_reg(operands[1].opr.reg) then
  958. exit;
  959. if opsize in [S_BW,S_BL,S_WL] then
  960. begin
  961. if ops<>2 then
  962. sizeerr:=true
  963. else
  964. begin
  965. case opsize of
  966. S_BW :
  967. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_W);
  968. S_BL :
  969. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_L);
  970. S_WL :
  971. sizeerr:=(tx86operand(operands[1]).opsize<>S_W) or (tx86operand(operands[2]).opsize<>S_L);
  972. {$ifdef x86_64}
  973. S_BQ:
  974. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_Q);
  975. S_WQ:
  976. sizeerr:=(tx86operand(operands[1]).opsize<>S_W) or (tx86operand(operands[2]).opsize<>S_Q);
  977. S_LQ:
  978. sizeerr:=(tx86operand(operands[1]).opsize<>S_L) or (tx86operand(operands[2]).opsize<>S_Q);
  979. {$endif}
  980. else
  981. ;
  982. end;
  983. end;
  984. end
  985. else
  986. begin
  987. for i:=1 to ops do
  988. begin
  989. if (operands[i].opr.typ<>OPR_CONSTANT) and
  990. (tx86operand(operands[i]).opsize in [S_B,S_W,S_L]) and
  991. (tx86operand(operands[i]).opsize<>opsize) then
  992. sizeerr:=true;
  993. end;
  994. end;
  995. if sizeerr then
  996. begin
  997. { if range checks are on then generate an error }
  998. if (cs_compilesystem in current_settings.moduleswitches) or
  999. not (cs_check_range in current_settings.localswitches) then
  1000. Message(asmr_w_size_suffix_and_dest_dont_match)
  1001. else
  1002. Message(asmr_e_size_suffix_and_dest_dont_match);
  1003. end;
  1004. end;
  1005. { This check must be done with the operand in ATT order
  1006. i.e.after swapping in the intel reader
  1007. but before swapping in the NASM and TASM writers PM }
  1008. procedure Tx86Instruction.CheckNonCommutativeOpcodes;
  1009. begin
  1010. if (
  1011. (ops=2) and
  1012. (operands[1].opr.typ=OPR_REGISTER) and
  1013. (operands[2].opr.typ=OPR_REGISTER) and
  1014. { if the first is ST and the second is also a register
  1015. it is necessarily ST1 .. ST7 }
  1016. ((operands[1].opr.reg=NR_ST) or
  1017. (operands[1].opr.reg=NR_ST0))
  1018. ) or
  1019. (ops=0) then
  1020. if opcode=A_FSUBR then
  1021. opcode:=A_FSUB
  1022. else if opcode=A_FSUB then
  1023. opcode:=A_FSUBR
  1024. else if opcode=A_FDIVR then
  1025. opcode:=A_FDIV
  1026. else if opcode=A_FDIV then
  1027. opcode:=A_FDIVR
  1028. else if opcode=A_FSUBRP then
  1029. opcode:=A_FSUBP
  1030. else if opcode=A_FSUBP then
  1031. opcode:=A_FSUBRP
  1032. else if opcode=A_FDIVRP then
  1033. opcode:=A_FDIVP
  1034. else if opcode=A_FDIVP then
  1035. opcode:=A_FDIVRP;
  1036. if (
  1037. (ops=1) and
  1038. (operands[1].opr.typ=OPR_REGISTER) and
  1039. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  1040. (operands[1].opr.reg<>NR_ST) and
  1041. (operands[1].opr.reg<>NR_ST0)
  1042. ) then
  1043. if opcode=A_FSUBRP then
  1044. opcode:=A_FSUBP
  1045. else if opcode=A_FSUBP then
  1046. opcode:=A_FSUBRP
  1047. else if opcode=A_FDIVRP then
  1048. opcode:=A_FDIVP
  1049. else if opcode=A_FDIVP then
  1050. opcode:=A_FDIVRP;
  1051. end;
  1052. procedure Tx86Instruction.FixupOpcode;
  1053. begin
  1054. { does nothing by default }
  1055. end;
  1056. {*****************************************************************************
  1057. opcode Adding
  1058. *****************************************************************************}
  1059. function Tx86Instruction.ConcatInstruction(p : TAsmList) : tai;
  1060. var
  1061. siz : topsize;
  1062. i,asize : longint;
  1063. ai : taicpu;
  1064. begin
  1065. ConcatInstruction:=nil;
  1066. ai:=nil;
  1067. for i:=1 to Ops do
  1068. if not operands[i].CheckOperand then
  1069. exit;
  1070. { Get Opsize }
  1071. if (opsize<>S_NO) or (Ops=0) then
  1072. siz:=opsize
  1073. else
  1074. begin
  1075. if (Ops=2) and (operands[1].opr.typ=OPR_REGISTER) then
  1076. siz:=tx86operand(operands[1]).opsize
  1077. else
  1078. siz:=tx86operand(operands[Ops]).opsize;
  1079. { MOVD should be of size S_LQ or S_QL, but these do not exist PM }
  1080. if (ops=2) and
  1081. (tx86operand(operands[1]).opsize<>S_NO) and
  1082. (tx86operand(operands[2]).opsize<>S_NO) and
  1083. (tx86operand(operands[1]).opsize<>tx86operand(operands[2]).opsize) then
  1084. siz:=S_NO;
  1085. end;
  1086. if ((opcode=A_MOVD)or
  1087. (opcode=A_CVTSI2SS)) and
  1088. ((tx86operand(operands[1]).opsize=S_NO) or
  1089. (tx86operand(operands[2]).opsize=S_NO)) then
  1090. siz:=S_NO;
  1091. { NASM does not support FADD without args
  1092. as alias of FADDP
  1093. and GNU AS interprets FADD without operand differently
  1094. for version 2.9.1 and 2.9.5 !! }
  1095. if (ops=0) and
  1096. ((opcode=A_FADD) or
  1097. (opcode=A_FMUL) or
  1098. (opcode=A_FSUB) or
  1099. (opcode=A_FSUBR) or
  1100. (opcode=A_FDIV) or
  1101. (opcode=A_FDIVR)) then
  1102. begin
  1103. if opcode=A_FADD then
  1104. opcode:=A_FADDP
  1105. else if opcode=A_FMUL then
  1106. opcode:=A_FMULP
  1107. else if opcode=A_FSUB then
  1108. opcode:=A_FSUBP
  1109. else if opcode=A_FSUBR then
  1110. opcode:=A_FSUBRP
  1111. else if opcode=A_FDIV then
  1112. opcode:=A_FDIVP
  1113. else if opcode=A_FDIVR then
  1114. opcode:=A_FDIVRP;
  1115. message1(asmr_w_fadd_to_faddp,std_op2str[opcode]);
  1116. end;
  1117. {It is valid to specify some instructions without operand size.}
  1118. if siz=S_NO then
  1119. begin
  1120. if (ops=1) and (opcode=A_INT) then
  1121. siz:=S_B;
  1122. if (ops=1) and (opcode=A_XABORT) then
  1123. siz:=S_B;
  1124. {$ifdef i8086}
  1125. if (ops=1) and (opcode=A_BRKEM) then
  1126. siz:=S_B;
  1127. {$endif i8086}
  1128. if (ops=1) and (opcode=A_RET) or (opcode=A_RETN) or (opcode=A_RETF) or
  1129. (opcode=A_RETW) or (opcode=A_RETNW) or (opcode=A_RETFW) or
  1130. {$ifndef x86_64}
  1131. (opcode=A_RETD) or (opcode=A_RETND) or
  1132. {$endif x86_64}
  1133. (opcode=A_RETFD)
  1134. {$ifdef x86_64}
  1135. or (opcode=A_RETQ) or (opcode=A_RETNQ) or (opcode=A_RETFQ)
  1136. {$endif x86_64}
  1137. then
  1138. siz:=S_W;
  1139. if (ops=1) and (opcode=A_PUSH) then
  1140. begin
  1141. {$ifdef i8086}
  1142. if (tx86operand(operands[1]).opr.val>=-128) and (tx86operand(operands[1]).opr.val<=127) then
  1143. begin
  1144. siz:=S_B;
  1145. message(asmr_w_unable_to_determine_constant_size_using_byte);
  1146. end
  1147. else
  1148. begin
  1149. siz:=S_W;
  1150. message(asmr_w_unable_to_determine_constant_size_using_word);
  1151. end;
  1152. {$else i8086}
  1153. { We are a 32 compiler, assume 32-bit by default. This is Delphi
  1154. compatible but bad coding practise.}
  1155. siz:=S_L;
  1156. message(asmr_w_unable_to_determine_reference_size_using_dword);
  1157. {$endif i8086}
  1158. end;
  1159. if (opcode=A_JMP) or (opcode=A_JCC) or (opcode=A_CALL) then
  1160. if ops=1 then
  1161. siz:=S_NEAR
  1162. else
  1163. siz:=S_FAR;
  1164. end;
  1165. { GNU AS interprets FDIV without operand differently
  1166. for version 2.9.1 and 2.10
  1167. we add explicit args to it !! }
  1168. if (ops=0) and
  1169. ((opcode=A_FSUBP) or
  1170. (opcode=A_FSUBRP) or
  1171. (opcode=A_FDIVP) or
  1172. (opcode=A_FDIVRP) or
  1173. (opcode=A_FSUB) or
  1174. (opcode=A_FSUBR) or
  1175. (opcode=A_FADD) or
  1176. (opcode=A_FADDP) or
  1177. (opcode=A_FDIV) or
  1178. (opcode=A_FDIVR)) then
  1179. begin
  1180. message1(asmr_w_adding_explicit_args_fXX,std_op2str[opcode]);
  1181. ops:=2;
  1182. operands[1].opr.typ:=OPR_REGISTER;
  1183. operands[2].opr.typ:=OPR_REGISTER;
  1184. operands[1].opr.reg:=NR_ST0;
  1185. operands[2].opr.reg:=NR_ST1;
  1186. end;
  1187. if (ops=1) and
  1188. (
  1189. (operands[1].opr.typ=OPR_REGISTER) and
  1190. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  1191. (operands[1].opr.reg<>NR_ST) and
  1192. (operands[1].opr.reg<>NR_ST0)
  1193. ) and
  1194. (
  1195. (opcode=A_FSUBP) or
  1196. (opcode=A_FSUBRP) or
  1197. (opcode=A_FDIVP) or
  1198. (opcode=A_FDIVRP) or
  1199. (opcode=A_FADDP) or
  1200. (opcode=A_FMULP)
  1201. ) then
  1202. begin
  1203. message1(asmr_w_adding_explicit_first_arg_fXX,std_op2str[opcode]);
  1204. ops:=2;
  1205. operands[2].opr.typ:=OPR_REGISTER;
  1206. operands[2].opr.reg:=operands[1].opr.reg;
  1207. operands[1].opr.reg:=NR_ST0;
  1208. end;
  1209. if (ops=1) and
  1210. (
  1211. (operands[1].opr.typ=OPR_REGISTER) and
  1212. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  1213. (operands[1].opr.reg<>NR_ST) and
  1214. (operands[1].opr.reg<>NR_ST0)
  1215. ) and
  1216. (
  1217. (opcode=A_FSUB) or
  1218. (opcode=A_FSUBR) or
  1219. (opcode=A_FDIV) or
  1220. (opcode=A_FDIVR) or
  1221. (opcode=A_FADD) or
  1222. (opcode=A_FMUL)
  1223. ) then
  1224. begin
  1225. message1(asmr_w_adding_explicit_second_arg_fXX,std_op2str[opcode]);
  1226. ops:=2;
  1227. operands[2].opr.typ:=OPR_REGISTER;
  1228. operands[2].opr.reg:=NR_ST0;
  1229. end;
  1230. { Check for 'POP CS' }
  1231. if (opcode=A_POP) and (ops=1) and (operands[1].opr.typ=OPR_REGISTER) and
  1232. (operands[1].opr.reg=NR_CS) then
  1233. {$ifdef i8086}
  1234. { On i8086 we print only a warning, because 'POP CS' works on 8086 and 8088
  1235. CPUs, but isn't supported on any later CPU }
  1236. Message(asmr_w_pop_cs_not_portable);
  1237. {$else i8086}
  1238. { On the i386 and x86_64 targets, we print out an error, because no CPU,
  1239. supported by these targets support 'POP CS' }
  1240. Message(asmr_e_pop_cs_not_valid);
  1241. {$endif i8086}
  1242. { I tried to convince Linus Torvalds to add
  1243. code to support ENTER instruction
  1244. (when raising a stack page fault)
  1245. but he replied that ENTER is a bad instruction and
  1246. Linux does not need to support it
  1247. So I think its at least a good idea to add a warning
  1248. if someone uses this in assembler code
  1249. FPC itself does not use it at all PM }
  1250. if (opcode=A_ENTER) and
  1251. (target_info.system in [system_i386_linux,system_i386_FreeBSD,system_i386_android]) then
  1252. Message(asmr_w_enter_not_supported_by_linux);
  1253. ai:=taicpu.op_none(opcode,siz);
  1254. ai.fileinfo:=filepos;
  1255. ai.SetOperandOrder(op_att);
  1256. ai.Ops:=Ops;
  1257. ai.Allocate_oper(Ops);
  1258. for i:=1 to Ops do
  1259. case operands[i].opr.typ of
  1260. OPR_CONSTANT :
  1261. ai.loadconst(i-1,operands[i].opr.val);
  1262. OPR_REGISTER:
  1263. ai.loadreg(i-1,operands[i].opr.reg);
  1264. OPR_SYMBOL:
  1265. {$ifdef i8086}
  1266. if operands[i].opr.symseg then
  1267. taicpu(ai).loadsegsymbol(i-1,operands[i].opr.symbol)
  1268. else
  1269. {$endif i8086}
  1270. ai.loadsymbol(i-1,operands[i].opr.symbol,operands[i].opr.symofs);
  1271. OPR_LOCAL :
  1272. with operands[i].opr do
  1273. begin
  1274. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1275. localscale,localgetoffset,localforceref);
  1276. ai.oper[i-1]^.localoper^.localsegment:=localsegment;
  1277. end;
  1278. OPR_REFERENCE:
  1279. begin
  1280. if (opcode<>A_XLAT) and not is_x86_string_op(opcode) then
  1281. optimize_ref(operands[i].opr.ref,true);
  1282. ai.loadref(i-1,operands[i].opr.ref);
  1283. if operands[i].size<>OS_NO then
  1284. begin
  1285. asize:=0;
  1286. case operands[i].size of
  1287. OS_8,OS_S8 :
  1288. asize:=OT_BITS8;
  1289. OS_16,OS_S16, OS_M16:
  1290. asize:=OT_BITS16;
  1291. OS_32,OS_S32 :
  1292. {$ifdef i8086}
  1293. if siz=S_FAR then
  1294. asize:=OT_FAR
  1295. else
  1296. asize:=OT_BITS32;
  1297. {$else i8086}
  1298. asize:=OT_BITS32;
  1299. {$endif i8086}
  1300. OS_F32,OS_M32 :
  1301. asize:=OT_BITS32;
  1302. OS_64,OS_S64:
  1303. begin
  1304. { Only FPU operations know about 64bit values, for all
  1305. integer operations it is seen as 32bit
  1306. this applies only to i386, see tw16622}
  1307. if gas_needsuffix[opcode] in [attsufFPU,attsufFPUint] then
  1308. asize:=OT_BITS64
  1309. {$ifdef i386}
  1310. else
  1311. asize:=OT_BITS32
  1312. {$endif i386}
  1313. ;
  1314. end;
  1315. OS_F64,OS_C64, OS_M64 :
  1316. asize:=OT_BITS64;
  1317. OS_F80 :
  1318. asize:=OT_BITS80;
  1319. OS_128,OS_M128,OS_MS128:
  1320. asize := OT_BITS128;
  1321. OS_M256,OS_MS256:
  1322. asize := OT_BITS256;
  1323. else
  1324. ;
  1325. end;
  1326. if asize<>0 then
  1327. ai.oper[i-1]^.ot:=(ai.oper[i-1]^.ot and not OT_SIZE_MASK) or asize;
  1328. end;
  1329. end;
  1330. else
  1331. ;
  1332. end;
  1333. { Condition ? }
  1334. if condition<>C_None then
  1335. ai.SetCondition(condition);
  1336. { Set is_jmp, it enables asmwriter to emit short jumps if appropriate }
  1337. if (opcode=A_JMP) or (opcode=A_JCC) then
  1338. ai.is_jmp := True;
  1339. { Concat the opcode or give an error }
  1340. if assigned(ai) then
  1341. p.concat(ai)
  1342. else
  1343. Message(asmr_e_invalid_opcode_and_operand);
  1344. result:=ai;
  1345. end;
  1346. end.