ra386.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 InitAsmRef(var instr: TInstruction;operandnum:byte);
  30. { Operand sizes }
  31. procedure AddReferenceSizes(var instr:TInstruction);
  32. procedure SetInstructionOpsize(var instr:TInstruction);
  33. procedure CheckOperandSizes(var instr:TInstruction);
  34. { opcode adding }
  35. procedure ConcatInstruction(p : paasmoutput;var instr:TInstruction);
  36. implementation
  37. uses
  38. globtype,globals,verbose,
  39. i386asm;
  40. {*****************************************************************************
  41. Parser Helpers
  42. *****************************************************************************}
  43. function is_prefix(t:tasmop):boolean;
  44. var
  45. i : longint;
  46. Begin
  47. is_prefix:=false;
  48. for i:=1 to AsmPrefixes do
  49. if t=AsmPrefix[i-1] then
  50. begin
  51. is_prefix:=true;
  52. exit;
  53. end;
  54. end;
  55. function is_override(t:tasmop):boolean;
  56. var
  57. i : longint;
  58. Begin
  59. is_override:=false;
  60. for i:=1 to AsmOverrides do
  61. if t=AsmOverride[i-1] then
  62. begin
  63. is_override:=true;
  64. exit;
  65. end;
  66. end;
  67. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  68. { Checks if the prefix is valid with the following opcode }
  69. { return false if not, otherwise true }
  70. Begin
  71. CheckPrefix := TRUE;
  72. (* Case prefix of
  73. A_REP,A_REPNE,A_REPE:
  74. Case opcode Of
  75. A_SCASB,A_SCASW,A_SCASD,
  76. A_INS,A_OUTS,A_MOVS,A_CMPS,A_LODS,A_STOS:;
  77. Else
  78. Begin
  79. CheckPrefix := FALSE;
  80. exit;
  81. end;
  82. end; { case }
  83. A_LOCK:
  84. Case opcode Of
  85. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,A_ADC,A_SBB,A_AND,A_SUB,
  86. A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  87. Else
  88. Begin
  89. CheckPrefix := FALSE;
  90. Exit;
  91. end;
  92. end; { case }
  93. A_NONE: exit; { no prefix here }
  94. else
  95. CheckPrefix := FALSE;
  96. end; { end case } *)
  97. end;
  98. Function CheckOverride(overrideop,op:tasmop): Boolean;
  99. { Check if the override is valid, and if so then }
  100. { update the instr variable accordingly. }
  101. Begin
  102. CheckOverride := true;
  103. { Case instr.getinstruction of
  104. A_MOVS,A_XLAT,A_CMPS:
  105. Begin
  106. CheckOverride := TRUE;
  107. Message(assem_e_segment_override_not_supported);
  108. end
  109. end }
  110. end;
  111. Procedure InitAsmRef(var instr: TInstruction;operandnum:byte);
  112. {*********************************************************************}
  113. { Description: This routine first check if the opcode is of }
  114. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  115. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  116. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  117. { to point to the default segment. }
  118. {*********************************************************************}
  119. Begin
  120. With instr do
  121. Begin
  122. case operands[operandnum].operandtype of
  123. OPR_REFERENCE: exit;
  124. OPR_NONE: ;
  125. else
  126. Message(assem_e_invalid_operand_type);
  127. end;
  128. operands[operandnum].operandtype := OPR_REFERENCE;
  129. operands[operandnum].ref.segment := R_DEFAULT_SEG;
  130. end;
  131. end;
  132. {*****************************************************************************
  133. Operand Sizes
  134. *****************************************************************************}
  135. procedure AddReferenceSizes(var instr:TInstruction);
  136. { this will add the sizes for references like [esi] which do not
  137. have the size set yet, it will take only the size if the other
  138. operand is a register }
  139. var
  140. operand2,i : longint;
  141. begin
  142. with instr do
  143. begin
  144. for i:=1to ops do
  145. if (operands[i].size=S_NO) then
  146. begin
  147. case operands[i].operandtype of
  148. OPR_REFERENCE :
  149. begin
  150. if i=2 then
  151. operand2:=1
  152. else
  153. operand2:=2;
  154. { Only allow register as operand to take the size from }
  155. if operands[operand2].operandtype=OPR_REGISTER then
  156. operands[i].size:=operands[operand2].size
  157. else
  158. begin
  159. { if no register then take the opsize (which is available with ATT) }
  160. operands[i].size:=opsize;
  161. end;
  162. end;
  163. OPR_SYMBOL :
  164. begin
  165. operands[i].size:=S_L;
  166. end;
  167. end;
  168. end;
  169. end;
  170. end;
  171. procedure SetInstructionOpsize(var instr:TInstruction);
  172. begin
  173. with instr do
  174. begin
  175. if opsize<>S_NO then
  176. exit;
  177. case ops of
  178. 0 : ;
  179. 1 :
  180. opsize:=operands[1].size;
  181. 2 :
  182. begin
  183. case opcode of
  184. A_MOVZX,A_MOVSX :
  185. begin
  186. case operands[1].size of
  187. S_W :
  188. case operands[2].size of
  189. S_L :
  190. opsize:=S_WL;
  191. end;
  192. S_B :
  193. case operands[2].size of
  194. S_W :
  195. opsize:=S_BW;
  196. S_L :
  197. opsize:=S_BL;
  198. end;
  199. end;
  200. end;
  201. A_IN,A_OUT :
  202. opsize:=operands[1].size;
  203. else
  204. opsize:=operands[2].size;
  205. end;
  206. end;
  207. 3 :
  208. opsize:=operands[3].size;
  209. end;
  210. end;
  211. end;
  212. procedure CheckOperandSizes(var instr:TInstruction);
  213. var
  214. sizeerr : boolean;
  215. i : longint;
  216. begin
  217. with instr do
  218. begin
  219. { don't check labeled instructions }
  220. if labeled then
  221. exit;
  222. { Check only the most common opcodes here, the others are done in
  223. the assembler pass }
  224. case opcode of
  225. A_PUSH,A_DEC,A_INC,A_NOT,A_NEG,
  226. A_CMP,A_MOV,
  227. A_ADD,A_SUB,A_ADC,A_SBB,
  228. A_AND,A_OR,A_TEST,A_XOR: ;
  229. else
  230. exit;
  231. end;
  232. { Handle the BW,BL,WL separatly }
  233. sizeerr:=false;
  234. if opsize in [S_BW,S_BL,S_WL] then
  235. begin
  236. if ops<>2 then
  237. sizeerr:=true
  238. else
  239. begin
  240. case opsize of
  241. S_BW :
  242. sizeerr:=(operands[1].size<>S_B) or (operands[2].size<>S_W);
  243. S_BL :
  244. sizeerr:=(operands[1].size<>S_B) or (operands[2].size<>S_L);
  245. S_WL :
  246. sizeerr:=(operands[1].size<>S_W) or (operands[2].size<>S_L);
  247. end;
  248. end;
  249. end
  250. else
  251. begin
  252. for i:=1to ops do
  253. begin
  254. if (operands[i].operandtype<>OPR_CONSTANT) and
  255. (operands[i].size<>opsize) then
  256. sizeerr:=true;
  257. end;
  258. end;
  259. if sizeerr then
  260. begin
  261. { if range checks are on then generate an error }
  262. if (cs_compilesystem in aktmoduleswitches) or
  263. not (cs_check_range in aktlocalswitches) then
  264. Message(assem_w_size_suffix_and_dest_dont_match)
  265. else
  266. Message(assem_e_size_suffix_and_dest_dont_match);
  267. end;
  268. end;
  269. end;
  270. {*****************************************************************************
  271. opcode Adding
  272. *****************************************************************************}
  273. procedure ConcatInstruction(p : paasmoutput;var instr:TInstruction);
  274. var
  275. siz : topsize;
  276. i : longint;
  277. hlab : plabel;
  278. ai : pai386;
  279. begin
  280. with instr do
  281. begin
  282. { Handle a labeled opcode first to see if it needs conversion }
  283. if labeled then
  284. begin
  285. { check if it's a jmp or call to a label, then issue a pai386_labeled }
  286. if (Ops=1) then
  287. begin
  288. case opcode of
  289. A_CALL,A_JMP,A_Jcc,A_JCXZ, A_JECXZ,
  290. A_LOOP, A_LOOPE, A_LOOPNE, A_LOOPNZ, A_LOOPZ :
  291. begin
  292. p^.concat(new(pai386_labeled,op_cond_lab(opcode,condition,operands[1].hl)));
  293. exit;
  294. end;
  295. end;
  296. end;
  297. { convert all labinstr to references }
  298. for i:=1to Ops do
  299. if operands[i].operandtype=OPR_LABINSTR then
  300. begin
  301. hlab:=operands[i].hl;
  302. operands[i].operandtype:=OPR_REFERENCE;
  303. reset_reference(operands[i].ref);
  304. operands[i].ref.symbol:=newasmsymbol(lab2str(hlab));
  305. end;
  306. end;
  307. { Get Opsize }
  308. if (opsize<>S_NO) or (Ops=0) then
  309. siz:=opsize
  310. else
  311. begin
  312. if (Ops=2) and (instr.operands[1].operandtype=OPR_REGISTER) then
  313. siz:=operands[1].size
  314. else
  315. siz:=operands[Ops].size;
  316. end;
  317. ai:=new(pai386,op_none(opcode,siz));
  318. ai^.Ops:=Ops;
  319. for i:=1to Ops do
  320. begin
  321. case instr.operands[i].operandtype of
  322. OPR_CONSTANT :
  323. ai^.loadconst(i-1,instr.operands[i].val);
  324. OPR_REGISTER:
  325. ai^.loadreg(i-1,instr.operands[i].reg);
  326. OPR_SYMBOL:
  327. ai^.loadsymbol(i-1,instr.operands[i].symbol,instr.operands[i].symofs);
  328. OPR_REFERENCE:
  329. ai^.loadref(i-1,newreference(instr.operands[i].ref));
  330. end;
  331. end;
  332. { Condition ? }
  333. if condition<>C_None then
  334. ai^.SetCondition(condition);
  335. { Concat the opcode or give an error }
  336. if assigned(ai) then
  337. p^.concat(ai)
  338. else
  339. Message(assem_e_invalid_opcode_and_operand);
  340. end;
  341. end;
  342. end.
  343. {
  344. $Log$
  345. Revision 1.1 1999-05-01 13:24:40 peter
  346. * merged nasm compiler
  347. * old asm moved to oldasm/
  348. Revision 1.7 1999/04/26 23:26:16 peter
  349. * redesigned record offset parsing to support nested records
  350. * normal compiler uses the redesigned createvarinstr()
  351. Revision 1.6 1999/04/14 09:07:44 peter
  352. * asm reader improvements
  353. Revision 1.5 1999/03/29 16:05:52 peter
  354. * optimizer working for ag386bin
  355. Revision 1.4 1999/03/26 00:01:16 peter
  356. * first things for optimizer (compiles but cycle crashes)
  357. Revision 1.3 1999/03/06 17:24:25 peter
  358. * rewritten intel parser a lot, especially reference reading
  359. * size checking added for asm parsers
  360. Revision 1.2 1999/03/02 02:56:29 peter
  361. + stabs support for binary writers
  362. * more fixes and missing updates from the previous commit :(
  363. Revision 1.1 1999/03/01 15:46:26 peter
  364. * ag386bin finally make cycles correct
  365. * prefixes are now also normal opcodes
  366. }