rax86.pas 49 KB

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