rax86.pas 41 KB

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