ra386.pas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. operands[i]^.size:=opsize;
  172. end;
  173. end;
  174. OPR_SYMBOL :
  175. begin
  176. { Fix lea which need a reference }
  177. if opcode=A_LEA then
  178. begin
  179. s:=operands[i]^.opr.symbol;
  180. so:=operands[i]^.opr.symofs;
  181. operands[i]^.opr.typ:=OPR_REFERENCE;
  182. reset_reference(operands[i]^.opr.ref);
  183. operands[i]^.opr.ref.symbol:=s;
  184. operands[i]^.opr.ref.offset:=so;
  185. end;
  186. operands[i]^.size:=S_L;
  187. end;
  188. end;
  189. end;
  190. end;
  191. end;
  192. procedure T386Instruction.SetInstructionOpsize;
  193. begin
  194. if opsize<>S_NO then
  195. exit;
  196. case ops of
  197. 0 : ;
  198. 1 :
  199. opsize:=operands[1]^.size;
  200. 2 :
  201. begin
  202. case opcode of
  203. A_MOVZX,A_MOVSX :
  204. begin
  205. case operands[1]^.size of
  206. S_W :
  207. case operands[2]^.size of
  208. S_L :
  209. opsize:=S_WL;
  210. end;
  211. S_B :
  212. case operands[2]^.size of
  213. S_W :
  214. opsize:=S_BW;
  215. S_L :
  216. opsize:=S_BL;
  217. end;
  218. end;
  219. end;
  220. A_OUT :
  221. opsize:=operands[1]^.size;
  222. else
  223. opsize:=operands[2]^.size;
  224. end;
  225. end;
  226. 3 :
  227. opsize:=operands[3]^.size;
  228. end;
  229. end;
  230. procedure T386Instruction.CheckOperandSizes;
  231. var
  232. sizeerr : boolean;
  233. i : longint;
  234. begin
  235. { Check only the most common opcodes here, the others are done in
  236. the assembler pass }
  237. case opcode of
  238. A_PUSH,A_DEC,A_INC,A_NOT,A_NEG,
  239. A_CMP,A_MOV,
  240. A_ADD,A_SUB,A_ADC,A_SBB,
  241. A_AND,A_OR,A_TEST,A_XOR: ;
  242. else
  243. exit;
  244. end;
  245. { Handle the BW,BL,WL separatly }
  246. sizeerr:=false;
  247. if opsize in [S_BW,S_BL,S_WL] then
  248. begin
  249. if ops<>2 then
  250. sizeerr:=true
  251. else
  252. begin
  253. case opsize of
  254. S_BW :
  255. sizeerr:=(operands[1]^.size<>S_B) or (operands[2]^.size<>S_W);
  256. S_BL :
  257. sizeerr:=(operands[1]^.size<>S_B) or (operands[2]^.size<>S_L);
  258. S_WL :
  259. sizeerr:=(operands[1]^.size<>S_W) or (operands[2]^.size<>S_L);
  260. end;
  261. end;
  262. end
  263. else
  264. begin
  265. for i:=1to ops do
  266. begin
  267. if (operands[i]^.opr.typ<>OPR_CONSTANT) and
  268. (operands[i]^.size in [S_B,S_W,S_L]) and
  269. (operands[i]^.size<>opsize) then
  270. sizeerr:=true;
  271. end;
  272. end;
  273. if sizeerr then
  274. begin
  275. { if range checks are on then generate an error }
  276. if (cs_compilesystem in aktmoduleswitches) or
  277. not (cs_check_range in aktlocalswitches) then
  278. Message(asmr_w_size_suffix_and_dest_dont_match)
  279. else
  280. Message(asmr_e_size_suffix_and_dest_dont_match);
  281. end;
  282. end;
  283. {*****************************************************************************
  284. opcode Adding
  285. *****************************************************************************}
  286. procedure T386Instruction.ConcatInstruction(p : paasmoutput);
  287. var
  288. siz : topsize;
  289. i : longint;
  290. ai : paicpu;
  291. begin
  292. { Get Opsize }
  293. if (opsize<>S_NO) or (Ops=0) then
  294. siz:=opsize
  295. else
  296. begin
  297. if (Ops=2) and (operands[1]^.opr.typ=OPR_REGISTER) then
  298. siz:=operands[1]^.size
  299. else
  300. siz:=operands[Ops]^.size;
  301. end;
  302. ai:=new(paicpu,op_none(opcode,siz));
  303. ai^.Ops:=Ops;
  304. for i:=1to Ops do
  305. begin
  306. case operands[i]^.opr.typ of
  307. OPR_CONSTANT :
  308. ai^.loadconst(i-1,operands[i]^.opr.val);
  309. OPR_REGISTER:
  310. ai^.loadreg(i-1,operands[i]^.opr.reg);
  311. OPR_SYMBOL:
  312. ai^.loadsymbol(i-1,operands[i]^.opr.symbol,operands[i]^.opr.symofs);
  313. OPR_REFERENCE:
  314. ai^.loadref(i-1,newreference(operands[i]^.opr.ref));
  315. end;
  316. end;
  317. { Condition ? }
  318. if condition<>C_None then
  319. ai^.SetCondition(condition);
  320. { Concat the opcode or give an error }
  321. if assigned(ai) then
  322. p^.concat(ai)
  323. else
  324. Message(asmr_e_invalid_opcode_and_operand);
  325. end;
  326. end.
  327. {
  328. $Log$
  329. Revision 1.13 2000-04-04 13:48:44 pierre
  330. + TOperand.SetCorrectSize virtual method added
  331. to be able to change the suffix according to the instruction
  332. (FIADD word ptr w need a s as ATT suffix
  333. wheras FILD word ptr w need a w suffix :( )
  334. Revision 1.12 2000/02/09 13:23:01 peter
  335. * log truncated
  336. Revision 1.11 2000/01/07 01:14:34 peter
  337. * updated copyright to 2000
  338. Revision 1.10 1999/12/12 12:59:34 peter
  339. * only check suffixsize for byte,word,long
  340. Revision 1.9 1999/08/25 12:00:05 jonas
  341. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  342. Revision 1.8 1999/08/04 00:23:23 florian
  343. * renamed i386asm and i386base to cpuasm and cpubase
  344. }