ra386.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. interface
  20. uses
  21. aasm,cpubase,RAUtils;
  22. { Parser helpers }
  23. function is_prefix(t:tasmop):boolean;
  24. function is_override(t:tasmop):boolean;
  25. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  26. Function CheckOverride(overrideop,op:tasmop): Boolean;
  27. Procedure FWaitWarning;
  28. type
  29. P386Operand=^T386Operand;
  30. T386Operand=object(TOperand)
  31. Procedure SetCorrectSize(opcode:tasmop);virtual;
  32. end;
  33. P386Instruction=^T386Instruction;
  34. T386Instruction=object(TInstruction)
  35. { Operand sizes }
  36. procedure AddReferenceSizes;
  37. procedure SetInstructionOpsize;
  38. procedure CheckOperandSizes;
  39. procedure CheckNonCommutativeOpcodes;
  40. { opcode adding }
  41. procedure ConcatInstruction(p : paasmoutput);virtual;
  42. end;
  43. implementation
  44. uses
  45. globtype,systems,globals,verbose,cpuasm;
  46. {*****************************************************************************
  47. Parser Helpers
  48. *****************************************************************************}
  49. function is_prefix(t:tasmop):boolean;
  50. var
  51. i : longint;
  52. Begin
  53. is_prefix:=false;
  54. for i:=1 to AsmPrefixes do
  55. if t=AsmPrefix[i-1] then
  56. begin
  57. is_prefix:=true;
  58. exit;
  59. end;
  60. end;
  61. function is_override(t:tasmop):boolean;
  62. var
  63. i : longint;
  64. Begin
  65. is_override:=false;
  66. for i:=1 to AsmOverrides do
  67. if t=AsmOverride[i-1] then
  68. begin
  69. is_override:=true;
  70. exit;
  71. end;
  72. end;
  73. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  74. { Checks if the prefix is valid with the following opcode }
  75. { return false if not, otherwise true }
  76. Begin
  77. CheckPrefix := TRUE;
  78. (* Case prefix of
  79. A_REP,A_REPNE,A_REPE:
  80. Case opcode Of
  81. A_SCASB,A_SCASW,A_SCASD,
  82. A_INS,A_OUTS,A_MOVS,A_CMPS,A_LODS,A_STOS:;
  83. Else
  84. Begin
  85. CheckPrefix := FALSE;
  86. exit;
  87. end;
  88. end; { case }
  89. A_LOCK:
  90. Case opcode Of
  91. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,A_ADC,A_SBB,A_AND,A_SUB,
  92. A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  93. Else
  94. Begin
  95. CheckPrefix := FALSE;
  96. Exit;
  97. end;
  98. end; { case }
  99. A_NONE: exit; { no prefix here }
  100. else
  101. CheckPrefix := FALSE;
  102. end; { end case } *)
  103. end;
  104. Function CheckOverride(overrideop,op:tasmop): Boolean;
  105. { Check if the override is valid, and if so then }
  106. { update the instr variable accordingly. }
  107. Begin
  108. CheckOverride := true;
  109. { Case instr.getinstruction of
  110. A_MOVS,A_XLAT,A_CMPS:
  111. Begin
  112. CheckOverride := TRUE;
  113. Message(assem_e_segment_override_not_supported);
  114. end
  115. end }
  116. end;
  117. Procedure FWaitWarning;
  118. begin
  119. if (target_info.target=target_i386_GO32V2) and (cs_fp_emulation in aktmoduleswitches) then
  120. Message(asmr_w_fwait_emu_prob);
  121. end;
  122. {*****************************************************************************
  123. T386Operand
  124. *****************************************************************************}
  125. Procedure T386Operand.SetCorrectSize(opcode:tasmop);
  126. begin
  127. if att_needsuffix[opcode]=attsufFPU then
  128. begin
  129. case size of
  130. S_L : size:=S_FS;
  131. S_IQ : size:=S_FL;
  132. end;
  133. end
  134. else if att_needsuffix[opcode]=attsufFPUint then
  135. begin
  136. case size of
  137. S_W : size:=S_IS;
  138. S_L : size:=S_IL;
  139. end;
  140. end;
  141. end;
  142. {*****************************************************************************
  143. T386Instruction
  144. *****************************************************************************}
  145. procedure T386Instruction.AddReferenceSizes;
  146. { this will add the sizes for references like [esi] which do not
  147. have the size set yet, it will take only the size if the other
  148. operand is a register }
  149. var
  150. operand2,i : longint;
  151. s : pasmsymbol;
  152. so : longint;
  153. begin
  154. for i:=1to ops do
  155. begin
  156. operands[i]^.SetCorrectSize(opcode);
  157. if (operands[i]^.size=S_NO) then
  158. begin
  159. case operands[i]^.Opr.Typ of
  160. OPR_REFERENCE :
  161. begin
  162. if i=2 then
  163. operand2:=1
  164. else
  165. operand2:=2;
  166. { Only allow register as operand to take the size from }
  167. if operands[operand2]^.opr.typ=OPR_REGISTER then
  168. operands[i]^.size:=operands[operand2]^.size
  169. else
  170. begin
  171. { if no register then take the opsize (which is available with ATT) }
  172. if opsize<>S_NO then
  173. operands[i]^.size:=opsize;
  174. end;
  175. end;
  176. OPR_SYMBOL :
  177. begin
  178. { Fix lea which need a reference }
  179. if opcode=A_LEA then
  180. begin
  181. s:=operands[i]^.opr.symbol;
  182. so:=operands[i]^.opr.symofs;
  183. operands[i]^.opr.typ:=OPR_REFERENCE;
  184. reset_reference(operands[i]^.opr.ref);
  185. operands[i]^.opr.ref.symbol:=s;
  186. operands[i]^.opr.ref.offset:=so;
  187. end;
  188. operands[i]^.size:=S_L;
  189. end;
  190. end;
  191. end;
  192. end;
  193. end;
  194. procedure T386Instruction.SetInstructionOpsize;
  195. begin
  196. if opsize<>S_NO then
  197. exit;
  198. case ops of
  199. 0 : ;
  200. 1 :
  201. { "push es" must be stored as a long PM }
  202. if ((opcode=A_PUSH) or
  203. (opcode=A_POP)) and
  204. (operands[1]^.opr.typ=OPR_REGISTER) and
  205. ((operands[1]^.opr.reg>=firstsreg) and
  206. (operands[1]^.opr.reg<=lastsreg)) then
  207. opsize:=S_L
  208. else
  209. opsize:=operands[1]^.size;
  210. 2 :
  211. begin
  212. case opcode of
  213. A_MOVZX,A_MOVSX :
  214. begin
  215. case operands[1]^.size of
  216. S_W :
  217. case operands[2]^.size of
  218. S_L :
  219. opsize:=S_WL;
  220. end;
  221. S_B :
  222. case operands[2]^.size of
  223. S_W :
  224. opsize:=S_BW;
  225. S_L :
  226. opsize:=S_BL;
  227. end;
  228. end;
  229. end;
  230. A_OUT :
  231. opsize:=operands[1]^.size;
  232. else
  233. opsize:=operands[2]^.size;
  234. end;
  235. end;
  236. 3 :
  237. opsize:=operands[3]^.size;
  238. end;
  239. end;
  240. procedure T386Instruction.CheckOperandSizes;
  241. var
  242. sizeerr : boolean;
  243. i : longint;
  244. begin
  245. { Check only the most common opcodes here, the others are done in
  246. the assembler pass }
  247. case opcode of
  248. A_PUSH,A_POP,A_DEC,A_INC,A_NOT,A_NEG,
  249. A_CMP,A_MOV,
  250. A_ADD,A_SUB,A_ADC,A_SBB,
  251. A_AND,A_OR,A_TEST,A_XOR: ;
  252. else
  253. exit;
  254. end;
  255. { Handle the BW,BL,WL separatly }
  256. sizeerr:=false;
  257. { special push/pop selector case }
  258. if ((opcode=A_PUSH) or
  259. (opcode=A_POP)) and
  260. (operands[1]^.opr.typ=OPR_REGISTER) and
  261. ((operands[1]^.opr.reg>=firstsreg) and
  262. (operands[1]^.opr.reg<=lastsreg)) then
  263. exit;
  264. if opsize in [S_BW,S_BL,S_WL] then
  265. begin
  266. if ops<>2 then
  267. sizeerr:=true
  268. else
  269. begin
  270. case opsize of
  271. S_BW :
  272. sizeerr:=(operands[1]^.size<>S_B) or (operands[2]^.size<>S_W);
  273. S_BL :
  274. sizeerr:=(operands[1]^.size<>S_B) or (operands[2]^.size<>S_L);
  275. S_WL :
  276. sizeerr:=(operands[1]^.size<>S_W) or (operands[2]^.size<>S_L);
  277. end;
  278. end;
  279. end
  280. else
  281. begin
  282. for i:=1to ops do
  283. begin
  284. if (operands[i]^.opr.typ<>OPR_CONSTANT) and
  285. (operands[i]^.size in [S_B,S_W,S_L]) and
  286. (operands[i]^.size<>opsize) then
  287. sizeerr:=true;
  288. end;
  289. end;
  290. if sizeerr then
  291. begin
  292. { if range checks are on then generate an error }
  293. if (cs_compilesystem in aktmoduleswitches) or
  294. not (cs_check_range in aktlocalswitches) then
  295. Message(asmr_w_size_suffix_and_dest_dont_match)
  296. else
  297. Message(asmr_e_size_suffix_and_dest_dont_match);
  298. end;
  299. end;
  300. { This check must be done with the operand in ATT order
  301. i.e.after swapping in the intel reader
  302. but before swapping in the NASM and TASM writers PM }
  303. procedure T386Instruction.CheckNonCommutativeOpcodes;
  304. begin
  305. if ((ops=2) and
  306. (operands[1]^.opr.typ=OPR_REGISTER) and
  307. (operands[2]^.opr.typ=OPR_REGISTER) and
  308. { if the first is ST and the second is also a register
  309. it is necessarily ST1 .. ST7 }
  310. (operands[1]^.opr.reg=R_ST)) or
  311. ((ops=1) and
  312. (operands[1]^.opr.typ=OPR_REGISTER) and
  313. (operands[1]^.opr.reg in [R_ST1..R_ST7])) or
  314. (ops=0) then
  315. if opcode=A_FSUBR then
  316. opcode:=A_FSUB
  317. else if opcode=A_FSUB then
  318. opcode:=A_FSUBR
  319. else if opcode=A_FDIVR then
  320. opcode:=A_FDIV
  321. else if opcode=A_FDIV then
  322. opcode:=A_FDIVR
  323. else if opcode=A_FSUBRP then
  324. opcode:=A_FSUBP
  325. else if opcode=A_FSUBP then
  326. opcode:=A_FSUBRP
  327. else if opcode=A_FDIVRP then
  328. opcode:=A_FDIVP
  329. else if opcode=A_FDIVP then
  330. opcode:=A_FDIVRP;
  331. end;
  332. {*****************************************************************************
  333. opcode Adding
  334. *****************************************************************************}
  335. procedure T386Instruction.ConcatInstruction(p : paasmoutput);
  336. var
  337. siz : topsize;
  338. i : longint;
  339. ai : paicpu;
  340. begin
  341. { Get Opsize }
  342. if (opsize<>S_NO) or (Ops=0) then
  343. siz:=opsize
  344. else
  345. begin
  346. if (Ops=2) and (operands[1]^.opr.typ=OPR_REGISTER) then
  347. siz:=operands[1]^.size
  348. else
  349. siz:=operands[Ops]^.size;
  350. end;
  351. ai:=new(paicpu,op_none(opcode,siz));
  352. ai^.Ops:=Ops;
  353. for i:=1to Ops do
  354. begin
  355. case operands[i]^.opr.typ of
  356. OPR_CONSTANT :
  357. ai^.loadconst(i-1,operands[i]^.opr.val);
  358. OPR_REGISTER:
  359. ai^.loadreg(i-1,operands[i]^.opr.reg);
  360. OPR_SYMBOL:
  361. ai^.loadsymbol(i-1,operands[i]^.opr.symbol,operands[i]^.opr.symofs);
  362. OPR_REFERENCE:
  363. ai^.loadref(i-1,newreference(operands[i]^.opr.ref));
  364. end;
  365. end;
  366. { Condition ? }
  367. if condition<>C_None then
  368. ai^.SetCondition(condition);
  369. { Concat the opcode or give an error }
  370. if assigned(ai) then
  371. p^.concat(ai)
  372. else
  373. Message(asmr_e_invalid_opcode_and_operand);
  374. end;
  375. end.
  376. {
  377. $Log$
  378. Revision 1.17 2000-05-12 21:26:22 pierre
  379. * fix the FDIV FDIVR FSUB FSUBR and popping equivalent
  380. simply by swapping from reverse to normal and vice-versa
  381. when passing from one syntax to the other !
  382. Revision 1.16 2000/05/10 08:55:08 pierre
  383. * no warning nor error for pushl of segment register
  384. Revision 1.15 2000/05/09 21:44:28 pierre
  385. * add .byte 066h to force correct pushw %es
  386. * handle push es as a pushl %es
  387. Revision 1.14 2000/04/14 12:26:33 pierre
  388. avoid to reset operand size of opsize is S_NO
  389. Revision 1.13 2000/04/04 13:48:44 pierre
  390. + TOperand.SetCorrectSize virtual method added
  391. to be able to change the suffix according to the instruction
  392. (FIADD word ptr w need a s as ATT suffix
  393. wheras FILD word ptr w need a w suffix :( )
  394. Revision 1.12 2000/02/09 13:23:01 peter
  395. * log truncated
  396. Revision 1.11 2000/01/07 01:14:34 peter
  397. * updated copyright to 2000
  398. Revision 1.10 1999/12/12 12:59:34 peter
  399. * only check suffixsize for byte,word,long
  400. Revision 1.9 1999/08/25 12:00:05 jonas
  401. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  402. Revision 1.8 1999/08/04 00:23:23 florian
  403. * renamed i386asm and i386base to cpuasm and cpubase
  404. }