rax86.pas 51 KB

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