ra386.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 : paasmoutput);virtual;
  44. end;
  45. implementation
  46. uses
  47. {$ifdef NEWCG}
  48. cgbase,
  49. {$else}
  50. hcodegen,
  51. {$endif}
  52. globtype,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. opr.reg:=R_ST0
  161. else if ret_in_acc(procinfo^.returntype.def) then
  162. case procinfo^.returntype.def^.size of
  163. 1 : opr.reg:=R_AL;
  164. 2 : opr.reg:=R_AX;
  165. 3,4 : opr.reg:=R_EAX;
  166. else
  167. begin
  168. Message(asmr_e_cannot_use_RESULT_here);
  169. exit;
  170. end;
  171. end;
  172. Message1(asmr_h_RESULT_is_reg,reg2str(opr.reg));
  173. end;
  174. SetupResult:=res;
  175. end;
  176. {*****************************************************************************
  177. T386Instruction
  178. *****************************************************************************}
  179. procedure T386Instruction.AddReferenceSizes;
  180. { this will add the sizes for references like [esi] which do not
  181. have the size set yet, it will take only the size if the other
  182. operand is a register }
  183. var
  184. operand2,i : longint;
  185. s : pasmsymbol;
  186. so : longint;
  187. begin
  188. for i:=1to ops do
  189. begin
  190. operands[i]^.SetCorrectSize(opcode);
  191. if (operands[i]^.size=S_NO) then
  192. begin
  193. case operands[i]^.Opr.Typ of
  194. OPR_REFERENCE :
  195. begin
  196. if i=2 then
  197. operand2:=1
  198. else
  199. operand2:=2;
  200. if operand2<ops then
  201. begin
  202. { Only allow register as operand to take the size from }
  203. if operands[operand2]^.opr.typ=OPR_REGISTER then
  204. operands[i]^.size:=operands[operand2]^.size
  205. else
  206. begin
  207. { if no register then take the opsize (which is available with ATT),
  208. if not availble then give an error }
  209. if opsize<>S_NO then
  210. operands[i]^.size:=opsize
  211. else
  212. begin
  213. Message(asmr_e_unable_to_determine_reference_size);
  214. { recovery }
  215. operands[i]^.size:=S_L;
  216. end;
  217. end;
  218. end
  219. else
  220. begin
  221. if opsize<>S_NO then
  222. operands[i]^.size:=opsize
  223. end;
  224. end;
  225. OPR_SYMBOL :
  226. begin
  227. { Fix lea which need a reference }
  228. if opcode=A_LEA then
  229. begin
  230. s:=operands[i]^.opr.symbol;
  231. so:=operands[i]^.opr.symofs;
  232. operands[i]^.opr.typ:=OPR_REFERENCE;
  233. reset_reference(operands[i]^.opr.ref);
  234. operands[i]^.opr.ref.symbol:=s;
  235. operands[i]^.opr.ref.offset:=so;
  236. end;
  237. operands[i]^.size:=S_L;
  238. end;
  239. end;
  240. end;
  241. end;
  242. end;
  243. procedure T386Instruction.SetInstructionOpsize;
  244. begin
  245. if opsize<>S_NO then
  246. exit;
  247. case ops of
  248. 0 : ;
  249. 1 :
  250. { "push es" must be stored as a long PM }
  251. if ((opcode=A_PUSH) or
  252. (opcode=A_POP)) and
  253. (operands[1]^.opr.typ=OPR_REGISTER) and
  254. ((operands[1]^.opr.reg>=firstsreg) and
  255. (operands[1]^.opr.reg<=lastsreg)) then
  256. opsize:=S_L
  257. else
  258. opsize:=operands[1]^.size;
  259. 2 :
  260. begin
  261. case opcode of
  262. A_MOVZX,A_MOVSX :
  263. begin
  264. case operands[1]^.size of
  265. S_W :
  266. case operands[2]^.size of
  267. S_L :
  268. opsize:=S_WL;
  269. end;
  270. S_B :
  271. case operands[2]^.size of
  272. S_W :
  273. opsize:=S_BW;
  274. S_L :
  275. opsize:=S_BL;
  276. end;
  277. end;
  278. end;
  279. A_OUT :
  280. opsize:=operands[1]^.size;
  281. else
  282. opsize:=operands[2]^.size;
  283. end;
  284. end;
  285. 3 :
  286. opsize:=operands[3]^.size;
  287. end;
  288. end;
  289. procedure T386Instruction.CheckOperandSizes;
  290. var
  291. sizeerr : boolean;
  292. i : longint;
  293. begin
  294. { Check only the most common opcodes here, the others are done in
  295. the assembler pass }
  296. case opcode of
  297. A_PUSH,A_POP,A_DEC,A_INC,A_NOT,A_NEG,
  298. A_CMP,A_MOV,
  299. A_ADD,A_SUB,A_ADC,A_SBB,
  300. A_AND,A_OR,A_TEST,A_XOR: ;
  301. else
  302. exit;
  303. end;
  304. { Handle the BW,BL,WL separatly }
  305. sizeerr:=false;
  306. { special push/pop selector case }
  307. if ((opcode=A_PUSH) or
  308. (opcode=A_POP)) and
  309. (operands[1]^.opr.typ=OPR_REGISTER) and
  310. ((operands[1]^.opr.reg>=firstsreg) and
  311. (operands[1]^.opr.reg<=lastsreg)) then
  312. exit;
  313. if opsize in [S_BW,S_BL,S_WL] then
  314. begin
  315. if ops<>2 then
  316. sizeerr:=true
  317. else
  318. begin
  319. case opsize of
  320. S_BW :
  321. sizeerr:=(operands[1]^.size<>S_B) or (operands[2]^.size<>S_W);
  322. S_BL :
  323. sizeerr:=(operands[1]^.size<>S_B) or (operands[2]^.size<>S_L);
  324. S_WL :
  325. sizeerr:=(operands[1]^.size<>S_W) or (operands[2]^.size<>S_L);
  326. end;
  327. end;
  328. end
  329. else
  330. begin
  331. for i:=1to ops do
  332. begin
  333. if (operands[i]^.opr.typ<>OPR_CONSTANT) and
  334. (operands[i]^.size in [S_B,S_W,S_L]) and
  335. (operands[i]^.size<>opsize) then
  336. sizeerr:=true;
  337. end;
  338. end;
  339. if sizeerr then
  340. begin
  341. { if range checks are on then generate an error }
  342. if (cs_compilesystem in aktmoduleswitches) or
  343. not (cs_check_range in aktlocalswitches) then
  344. Message(asmr_w_size_suffix_and_dest_dont_match)
  345. else
  346. Message(asmr_e_size_suffix_and_dest_dont_match);
  347. end;
  348. end;
  349. { This check must be done with the operand in ATT order
  350. i.e.after swapping in the intel reader
  351. but before swapping in the NASM and TASM writers PM }
  352. procedure T386Instruction.CheckNonCommutativeOpcodes;
  353. begin
  354. if ((ops=2) and
  355. (operands[1]^.opr.typ=OPR_REGISTER) and
  356. (operands[2]^.opr.typ=OPR_REGISTER) and
  357. { if the first is ST and the second is also a register
  358. it is necessarily ST1 .. ST7 }
  359. (operands[1]^.opr.reg=R_ST)) or
  360. ((ops=1) and
  361. (operands[1]^.opr.typ=OPR_REGISTER) and
  362. (operands[1]^.opr.reg in [R_ST1..R_ST7])) or
  363. (ops=0) then
  364. if opcode=A_FSUBR then
  365. opcode:=A_FSUB
  366. else if opcode=A_FSUB then
  367. opcode:=A_FSUBR
  368. else if opcode=A_FDIVR then
  369. opcode:=A_FDIV
  370. else if opcode=A_FDIV then
  371. opcode:=A_FDIVR
  372. else if opcode=A_FSUBRP then
  373. opcode:=A_FSUBP
  374. else if opcode=A_FSUBP then
  375. opcode:=A_FSUBRP
  376. else if opcode=A_FDIVRP then
  377. opcode:=A_FDIVP
  378. else if opcode=A_FDIVP then
  379. opcode:=A_FDIVRP;
  380. end;
  381. {*****************************************************************************
  382. opcode Adding
  383. *****************************************************************************}
  384. procedure T386Instruction.ConcatInstruction(p : paasmoutput);
  385. var
  386. siz : topsize;
  387. i : longint;
  388. ai : paicpu;
  389. begin
  390. { Get Opsize }
  391. if (opsize<>S_NO) or (Ops=0) then
  392. siz:=opsize
  393. else
  394. begin
  395. if (Ops=2) and (operands[1]^.opr.typ=OPR_REGISTER) then
  396. siz:=operands[1]^.size
  397. else
  398. siz:=operands[Ops]^.size;
  399. end;
  400. { NASM does not support FADD without args
  401. as alias of FADDP
  402. and GNU AS interprets FADD without operand differently
  403. for version 2.9.1 and 2.9.5 !! }
  404. if (opcode=A_FADD) and (ops=0) then
  405. begin
  406. opcode:=A_FADDP;
  407. message(asmr_w_fadd_to_faddp);
  408. end;
  409. { I tried to convince Linus Torwald to add
  410. code to support ENTER instruction
  411. (when raising a stack page fault)
  412. but he replied that ENTER is a bad instruction and
  413. Linux does not need to support it
  414. So I think its at least a good idea to add a warning
  415. if someone uses this in assembler code
  416. FPC itself does not use it at all PM }
  417. if (opcode=A_ENTER) and ((target_info.target=target_i386_linux) or
  418. (target_info.target=target_i386_FreeBSD)) then
  419. begin
  420. message(asmr_w_enter_not_supported_by_linux);
  421. end;
  422. ai:=new(paicpu,op_none(opcode,siz));
  423. ai^.Ops:=Ops;
  424. for i:=1to Ops do
  425. begin
  426. case operands[i]^.opr.typ of
  427. OPR_CONSTANT :
  428. ai^.loadconst(i-1,operands[i]^.opr.val);
  429. OPR_REGISTER:
  430. ai^.loadreg(i-1,operands[i]^.opr.reg);
  431. OPR_SYMBOL:
  432. ai^.loadsymbol(i-1,operands[i]^.opr.symbol,operands[i]^.opr.symofs);
  433. OPR_REFERENCE:
  434. ai^.loadref(i-1,newreference(operands[i]^.opr.ref));
  435. end;
  436. end;
  437. { Condition ? }
  438. if condition<>C_None then
  439. ai^.SetCondition(condition);
  440. { Concat the opcode or give an error }
  441. if assigned(ai) then
  442. p^.concat(ai)
  443. else
  444. Message(asmr_e_invalid_opcode_and_operand);
  445. end;
  446. end.
  447. {
  448. $Log$
  449. Revision 1.7 2000-10-08 10:26:33 peter
  450. * merged @result fix from Pierre
  451. Revision 1.6 2000/09/24 21:33:47 peter
  452. * message updates merges
  453. Revision 1.5 2000/09/24 15:06:25 peter
  454. * use defines.inc
  455. Revision 1.4 2000/09/16 12:22:52 peter
  456. * freebsd support merged
  457. Revision 1.3 2000/09/03 11:44:00 peter
  458. * error for not specified operand size, which is now required for
  459. newer binutils (merged)
  460. * previous commit fix for tcflw (merged)
  461. Revision 1.2 2000/07/13 11:32:47 michael
  462. + removed logs
  463. }