ra386.pas 9.0 KB

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