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,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<>opsize) then
  245. sizeerr:=true;
  246. end;
  247. end;
  248. if sizeerr then
  249. begin
  250. { if range checks are on then generate an error }
  251. if (cs_compilesystem in aktmoduleswitches) or
  252. not (cs_check_range in aktlocalswitches) then
  253. Message(asmr_w_size_suffix_and_dest_dont_match)
  254. else
  255. Message(asmr_e_size_suffix_and_dest_dont_match);
  256. end;
  257. end;
  258. {*****************************************************************************
  259. opcode Adding
  260. *****************************************************************************}
  261. procedure T386Instruction.ConcatInstruction(p : paasmoutput);
  262. var
  263. siz : topsize;
  264. i : longint;
  265. ai : pai386;
  266. begin
  267. { Get Opsize }
  268. if (opsize<>S_NO) or (Ops=0) then
  269. siz:=opsize
  270. else
  271. begin
  272. if (Ops=2) and (operands[1]^.opr.typ=OPR_REGISTER) then
  273. siz:=operands[1]^.size
  274. else
  275. siz:=operands[Ops]^.size;
  276. end;
  277. ai:=new(pai386,op_none(opcode,siz));
  278. ai^.Ops:=Ops;
  279. for i:=1to Ops do
  280. begin
  281. case operands[i]^.opr.typ of
  282. OPR_CONSTANT :
  283. ai^.loadconst(i-1,operands[i]^.opr.val);
  284. OPR_REGISTER:
  285. ai^.loadreg(i-1,operands[i]^.opr.reg);
  286. OPR_SYMBOL:
  287. ai^.loadsymbol(i-1,operands[i]^.opr.symbol,operands[i]^.opr.symofs);
  288. OPR_REFERENCE:
  289. ai^.loadref(i-1,newreference(operands[i]^.opr.ref));
  290. end;
  291. end;
  292. { Condition ? }
  293. if condition<>C_None then
  294. ai^.SetCondition(condition);
  295. { Concat the opcode or give an error }
  296. if assigned(ai) then
  297. p^.concat(ai)
  298. else
  299. Message(asmr_e_invalid_opcode_and_operand);
  300. end;
  301. end.
  302. {
  303. $Log$
  304. Revision 1.8 1999-08-04 00:23:23 florian
  305. * renamed i386asm and i386base to cpuasm and cpubase
  306. Revision 1.7 1999/05/27 19:44:55 peter
  307. * removed oldasm
  308. * plabel -> pasmlabel
  309. * -a switches to source writing automaticly
  310. * assembler readers OOPed
  311. * asmsymbol automaticly external
  312. * jumptables and other label fixes for asm readers
  313. Revision 1.6 1999/05/21 13:55:12 peter
  314. * NEWLAB for label as symbol
  315. Revision 1.5 1999/05/13 21:59:40 peter
  316. * removed oldppu code
  317. * warning if objpas is loaded from uses
  318. * first things for new deref writing
  319. Revision 1.4 1999/05/12 00:19:55 peter
  320. * removed R_DEFAULT_SEG
  321. * uniform float names
  322. Revision 1.3 1999/05/05 22:21:59 peter
  323. * updated messages
  324. Revision 1.2 1999/05/02 14:24:26 peter
  325. * translate opr_symbol to reference for lea
  326. Revision 1.1 1999/05/01 13:24:40 peter
  327. * merged nasm compiler
  328. * old asm moved to oldasm/
  329. Revision 1.7 1999/04/26 23:26:16 peter
  330. * redesigned record offset parsing to support nested records
  331. * normal compiler uses the redesigned createvarinstr()
  332. Revision 1.6 1999/04/14 09:07:44 peter
  333. * asm reader improvements
  334. Revision 1.5 1999/03/29 16:05:52 peter
  335. * optimizer working for ag386bin
  336. Revision 1.4 1999/03/26 00:01:16 peter
  337. * first things for optimizer (compiles but cycle crashes)
  338. Revision 1.3 1999/03/06 17:24:25 peter
  339. * rewritten intel parser a lot, especially reference reading
  340. * size checking added for asm parsers
  341. Revision 1.2 1999/03/02 02:56:29 peter
  342. + stabs support for binary writers
  343. * more fixes and missing updates from the previous commit :(
  344. Revision 1.1 1999/03/01 15:46:26 peter
  345. * ag386bin finally make cycles correct
  346. * prefixes are now also normal opcodes
  347. }