ra386.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Carl Eric Codere and Peter Vreman
  4. Handles the common i386 assembler reader routines
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit Ra386;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. aasm,cpubase,RAUtils;
  23. { Parser helpers }
  24. function is_prefix(t:tasmop):boolean;
  25. function is_override(t:tasmop):boolean;
  26. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  27. Function CheckOverride(overrideop,op:tasmop): Boolean;
  28. Procedure FWaitWarning;
  29. type
  30. P386Operand=^T386Operand;
  31. T386Operand=object(TOperand)
  32. Procedure SetCorrectSize(opcode:tasmop);virtual;
  33. Function SetupResult : boolean;virtual;
  34. end;
  35. P386Instruction=^T386Instruction;
  36. T386Instruction=object(TInstruction)
  37. { Operand sizes }
  38. procedure AddReferenceSizes;
  39. procedure SetInstructionOpsize;
  40. procedure CheckOperandSizes;
  41. procedure CheckNonCommutativeOpcodes;
  42. { opcode adding }
  43. procedure ConcatInstruction(p : taasmoutput);virtual;
  44. end;
  45. implementation
  46. uses
  47. {$ifdef NEWCG}
  48. cgbase,
  49. {$else}
  50. hcodegen,
  51. {$endif}
  52. globtype,symconst,symdef,systems,types,globals,verbose,cpuasm;
  53. {*****************************************************************************
  54. Parser Helpers
  55. *****************************************************************************}
  56. function is_prefix(t:tasmop):boolean;
  57. var
  58. i : longint;
  59. Begin
  60. is_prefix:=false;
  61. for i:=1 to AsmPrefixes do
  62. if t=AsmPrefix[i-1] then
  63. begin
  64. is_prefix:=true;
  65. exit;
  66. end;
  67. end;
  68. function is_override(t:tasmop):boolean;
  69. var
  70. i : longint;
  71. Begin
  72. is_override:=false;
  73. for i:=1 to AsmOverrides do
  74. if t=AsmOverride[i-1] then
  75. begin
  76. is_override:=true;
  77. exit;
  78. end;
  79. end;
  80. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  81. { Checks if the prefix is valid with the following opcode }
  82. { return false if not, otherwise true }
  83. Begin
  84. CheckPrefix := TRUE;
  85. (* Case prefix of
  86. A_REP,A_REPNE,A_REPE:
  87. Case opcode Of
  88. A_SCASB,A_SCASW,A_SCASD,
  89. A_INS,A_OUTS,A_MOVS,A_CMPS,A_LODS,A_STOS:;
  90. Else
  91. Begin
  92. CheckPrefix := FALSE;
  93. exit;
  94. end;
  95. end; { case }
  96. A_LOCK:
  97. Case opcode Of
  98. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,A_ADC,A_SBB,A_AND,A_SUB,
  99. A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  100. Else
  101. Begin
  102. CheckPrefix := FALSE;
  103. Exit;
  104. end;
  105. end; { case }
  106. A_NONE: exit; { no prefix here }
  107. else
  108. CheckPrefix := FALSE;
  109. end; { end case } *)
  110. end;
  111. Function CheckOverride(overrideop,op:tasmop): Boolean;
  112. { Check if the override is valid, and if so then }
  113. { update the instr variable accordingly. }
  114. Begin
  115. CheckOverride := true;
  116. { Case instr.getinstruction of
  117. A_MOVS,A_XLAT,A_CMPS:
  118. Begin
  119. CheckOverride := TRUE;
  120. Message(assem_e_segment_override_not_supported);
  121. end
  122. end }
  123. end;
  124. Procedure FWaitWarning;
  125. begin
  126. if (target_info.target=target_i386_GO32V2) and (cs_fp_emulation in aktmoduleswitches) then
  127. Message(asmr_w_fwait_emu_prob);
  128. end;
  129. {*****************************************************************************
  130. T386Operand
  131. *****************************************************************************}
  132. Procedure T386Operand.SetCorrectSize(opcode:tasmop);
  133. begin
  134. if att_needsuffix[opcode]=attsufFPU then
  135. begin
  136. case size of
  137. S_L : size:=S_FS;
  138. S_IQ : size:=S_FL;
  139. end;
  140. end
  141. else if att_needsuffix[opcode]=attsufFPUint then
  142. begin
  143. case size of
  144. S_W : size:=S_IS;
  145. S_L : size:=S_IL;
  146. end;
  147. end;
  148. end;
  149. Function T386Operand.SetupResult:boolean;
  150. var
  151. Res : boolean;
  152. Begin
  153. Res:=TOperand.setupResult;
  154. { replace by ref by register if not place was
  155. reserved on stack }
  156. if res and (procinfo^.return_offset=0) then
  157. begin
  158. opr.typ:=OPR_REGISTER;
  159. if is_fpu(procinfo^.returntype.def) then
  160. begin
  161. opr.reg:=R_ST0;
  162. case pfloatdef(procinfo^.returntype.def)^.typ of
  163. s32real : size:=S_FS;
  164. s64real : size:=S_FL;
  165. s80real : size:=S_FX;
  166. s64comp : size:=S_IQ;
  167. else
  168. begin
  169. Message(asmr_e_cannot_use_RESULT_here);
  170. res:=false;
  171. end;
  172. end;
  173. end
  174. else if ret_in_acc(procinfo^.returntype.def) then
  175. case procinfo^.returntype.def^.size of
  176. 1 : begin
  177. opr.reg:=R_AL;
  178. size:=S_B;
  179. end;
  180. 2 : begin
  181. opr.reg:=R_AX;
  182. size:=S_W;
  183. end;
  184. 3,4 : begin
  185. opr.reg:=R_EAX;
  186. size:=S_L;
  187. end;
  188. else
  189. begin
  190. Message(asmr_e_cannot_use_RESULT_here);
  191. res:=false;
  192. end;
  193. end;
  194. Message1(asmr_h_RESULT_is_reg,reg2str(opr.reg));
  195. end;
  196. SetupResult:=res;
  197. end;
  198. {*****************************************************************************
  199. T386Instruction
  200. *****************************************************************************}
  201. procedure T386Instruction.AddReferenceSizes;
  202. { this will add the sizes for references like [esi] which do not
  203. have the size set yet, it will take only the size if the other
  204. operand is a register }
  205. var
  206. operand2,i : longint;
  207. s : pasmsymbol;
  208. so : longint;
  209. begin
  210. for i:=1to ops do
  211. begin
  212. operands[i]^.SetCorrectSize(opcode);
  213. if (operands[i]^.size=S_NO) then
  214. begin
  215. case operands[i]^.Opr.Typ of
  216. OPR_REFERENCE :
  217. begin
  218. if i=2 then
  219. operand2:=1
  220. else
  221. operand2:=2;
  222. if operand2<ops then
  223. begin
  224. { Only allow register as operand to take the size from }
  225. if operands[operand2]^.opr.typ=OPR_REGISTER then
  226. operands[i]^.size:=operands[operand2]^.size
  227. else
  228. begin
  229. { if no register then take the opsize (which is available with ATT),
  230. if not availble then give an error }
  231. if opsize<>S_NO then
  232. operands[i]^.size:=opsize
  233. else
  234. begin
  235. Message(asmr_e_unable_to_determine_reference_size);
  236. { recovery }
  237. operands[i]^.size:=S_L;
  238. end;
  239. end;
  240. end
  241. else
  242. begin
  243. if opsize<>S_NO then
  244. operands[i]^.size:=opsize
  245. end;
  246. end;
  247. OPR_SYMBOL :
  248. begin
  249. { Fix lea which need a reference }
  250. if opcode=A_LEA then
  251. begin
  252. s:=operands[i]^.opr.symbol;
  253. so:=operands[i]^.opr.symofs;
  254. operands[i]^.opr.typ:=OPR_REFERENCE;
  255. reset_reference(operands[i]^.opr.ref);
  256. operands[i]^.opr.ref.symbol:=s;
  257. operands[i]^.opr.ref.offset:=so;
  258. end;
  259. operands[i]^.size:=S_L;
  260. end;
  261. end;
  262. end;
  263. end;
  264. end;
  265. procedure T386Instruction.SetInstructionOpsize;
  266. begin
  267. if opsize<>S_NO then
  268. exit;
  269. case ops of
  270. 0 : ;
  271. 1 :
  272. { "push es" must be stored as a long PM }
  273. if ((opcode=A_PUSH) or
  274. (opcode=A_POP)) and
  275. (operands[1]^.opr.typ=OPR_REGISTER) and
  276. ((operands[1]^.opr.reg>=firstsreg) and
  277. (operands[1]^.opr.reg<=lastsreg)) then
  278. opsize:=S_L
  279. else
  280. opsize:=operands[1]^.size;
  281. 2 :
  282. begin
  283. case opcode of
  284. A_MOVZX,A_MOVSX :
  285. begin
  286. case operands[1]^.size of
  287. S_W :
  288. case operands[2]^.size of
  289. S_L :
  290. opsize:=S_WL;
  291. end;
  292. S_B :
  293. case operands[2]^.size of
  294. S_W :
  295. opsize:=S_BW;
  296. S_L :
  297. opsize:=S_BL;
  298. end;
  299. end;
  300. end;
  301. A_OUT :
  302. opsize:=operands[1]^.size;
  303. else
  304. opsize:=operands[2]^.size;
  305. end;
  306. end;
  307. 3 :
  308. opsize:=operands[3]^.size;
  309. end;
  310. end;
  311. procedure T386Instruction.CheckOperandSizes;
  312. var
  313. sizeerr : boolean;
  314. i : longint;
  315. begin
  316. { Check only the most common opcodes here, the others are done in
  317. the assembler pass }
  318. case opcode of
  319. A_PUSH,A_POP,A_DEC,A_INC,A_NOT,A_NEG,
  320. A_CMP,A_MOV,
  321. A_ADD,A_SUB,A_ADC,A_SBB,
  322. A_AND,A_OR,A_TEST,A_XOR: ;
  323. else
  324. exit;
  325. end;
  326. { Handle the BW,BL,WL separatly }
  327. sizeerr:=false;
  328. { special push/pop selector case }
  329. if ((opcode=A_PUSH) or
  330. (opcode=A_POP)) and
  331. (operands[1]^.opr.typ=OPR_REGISTER) and
  332. ((operands[1]^.opr.reg>=firstsreg) and
  333. (operands[1]^.opr.reg<=lastsreg)) then
  334. exit;
  335. if opsize in [S_BW,S_BL,S_WL] then
  336. begin
  337. if ops<>2 then
  338. sizeerr:=true
  339. else
  340. begin
  341. case opsize of
  342. S_BW :
  343. sizeerr:=(operands[1]^.size<>S_B) or (operands[2]^.size<>S_W);
  344. S_BL :
  345. sizeerr:=(operands[1]^.size<>S_B) or (operands[2]^.size<>S_L);
  346. S_WL :
  347. sizeerr:=(operands[1]^.size<>S_W) or (operands[2]^.size<>S_L);
  348. end;
  349. end;
  350. end
  351. else
  352. begin
  353. for i:=1to ops do
  354. begin
  355. if (operands[i]^.opr.typ<>OPR_CONSTANT) and
  356. (operands[i]^.size in [S_B,S_W,S_L]) and
  357. (operands[i]^.size<>opsize) then
  358. sizeerr:=true;
  359. end;
  360. end;
  361. if sizeerr then
  362. begin
  363. { if range checks are on then generate an error }
  364. if (cs_compilesystem in aktmoduleswitches) or
  365. not (cs_check_range in aktlocalswitches) then
  366. Message(asmr_w_size_suffix_and_dest_dont_match)
  367. else
  368. Message(asmr_e_size_suffix_and_dest_dont_match);
  369. end;
  370. end;
  371. { This check must be done with the operand in ATT order
  372. i.e.after swapping in the intel reader
  373. but before swapping in the NASM and TASM writers PM }
  374. procedure T386Instruction.CheckNonCommutativeOpcodes;
  375. begin
  376. if ((ops=2) and
  377. (operands[1]^.opr.typ=OPR_REGISTER) and
  378. (operands[2]^.opr.typ=OPR_REGISTER) and
  379. { if the first is ST and the second is also a register
  380. it is necessarily ST1 .. ST7 }
  381. (operands[1]^.opr.reg=R_ST)) or
  382. ((ops=1) and
  383. (operands[1]^.opr.typ=OPR_REGISTER) and
  384. (operands[1]^.opr.reg in [R_ST1..R_ST7])) or
  385. (ops=0) then
  386. if opcode=A_FSUBR then
  387. opcode:=A_FSUB
  388. else if opcode=A_FSUB then
  389. opcode:=A_FSUBR
  390. else if opcode=A_FDIVR then
  391. opcode:=A_FDIV
  392. else if opcode=A_FDIV then
  393. opcode:=A_FDIVR
  394. else if opcode=A_FSUBRP then
  395. opcode:=A_FSUBP
  396. else if opcode=A_FSUBP then
  397. opcode:=A_FSUBRP
  398. else if opcode=A_FDIVRP then
  399. opcode:=A_FDIVP
  400. else if opcode=A_FDIVP then
  401. opcode:=A_FDIVRP;
  402. end;
  403. {*****************************************************************************
  404. opcode Adding
  405. *****************************************************************************}
  406. procedure T386Instruction.ConcatInstruction(p : taasmoutput);
  407. var
  408. siz : topsize;
  409. i : longint;
  410. ai : taicpu;
  411. begin
  412. { Get Opsize }
  413. if (opsize<>S_NO) or (Ops=0) then
  414. siz:=opsize
  415. else
  416. begin
  417. if (Ops=2) and (operands[1]^.opr.typ=OPR_REGISTER) then
  418. siz:=operands[1]^.size
  419. else
  420. siz:=operands[Ops]^.size;
  421. end;
  422. { NASM does not support FADD without args
  423. as alias of FADDP
  424. and GNU AS interprets FADD without operand differently
  425. for version 2.9.1 and 2.9.5 !! }
  426. if (opcode=A_FADD) and (ops=0) then
  427. begin
  428. opcode:=A_FADDP;
  429. message(asmr_w_fadd_to_faddp);
  430. end;
  431. { I tried to convince Linus Torwald to add
  432. code to support ENTER instruction
  433. (when raising a stack page fault)
  434. but he replied that ENTER is a bad instruction and
  435. Linux does not need to support it
  436. So I think its at least a good idea to add a warning
  437. if someone uses this in assembler code
  438. FPC itself does not use it at all PM }
  439. if (opcode=A_ENTER) and ((target_info.target=target_i386_linux) or
  440. (target_info.target=target_i386_FreeBSD)) then
  441. begin
  442. message(asmr_w_enter_not_supported_by_linux);
  443. end;
  444. ai:=taicpu.op_none(opcode,siz);
  445. ai.Ops:=Ops;
  446. for i:=1to Ops do
  447. begin
  448. case operands[i]^.opr.typ of
  449. OPR_CONSTANT :
  450. ai.loadconst(i-1,operands[i]^.opr.val);
  451. OPR_REGISTER:
  452. ai.loadreg(i-1,operands[i]^.opr.reg);
  453. OPR_SYMBOL:
  454. ai.loadsymbol(i-1,operands[i]^.opr.symbol,operands[i]^.opr.symofs);
  455. OPR_REFERENCE:
  456. ai.loadref(i-1,newreference(operands[i]^.opr.ref));
  457. end;
  458. end;
  459. { Condition ? }
  460. if condition<>C_None then
  461. ai.SetCondition(condition);
  462. { Concat the opcode or give an error }
  463. if assigned(ai) then
  464. p.concat(ai)
  465. else
  466. Message(asmr_e_invalid_opcode_and_operand);
  467. end;
  468. end.
  469. {
  470. $Log$
  471. Revision 1.4 2000-12-25 00:07:34 peter
  472. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  473. tlinkedlist objects)
  474. Revision 1.3 2000/11/29 00:30:50 florian
  475. * unused units removed from uses clause
  476. * some changes for widestrings
  477. Revision 1.2 2000/10/31 22:30:14 peter
  478. * merged asm result patch part 2
  479. Revision 1.1 2000/10/15 09:47:43 peter
  480. * moved to i386/
  481. Revision 1.7 2000/10/08 10:26:33 peter
  482. * merged @result fix from Pierre
  483. Revision 1.6 2000/09/24 21:33:47 peter
  484. * message updates merges
  485. Revision 1.5 2000/09/24 15:06:25 peter
  486. * use defines.inc
  487. Revision 1.4 2000/09/16 12:22:52 peter
  488. * freebsd support merged
  489. Revision 1.3 2000/09/03 11:44:00 peter
  490. * error for not specified operand size, which is now required for
  491. newer binutils (merged)
  492. * previous commit fix for tcflw (merged)
  493. Revision 1.2 2000/07/13 11:32:47 michael
  494. + removed logs
  495. }