rax86.pas 50 KB

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