ra386.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. {
  2. $Id$
  3. Copyright (c) 1997-98 by Carl Eric Codere
  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,
  22. i386base,
  23. RAUtils;
  24. { Parser helpers }
  25. function is_prefix(t:tasmop):boolean;
  26. function is_override(t:tasmop):boolean;
  27. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  28. Function CheckOverride(overrideop,op:tasmop): Boolean;
  29. Procedure FWaitWarning;
  30. type
  31. P386Operand=^T386Operand;
  32. T386Operand=object(TOperand)
  33. end;
  34. P386Instruction=^T386Instruction;
  35. T386Instruction=object(TInstruction)
  36. { Operand sizes }
  37. procedure AddReferenceSizes;
  38. procedure SetInstructionOpsize;
  39. procedure CheckOperandSizes;
  40. { opcode adding }
  41. procedure ConcatInstruction(p : paasmoutput);virtual;
  42. end;
  43. implementation
  44. uses
  45. globtype,systems,globals,verbose,
  46. i386asm;
  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. T386Instruction
  125. *****************************************************************************}
  126. procedure T386Instruction.AddReferenceSizes;
  127. { this will add the sizes for references like [esi] which do not
  128. have the size set yet, it will take only the size if the other
  129. operand is a register }
  130. var
  131. operand2,i : longint;
  132. s : pasmsymbol;
  133. so : longint;
  134. begin
  135. for i:=1to ops do
  136. if (operands[i]^.size=S_NO) then
  137. begin
  138. case operands[i]^.Opr.Typ of
  139. OPR_REFERENCE :
  140. begin
  141. if i=2 then
  142. operand2:=1
  143. else
  144. operand2:=2;
  145. { Only allow register as operand to take the size from }
  146. if operands[operand2]^.opr.typ=OPR_REGISTER then
  147. operands[i]^.size:=operands[operand2]^.size
  148. else
  149. begin
  150. { if no register then take the opsize (which is available with ATT) }
  151. operands[i]^.size:=opsize;
  152. end;
  153. end;
  154. OPR_SYMBOL :
  155. begin
  156. { Fix lea which need a reference }
  157. if opcode=A_LEA then
  158. begin
  159. s:=operands[i]^.opr.symbol;
  160. so:=operands[i]^.opr.symofs;
  161. operands[i]^.opr.typ:=OPR_REFERENCE;
  162. reset_reference(operands[i]^.opr.ref);
  163. operands[i]^.opr.ref.symbol:=s;
  164. operands[i]^.opr.ref.offset:=so;
  165. end;
  166. operands[i]^.size:=S_L;
  167. end;
  168. end;
  169. end;
  170. end;
  171. procedure T386Instruction.SetInstructionOpsize;
  172. begin
  173. if opsize<>S_NO then
  174. exit;
  175. case ops of
  176. 0 : ;
  177. 1 :
  178. opsize:=operands[1]^.size;
  179. 2 :
  180. begin
  181. case opcode of
  182. A_MOVZX,A_MOVSX :
  183. begin
  184. case operands[1]^.size of
  185. S_W :
  186. case operands[2]^.size of
  187. S_L :
  188. opsize:=S_WL;
  189. end;
  190. S_B :
  191. case operands[2]^.size of
  192. S_W :
  193. opsize:=S_BW;
  194. S_L :
  195. opsize:=S_BL;
  196. end;
  197. end;
  198. end;
  199. A_OUT :
  200. opsize:=operands[1]^.size;
  201. else
  202. opsize:=operands[2]^.size;
  203. end;
  204. end;
  205. 3 :
  206. opsize:=operands[3]^.size;
  207. end;
  208. end;
  209. procedure T386Instruction.CheckOperandSizes;
  210. var
  211. sizeerr : boolean;
  212. i : longint;
  213. begin
  214. { Check only the most common opcodes here, the others are done in
  215. the assembler pass }
  216. case opcode of
  217. A_PUSH,A_DEC,A_INC,A_NOT,A_NEG,
  218. A_CMP,A_MOV,
  219. A_ADD,A_SUB,A_ADC,A_SBB,
  220. A_AND,A_OR,A_TEST,A_XOR: ;
  221. else
  222. exit;
  223. end;
  224. { Handle the BW,BL,WL separatly }
  225. sizeerr:=false;
  226. if opsize in [S_BW,S_BL,S_WL] then
  227. begin
  228. if ops<>2 then
  229. sizeerr:=true
  230. else
  231. begin
  232. case opsize of
  233. S_BW :
  234. sizeerr:=(operands[1]^.size<>S_B) or (operands[2]^.size<>S_W);
  235. S_BL :
  236. sizeerr:=(operands[1]^.size<>S_B) or (operands[2]^.size<>S_L);
  237. S_WL :
  238. sizeerr:=(operands[1]^.size<>S_W) or (operands[2]^.size<>S_L);
  239. end;
  240. end;
  241. end
  242. else
  243. begin
  244. for i:=1to ops do
  245. begin
  246. if (operands[i]^.opr.typ<>OPR_CONSTANT) and
  247. (operands[i]^.size<>opsize) then
  248. sizeerr:=true;
  249. end;
  250. end;
  251. if sizeerr then
  252. begin
  253. { if range checks are on then generate an error }
  254. if (cs_compilesystem in aktmoduleswitches) or
  255. not (cs_check_range in aktlocalswitches) then
  256. Message(asmr_w_size_suffix_and_dest_dont_match)
  257. else
  258. Message(asmr_e_size_suffix_and_dest_dont_match);
  259. end;
  260. end;
  261. {*****************************************************************************
  262. opcode Adding
  263. *****************************************************************************}
  264. procedure T386Instruction.ConcatInstruction(p : paasmoutput);
  265. var
  266. siz : topsize;
  267. i : longint;
  268. ai : pai386;
  269. begin
  270. { Get Opsize }
  271. if (opsize<>S_NO) or (Ops=0) then
  272. siz:=opsize
  273. else
  274. begin
  275. if (Ops=2) and (operands[1]^.opr.typ=OPR_REGISTER) then
  276. siz:=operands[1]^.size
  277. else
  278. siz:=operands[Ops]^.size;
  279. end;
  280. ai:=new(pai386,op_none(opcode,siz));
  281. ai^.Ops:=Ops;
  282. for i:=1to Ops do
  283. begin
  284. case operands[i]^.opr.typ of
  285. OPR_CONSTANT :
  286. ai^.loadconst(i-1,operands[i]^.opr.val);
  287. OPR_REGISTER:
  288. ai^.loadreg(i-1,operands[i]^.opr.reg);
  289. OPR_SYMBOL:
  290. ai^.loadsymbol(i-1,operands[i]^.opr.symbol,operands[i]^.opr.symofs);
  291. OPR_REFERENCE:
  292. ai^.loadref(i-1,newreference(operands[i]^.opr.ref));
  293. end;
  294. end;
  295. { Condition ? }
  296. if condition<>C_None then
  297. ai^.SetCondition(condition);
  298. { Concat the opcode or give an error }
  299. if assigned(ai) then
  300. p^.concat(ai)
  301. else
  302. Message(asmr_e_invalid_opcode_and_operand);
  303. end;
  304. end.
  305. {
  306. $Log$
  307. Revision 1.7 1999-05-27 19:44:55 peter
  308. * removed oldasm
  309. * plabel -> pasmlabel
  310. * -a switches to source writing automaticly
  311. * assembler readers OOPed
  312. * asmsymbol automaticly external
  313. * jumptables and other label fixes for asm readers
  314. Revision 1.6 1999/05/21 13:55:12 peter
  315. * NEWLAB for label as symbol
  316. Revision 1.5 1999/05/13 21:59:40 peter
  317. * removed oldppu code
  318. * warning if objpas is loaded from uses
  319. * first things for new deref writing
  320. Revision 1.4 1999/05/12 00:19:55 peter
  321. * removed R_DEFAULT_SEG
  322. * uniform float names
  323. Revision 1.3 1999/05/05 22:21:59 peter
  324. * updated messages
  325. Revision 1.2 1999/05/02 14:24:26 peter
  326. * translate opr_symbol to reference for lea
  327. Revision 1.1 1999/05/01 13:24:40 peter
  328. * merged nasm compiler
  329. * old asm moved to oldasm/
  330. Revision 1.7 1999/04/26 23:26:16 peter
  331. * redesigned record offset parsing to support nested records
  332. * normal compiler uses the redesigned createvarinstr()
  333. Revision 1.6 1999/04/14 09:07:44 peter
  334. * asm reader improvements
  335. Revision 1.5 1999/03/29 16:05:52 peter
  336. * optimizer working for ag386bin
  337. Revision 1.4 1999/03/26 00:01:16 peter
  338. * first things for optimizer (compiles but cycle crashes)
  339. Revision 1.3 1999/03/06 17:24:25 peter
  340. * rewritten intel parser a lot, especially reference reading
  341. * size checking added for asm parsers
  342. Revision 1.2 1999/03/02 02:56:29 peter
  343. + stabs support for binary writers
  344. * more fixes and missing updates from the previous commit :(
  345. Revision 1.1 1999/03/01 15:46:26 peter
  346. * ag386bin finally make cycles correct
  347. * prefixes are now also normal opcodes
  348. }