ra386.pas 10 KB

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