ra386.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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(asmr_e_invalid_operand_type);
  127. end;
  128. operands[operandnum].operandtype := OPR_REFERENCE;
  129. operands[operandnum].ref.segment := R_NO;
  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. s : pasmsymbol;
  142. so : longint;
  143. begin
  144. with instr do
  145. begin
  146. for i:=1to ops do
  147. if (operands[i].size=S_NO) then
  148. begin
  149. case operands[i].operandtype of
  150. OPR_REFERENCE :
  151. begin
  152. if i=2 then
  153. operand2:=1
  154. else
  155. operand2:=2;
  156. { Only allow register as operand to take the size from }
  157. if operands[operand2].operandtype=OPR_REGISTER then
  158. operands[i].size:=operands[operand2].size
  159. else
  160. begin
  161. { if no register then take the opsize (which is available with ATT) }
  162. operands[i].size:=opsize;
  163. end;
  164. end;
  165. OPR_SYMBOL :
  166. begin
  167. { Fix lea which need a reference }
  168. if opcode=A_LEA then
  169. begin
  170. s:=operands[i].symbol;
  171. so:=operands[i].symofs;
  172. operands[i].operandtype:=OPR_REFERENCE;
  173. reset_reference(operands[i].ref);
  174. operands[i].ref.symbol:=s;
  175. operands[i].ref.offset:=so;
  176. end;
  177. operands[i].size:=S_L;
  178. end;
  179. end;
  180. end;
  181. end;
  182. end;
  183. procedure SetInstructionOpsize(var instr:TInstruction);
  184. begin
  185. with instr do
  186. begin
  187. if opsize<>S_NO then
  188. exit;
  189. case ops of
  190. 0 : ;
  191. 1 :
  192. opsize:=operands[1].size;
  193. 2 :
  194. begin
  195. case opcode of
  196. A_MOVZX,A_MOVSX :
  197. begin
  198. case operands[1].size of
  199. S_W :
  200. case operands[2].size of
  201. S_L :
  202. opsize:=S_WL;
  203. end;
  204. S_B :
  205. case operands[2].size of
  206. S_W :
  207. opsize:=S_BW;
  208. S_L :
  209. opsize:=S_BL;
  210. end;
  211. end;
  212. end;
  213. A_OUT :
  214. opsize:=operands[1].size;
  215. else
  216. opsize:=operands[2].size;
  217. end;
  218. end;
  219. 3 :
  220. opsize:=operands[3].size;
  221. end;
  222. end;
  223. end;
  224. procedure CheckOperandSizes(var instr:TInstruction);
  225. var
  226. sizeerr : boolean;
  227. i : longint;
  228. begin
  229. with instr do
  230. begin
  231. { don't check labeled instructions }
  232. if labeled then
  233. exit;
  234. { Check only the most common opcodes here, the others are done in
  235. the assembler pass }
  236. case opcode of
  237. A_PUSH,A_DEC,A_INC,A_NOT,A_NEG,
  238. A_CMP,A_MOV,
  239. A_ADD,A_SUB,A_ADC,A_SBB,
  240. A_AND,A_OR,A_TEST,A_XOR: ;
  241. else
  242. exit;
  243. end;
  244. { Handle the BW,BL,WL separatly }
  245. sizeerr:=false;
  246. if opsize in [S_BW,S_BL,S_WL] then
  247. begin
  248. if ops<>2 then
  249. sizeerr:=true
  250. else
  251. begin
  252. case opsize of
  253. S_BW :
  254. sizeerr:=(operands[1].size<>S_B) or (operands[2].size<>S_W);
  255. S_BL :
  256. sizeerr:=(operands[1].size<>S_B) or (operands[2].size<>S_L);
  257. S_WL :
  258. sizeerr:=(operands[1].size<>S_W) or (operands[2].size<>S_L);
  259. end;
  260. end;
  261. end
  262. else
  263. begin
  264. for i:=1to ops do
  265. begin
  266. if (operands[i].operandtype<>OPR_CONSTANT) and
  267. (operands[i].size<>opsize) then
  268. sizeerr:=true;
  269. end;
  270. end;
  271. if sizeerr then
  272. begin
  273. { if range checks are on then generate an error }
  274. if (cs_compilesystem in aktmoduleswitches) or
  275. not (cs_check_range in aktlocalswitches) then
  276. Message(asmr_w_size_suffix_and_dest_dont_match)
  277. else
  278. Message(asmr_e_size_suffix_and_dest_dont_match);
  279. end;
  280. end;
  281. end;
  282. {*****************************************************************************
  283. opcode Adding
  284. *****************************************************************************}
  285. procedure ConcatInstruction(p : paasmoutput;var instr:TInstruction);
  286. var
  287. siz : topsize;
  288. i : longint;
  289. hlab : plabel;
  290. ai : pai386;
  291. begin
  292. with instr do
  293. begin
  294. { Handle a labeled opcode first to see if it needs conversion }
  295. if labeled then
  296. begin
  297. { check if it's a jmp or call to a label, then issue a pai386_labeled }
  298. if (Ops=1) then
  299. begin
  300. case opcode of
  301. A_CALL,A_JMP,A_Jcc,A_JCXZ, A_JECXZ,
  302. A_LOOP, A_LOOPE, A_LOOPNE, A_LOOPNZ, A_LOOPZ :
  303. begin
  304. p^.concat(new(pai386_labeled,op_cond_lab(opcode,condition,operands[1].hl)));
  305. exit;
  306. end;
  307. end;
  308. end;
  309. { convert all labinstr to references }
  310. for i:=1to Ops do
  311. if operands[i].operandtype=OPR_LABINSTR then
  312. begin
  313. hlab:=operands[i].hl;
  314. operands[i].operandtype:=OPR_REFERENCE;
  315. reset_reference(operands[i].ref);
  316. operands[i].ref.symbol:=newasmsymbol(lab2str(hlab));
  317. end;
  318. end;
  319. { Get Opsize }
  320. if (opsize<>S_NO) or (Ops=0) then
  321. siz:=opsize
  322. else
  323. begin
  324. if (Ops=2) and (instr.operands[1].operandtype=OPR_REGISTER) then
  325. siz:=operands[1].size
  326. else
  327. siz:=operands[Ops].size;
  328. end;
  329. ai:=new(pai386,op_none(opcode,siz));
  330. ai^.Ops:=Ops;
  331. for i:=1to Ops do
  332. begin
  333. case instr.operands[i].operandtype of
  334. OPR_CONSTANT :
  335. ai^.loadconst(i-1,instr.operands[i].val);
  336. OPR_REGISTER:
  337. ai^.loadreg(i-1,instr.operands[i].reg);
  338. OPR_SYMBOL:
  339. ai^.loadsymbol(i-1,instr.operands[i].symbol,instr.operands[i].symofs);
  340. OPR_REFERENCE:
  341. ai^.loadref(i-1,newreference(instr.operands[i].ref));
  342. end;
  343. end;
  344. { Condition ? }
  345. if condition<>C_None then
  346. ai^.SetCondition(condition);
  347. { Concat the opcode or give an error }
  348. if assigned(ai) then
  349. p^.concat(ai)
  350. else
  351. Message(asmr_e_invalid_opcode_and_operand);
  352. end;
  353. end;
  354. end.
  355. {
  356. $Log$
  357. Revision 1.5 1999-05-13 21:59:40 peter
  358. * removed oldppu code
  359. * warning if objpas is loaded from uses
  360. * first things for new deref writing
  361. Revision 1.4 1999/05/12 00:19:55 peter
  362. * removed R_DEFAULT_SEG
  363. * uniform float names
  364. Revision 1.3 1999/05/05 22:21:59 peter
  365. * updated messages
  366. Revision 1.2 1999/05/02 14:24:26 peter
  367. * translate opr_symbol to reference for lea
  368. Revision 1.1 1999/05/01 13:24:40 peter
  369. * merged nasm compiler
  370. * old asm moved to oldasm/
  371. Revision 1.7 1999/04/26 23:26:16 peter
  372. * redesigned record offset parsing to support nested records
  373. * normal compiler uses the redesigned createvarinstr()
  374. Revision 1.6 1999/04/14 09:07:44 peter
  375. * asm reader improvements
  376. Revision 1.5 1999/03/29 16:05:52 peter
  377. * optimizer working for ag386bin
  378. Revision 1.4 1999/03/26 00:01:16 peter
  379. * first things for optimizer (compiles but cycle crashes)
  380. Revision 1.3 1999/03/06 17:24:25 peter
  381. * rewritten intel parser a lot, especially reference reading
  382. * size checking added for asm parsers
  383. Revision 1.2 1999/03/02 02:56:29 peter
  384. + stabs support for binary writers
  385. * more fixes and missing updates from the previous commit :(
  386. Revision 1.1 1999/03/01 15:46:26 peter
  387. * ag386bin finally make cycles correct
  388. * prefixes are now also normal opcodes
  389. }