ra386.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. end;
  34. P386Instruction=^T386Instruction;
  35. T386Instruction=object(TInstruction)
  36. { Operand sizes }
  37. procedure AddReferenceSizes;
  38. procedure SetInstructionOpsize;
  39. procedure CheckOperandSizes;
  40. procedure CheckNonCommutativeOpcodes;
  41. { opcode adding }
  42. procedure ConcatInstruction(p : paasmoutput);virtual;
  43. end;
  44. implementation
  45. uses
  46. globtype,systems,globals,verbose,cpuasm;
  47. {*****************************************************************************
  48. Parser Helpers
  49. *****************************************************************************}
  50. function is_prefix(t:tasmop):boolean;
  51. var
  52. i : longint;
  53. Begin
  54. is_prefix:=false;
  55. for i:=1 to AsmPrefixes do
  56. if t=AsmPrefix[i-1] then
  57. begin
  58. is_prefix:=true;
  59. exit;
  60. end;
  61. end;
  62. function is_override(t:tasmop):boolean;
  63. var
  64. i : longint;
  65. Begin
  66. is_override:=false;
  67. for i:=1 to AsmOverrides do
  68. if t=AsmOverride[i-1] then
  69. begin
  70. is_override:=true;
  71. exit;
  72. end;
  73. end;
  74. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  75. { Checks if the prefix is valid with the following opcode }
  76. { return false if not, otherwise true }
  77. Begin
  78. CheckPrefix := TRUE;
  79. (* Case prefix of
  80. A_REP,A_REPNE,A_REPE:
  81. Case opcode Of
  82. A_SCASB,A_SCASW,A_SCASD,
  83. A_INS,A_OUTS,A_MOVS,A_CMPS,A_LODS,A_STOS:;
  84. Else
  85. Begin
  86. CheckPrefix := FALSE;
  87. exit;
  88. end;
  89. end; { case }
  90. A_LOCK:
  91. Case opcode Of
  92. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,A_ADC,A_SBB,A_AND,A_SUB,
  93. A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  94. Else
  95. Begin
  96. CheckPrefix := FALSE;
  97. Exit;
  98. end;
  99. end; { case }
  100. A_NONE: exit; { no prefix here }
  101. else
  102. CheckPrefix := FALSE;
  103. end; { end case } *)
  104. end;
  105. Function CheckOverride(overrideop,op:tasmop): Boolean;
  106. { Check if the override is valid, and if so then }
  107. { update the instr variable accordingly. }
  108. Begin
  109. CheckOverride := true;
  110. { Case instr.getinstruction of
  111. A_MOVS,A_XLAT,A_CMPS:
  112. Begin
  113. CheckOverride := TRUE;
  114. Message(assem_e_segment_override_not_supported);
  115. end
  116. end }
  117. end;
  118. Procedure FWaitWarning;
  119. begin
  120. if (target_info.target=target_i386_GO32V2) and (cs_fp_emulation in aktmoduleswitches) then
  121. Message(asmr_w_fwait_emu_prob);
  122. end;
  123. {*****************************************************************************
  124. T386Operand
  125. *****************************************************************************}
  126. Procedure T386Operand.SetCorrectSize(opcode:tasmop);
  127. begin
  128. if att_needsuffix[opcode]=attsufFPU then
  129. begin
  130. case size of
  131. S_L : size:=S_FS;
  132. S_IQ : size:=S_FL;
  133. end;
  134. end
  135. else if att_needsuffix[opcode]=attsufFPUint then
  136. begin
  137. case size of
  138. S_W : size:=S_IS;
  139. S_L : size:=S_IL;
  140. end;
  141. end;
  142. end;
  143. {*****************************************************************************
  144. T386Instruction
  145. *****************************************************************************}
  146. procedure T386Instruction.AddReferenceSizes;
  147. { this will add the sizes for references like [esi] which do not
  148. have the size set yet, it will take only the size if the other
  149. operand is a register }
  150. var
  151. operand2,i : longint;
  152. s : pasmsymbol;
  153. so : longint;
  154. begin
  155. for i:=1to ops do
  156. begin
  157. operands[i]^.SetCorrectSize(opcode);
  158. if (operands[i]^.size=S_NO) then
  159. begin
  160. case operands[i]^.Opr.Typ of
  161. OPR_REFERENCE :
  162. begin
  163. if i=2 then
  164. operand2:=1
  165. else
  166. operand2:=2;
  167. if operand2<ops then
  168. begin
  169. { Only allow register as operand to take the size from }
  170. if operands[operand2]^.opr.typ=OPR_REGISTER then
  171. operands[i]^.size:=operands[operand2]^.size
  172. else
  173. begin
  174. { if no register then take the opsize (which is available with ATT),
  175. if not availble then give an error }
  176. if opsize<>S_NO then
  177. operands[i]^.size:=opsize
  178. else
  179. begin
  180. Comment(V_Error,'No size specified and unable to determine the size of the operands');
  181. { recovery }
  182. operands[i]^.size:=S_L;
  183. end;
  184. end;
  185. end
  186. else
  187. begin
  188. if opsize<>S_NO then
  189. operands[i]^.size:=opsize
  190. end;
  191. end;
  192. OPR_SYMBOL :
  193. begin
  194. { Fix lea which need a reference }
  195. if opcode=A_LEA then
  196. begin
  197. s:=operands[i]^.opr.symbol;
  198. so:=operands[i]^.opr.symofs;
  199. operands[i]^.opr.typ:=OPR_REFERENCE;
  200. reset_reference(operands[i]^.opr.ref);
  201. operands[i]^.opr.ref.symbol:=s;
  202. operands[i]^.opr.ref.offset:=so;
  203. end;
  204. operands[i]^.size:=S_L;
  205. end;
  206. end;
  207. end;
  208. end;
  209. end;
  210. procedure T386Instruction.SetInstructionOpsize;
  211. begin
  212. if opsize<>S_NO then
  213. exit;
  214. case ops of
  215. 0 : ;
  216. 1 :
  217. { "push es" must be stored as a long PM }
  218. if ((opcode=A_PUSH) or
  219. (opcode=A_POP)) and
  220. (operands[1]^.opr.typ=OPR_REGISTER) and
  221. ((operands[1]^.opr.reg>=firstsreg) and
  222. (operands[1]^.opr.reg<=lastsreg)) then
  223. opsize:=S_L
  224. else
  225. opsize:=operands[1]^.size;
  226. 2 :
  227. begin
  228. case opcode of
  229. A_MOVZX,A_MOVSX :
  230. begin
  231. case operands[1]^.size of
  232. S_W :
  233. case operands[2]^.size of
  234. S_L :
  235. opsize:=S_WL;
  236. end;
  237. S_B :
  238. case operands[2]^.size of
  239. S_W :
  240. opsize:=S_BW;
  241. S_L :
  242. opsize:=S_BL;
  243. end;
  244. end;
  245. end;
  246. A_OUT :
  247. opsize:=operands[1]^.size;
  248. else
  249. opsize:=operands[2]^.size;
  250. end;
  251. end;
  252. 3 :
  253. opsize:=operands[3]^.size;
  254. end;
  255. end;
  256. procedure T386Instruction.CheckOperandSizes;
  257. var
  258. sizeerr : boolean;
  259. i : longint;
  260. begin
  261. { Check only the most common opcodes here, the others are done in
  262. the assembler pass }
  263. case opcode of
  264. A_PUSH,A_POP,A_DEC,A_INC,A_NOT,A_NEG,
  265. A_CMP,A_MOV,
  266. A_ADD,A_SUB,A_ADC,A_SBB,
  267. A_AND,A_OR,A_TEST,A_XOR: ;
  268. else
  269. exit;
  270. end;
  271. { Handle the BW,BL,WL separatly }
  272. sizeerr:=false;
  273. { special push/pop selector case }
  274. if ((opcode=A_PUSH) or
  275. (opcode=A_POP)) and
  276. (operands[1]^.opr.typ=OPR_REGISTER) and
  277. ((operands[1]^.opr.reg>=firstsreg) and
  278. (operands[1]^.opr.reg<=lastsreg)) then
  279. exit;
  280. if opsize in [S_BW,S_BL,S_WL] then
  281. begin
  282. if ops<>2 then
  283. sizeerr:=true
  284. else
  285. begin
  286. case opsize of
  287. S_BW :
  288. sizeerr:=(operands[1]^.size<>S_B) or (operands[2]^.size<>S_W);
  289. S_BL :
  290. sizeerr:=(operands[1]^.size<>S_B) or (operands[2]^.size<>S_L);
  291. S_WL :
  292. sizeerr:=(operands[1]^.size<>S_W) or (operands[2]^.size<>S_L);
  293. end;
  294. end;
  295. end
  296. else
  297. begin
  298. for i:=1to ops do
  299. begin
  300. if (operands[i]^.opr.typ<>OPR_CONSTANT) and
  301. (operands[i]^.size in [S_B,S_W,S_L]) and
  302. (operands[i]^.size<>opsize) then
  303. sizeerr:=true;
  304. end;
  305. end;
  306. if sizeerr then
  307. begin
  308. { if range checks are on then generate an error }
  309. if (cs_compilesystem in aktmoduleswitches) or
  310. not (cs_check_range in aktlocalswitches) then
  311. Message(asmr_w_size_suffix_and_dest_dont_match)
  312. else
  313. Message(asmr_e_size_suffix_and_dest_dont_match);
  314. end;
  315. end;
  316. { This check must be done with the operand in ATT order
  317. i.e.after swapping in the intel reader
  318. but before swapping in the NASM and TASM writers PM }
  319. procedure T386Instruction.CheckNonCommutativeOpcodes;
  320. begin
  321. if ((ops=2) and
  322. (operands[1]^.opr.typ=OPR_REGISTER) and
  323. (operands[2]^.opr.typ=OPR_REGISTER) and
  324. { if the first is ST and the second is also a register
  325. it is necessarily ST1 .. ST7 }
  326. (operands[1]^.opr.reg=R_ST)) or
  327. ((ops=1) and
  328. (operands[1]^.opr.typ=OPR_REGISTER) and
  329. (operands[1]^.opr.reg in [R_ST1..R_ST7])) or
  330. (ops=0) then
  331. if opcode=A_FSUBR then
  332. opcode:=A_FSUB
  333. else if opcode=A_FSUB then
  334. opcode:=A_FSUBR
  335. else if opcode=A_FDIVR then
  336. opcode:=A_FDIV
  337. else if opcode=A_FDIV then
  338. opcode:=A_FDIVR
  339. else if opcode=A_FSUBRP then
  340. opcode:=A_FSUBP
  341. else if opcode=A_FSUBP then
  342. opcode:=A_FSUBRP
  343. else if opcode=A_FDIVRP then
  344. opcode:=A_FDIVP
  345. else if opcode=A_FDIVP then
  346. opcode:=A_FDIVRP;
  347. end;
  348. {*****************************************************************************
  349. opcode Adding
  350. *****************************************************************************}
  351. procedure T386Instruction.ConcatInstruction(p : paasmoutput);
  352. var
  353. siz : topsize;
  354. i : longint;
  355. ai : paicpu;
  356. begin
  357. { Get Opsize }
  358. if (opsize<>S_NO) or (Ops=0) then
  359. siz:=opsize
  360. else
  361. begin
  362. if (Ops=2) and (operands[1]^.opr.typ=OPR_REGISTER) then
  363. siz:=operands[1]^.size
  364. else
  365. siz:=operands[Ops]^.size;
  366. end;
  367. { NASM does not support FADD without args
  368. as alias of FADDP
  369. and GNU AS interprets FADD without operand differently
  370. for version 2.9.1 and 2.9.5 !! }
  371. if (opcode=A_FADD) and (ops=0) then
  372. begin
  373. opcode:=A_FADDP;
  374. message(asmr_w_fadd_to_faddp);
  375. end;
  376. { I tried to convince Linus Torwald to add
  377. code to support ENTER instruction
  378. (when raising a stack page fault)
  379. but he replied that ENTER is a bad instruction and
  380. Linux does not need to support it
  381. So I think its at least a good idea to add a warning
  382. if someone uses this in assembler code
  383. FPC itself does not use it at all PM }
  384. if (opcode=A_ENTER) and ((target_info.target=target_i386_linux) or
  385. (target_info.target=target_i386_FreeBSD)) then
  386. begin
  387. message(asmr_w_enter_not_supported_by_linux);
  388. end;
  389. ai:=new(paicpu,op_none(opcode,siz));
  390. ai^.Ops:=Ops;
  391. for i:=1to Ops do
  392. begin
  393. case operands[i]^.opr.typ of
  394. OPR_CONSTANT :
  395. ai^.loadconst(i-1,operands[i]^.opr.val);
  396. OPR_REGISTER:
  397. ai^.loadreg(i-1,operands[i]^.opr.reg);
  398. OPR_SYMBOL:
  399. ai^.loadsymbol(i-1,operands[i]^.opr.symbol,operands[i]^.opr.symofs);
  400. OPR_REFERENCE:
  401. ai^.loadref(i-1,newreference(operands[i]^.opr.ref));
  402. end;
  403. end;
  404. { Condition ? }
  405. if condition<>C_None then
  406. ai^.SetCondition(condition);
  407. { Concat the opcode or give an error }
  408. if assigned(ai) then
  409. p^.concat(ai)
  410. else
  411. Message(asmr_e_invalid_opcode_and_operand);
  412. end;
  413. end.
  414. {
  415. $Log$
  416. Revision 1.5 2000-09-24 15:06:25 peter
  417. * use defines.inc
  418. Revision 1.4 2000/09/16 12:22:52 peter
  419. * freebsd support merged
  420. Revision 1.3 2000/09/03 11:44:00 peter
  421. * error for not specified operand size, which is now required for
  422. newer binutils (merged)
  423. * previous commit fix for tcflw (merged)
  424. Revision 1.2 2000/07/13 11:32:47 michael
  425. + removed logs
  426. }