ra386.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. procedure CheckNonCommutativeOpcodes;
  40. { opcode adding }
  41. procedure ConcatInstruction(p : paasmoutput);virtual;
  42. end;
  43. implementation
  44. uses
  45. globtype,systems,globals,verbose,cpuasm;
  46. {*****************************************************************************
  47. Parser Helpers
  48. *****************************************************************************}
  49. function is_prefix(t:tasmop):boolean;
  50. var
  51. i : longint;
  52. Begin
  53. is_prefix:=false;
  54. for i:=1 to AsmPrefixes do
  55. if t=AsmPrefix[i-1] then
  56. begin
  57. is_prefix:=true;
  58. exit;
  59. end;
  60. end;
  61. function is_override(t:tasmop):boolean;
  62. var
  63. i : longint;
  64. Begin
  65. is_override:=false;
  66. for i:=1 to AsmOverrides do
  67. if t=AsmOverride[i-1] then
  68. begin
  69. is_override:=true;
  70. exit;
  71. end;
  72. end;
  73. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  74. { Checks if the prefix is valid with the following opcode }
  75. { return false if not, otherwise true }
  76. Begin
  77. CheckPrefix := TRUE;
  78. (* Case prefix of
  79. A_REP,A_REPNE,A_REPE:
  80. Case opcode Of
  81. A_SCASB,A_SCASW,A_SCASD,
  82. A_INS,A_OUTS,A_MOVS,A_CMPS,A_LODS,A_STOS:;
  83. Else
  84. Begin
  85. CheckPrefix := FALSE;
  86. exit;
  87. end;
  88. end; { case }
  89. A_LOCK:
  90. Case opcode Of
  91. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,A_ADC,A_SBB,A_AND,A_SUB,
  92. A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  93. Else
  94. Begin
  95. CheckPrefix := FALSE;
  96. Exit;
  97. end;
  98. end; { case }
  99. A_NONE: exit; { no prefix here }
  100. else
  101. CheckPrefix := FALSE;
  102. end; { end case } *)
  103. end;
  104. Function CheckOverride(overrideop,op:tasmop): Boolean;
  105. { Check if the override is valid, and if so then }
  106. { update the instr variable accordingly. }
  107. Begin
  108. CheckOverride := true;
  109. { Case instr.getinstruction of
  110. A_MOVS,A_XLAT,A_CMPS:
  111. Begin
  112. CheckOverride := TRUE;
  113. Message(assem_e_segment_override_not_supported);
  114. end
  115. end }
  116. end;
  117. Procedure FWaitWarning;
  118. begin
  119. if (target_info.target=target_i386_GO32V2) and (cs_fp_emulation in aktmoduleswitches) then
  120. Message(asmr_w_fwait_emu_prob);
  121. end;
  122. {*****************************************************************************
  123. T386Operand
  124. *****************************************************************************}
  125. Procedure T386Operand.SetCorrectSize(opcode:tasmop);
  126. begin
  127. if att_needsuffix[opcode]=attsufFPU then
  128. begin
  129. case size of
  130. S_L : size:=S_FS;
  131. S_IQ : size:=S_FL;
  132. end;
  133. end
  134. else if att_needsuffix[opcode]=attsufFPUint then
  135. begin
  136. case size of
  137. S_W : size:=S_IS;
  138. S_L : size:=S_IL;
  139. end;
  140. end;
  141. end;
  142. {*****************************************************************************
  143. T386Instruction
  144. *****************************************************************************}
  145. procedure T386Instruction.AddReferenceSizes;
  146. { this will add the sizes for references like [esi] which do not
  147. have the size set yet, it will take only the size if the other
  148. operand is a register }
  149. var
  150. operand2,i : longint;
  151. s : pasmsymbol;
  152. so : longint;
  153. begin
  154. for i:=1to ops do
  155. begin
  156. operands[i]^.SetCorrectSize(opcode);
  157. if (operands[i]^.size=S_NO) then
  158. begin
  159. case operands[i]^.Opr.Typ of
  160. OPR_REFERENCE :
  161. begin
  162. if i=2 then
  163. operand2:=1
  164. else
  165. operand2:=2;
  166. if operand2<ops then
  167. begin
  168. { Only allow register as operand to take the size from }
  169. if operands[operand2]^.opr.typ=OPR_REGISTER then
  170. operands[i]^.size:=operands[operand2]^.size
  171. else
  172. begin
  173. { if no register then take the opsize (which is available with ATT),
  174. if not availble then give an error }
  175. if opsize<>S_NO then
  176. operands[i]^.size:=opsize
  177. else
  178. begin
  179. Comment(V_Error,'No size specified and unable to determine the size of the operands');
  180. { recovery }
  181. operands[i]^.size:=S_L;
  182. end;
  183. end;
  184. end
  185. else
  186. begin
  187. if opsize<>S_NO then
  188. operands[i]^.size:=opsize
  189. end;
  190. end;
  191. OPR_SYMBOL :
  192. begin
  193. { Fix lea which need a reference }
  194. if opcode=A_LEA then
  195. begin
  196. s:=operands[i]^.opr.symbol;
  197. so:=operands[i]^.opr.symofs;
  198. operands[i]^.opr.typ:=OPR_REFERENCE;
  199. reset_reference(operands[i]^.opr.ref);
  200. operands[i]^.opr.ref.symbol:=s;
  201. operands[i]^.opr.ref.offset:=so;
  202. end;
  203. operands[i]^.size:=S_L;
  204. end;
  205. end;
  206. end;
  207. end;
  208. end;
  209. procedure T386Instruction.SetInstructionOpsize;
  210. begin
  211. if opsize<>S_NO then
  212. exit;
  213. case ops of
  214. 0 : ;
  215. 1 :
  216. { "push es" must be stored as a long PM }
  217. if ((opcode=A_PUSH) or
  218. (opcode=A_POP)) and
  219. (operands[1]^.opr.typ=OPR_REGISTER) and
  220. ((operands[1]^.opr.reg>=firstsreg) and
  221. (operands[1]^.opr.reg<=lastsreg)) then
  222. opsize:=S_L
  223. else
  224. opsize:=operands[1]^.size;
  225. 2 :
  226. begin
  227. case opcode of
  228. A_MOVZX,A_MOVSX :
  229. begin
  230. case operands[1]^.size of
  231. S_W :
  232. case operands[2]^.size of
  233. S_L :
  234. opsize:=S_WL;
  235. end;
  236. S_B :
  237. case operands[2]^.size of
  238. S_W :
  239. opsize:=S_BW;
  240. S_L :
  241. opsize:=S_BL;
  242. end;
  243. end;
  244. end;
  245. A_OUT :
  246. opsize:=operands[1]^.size;
  247. else
  248. opsize:=operands[2]^.size;
  249. end;
  250. end;
  251. 3 :
  252. opsize:=operands[3]^.size;
  253. end;
  254. end;
  255. procedure T386Instruction.CheckOperandSizes;
  256. var
  257. sizeerr : boolean;
  258. i : longint;
  259. begin
  260. { Check only the most common opcodes here, the others are done in
  261. the assembler pass }
  262. case opcode of
  263. A_PUSH,A_POP,A_DEC,A_INC,A_NOT,A_NEG,
  264. A_CMP,A_MOV,
  265. A_ADD,A_SUB,A_ADC,A_SBB,
  266. A_AND,A_OR,A_TEST,A_XOR: ;
  267. else
  268. exit;
  269. end;
  270. { Handle the BW,BL,WL separatly }
  271. sizeerr:=false;
  272. { special push/pop selector case }
  273. if ((opcode=A_PUSH) or
  274. (opcode=A_POP)) and
  275. (operands[1]^.opr.typ=OPR_REGISTER) and
  276. ((operands[1]^.opr.reg>=firstsreg) and
  277. (operands[1]^.opr.reg<=lastsreg)) then
  278. exit;
  279. if opsize in [S_BW,S_BL,S_WL] then
  280. begin
  281. if ops<>2 then
  282. sizeerr:=true
  283. else
  284. begin
  285. case opsize of
  286. S_BW :
  287. sizeerr:=(operands[1]^.size<>S_B) or (operands[2]^.size<>S_W);
  288. S_BL :
  289. sizeerr:=(operands[1]^.size<>S_B) or (operands[2]^.size<>S_L);
  290. S_WL :
  291. sizeerr:=(operands[1]^.size<>S_W) or (operands[2]^.size<>S_L);
  292. end;
  293. end;
  294. end
  295. else
  296. begin
  297. for i:=1to ops do
  298. begin
  299. if (operands[i]^.opr.typ<>OPR_CONSTANT) and
  300. (operands[i]^.size in [S_B,S_W,S_L]) and
  301. (operands[i]^.size<>opsize) then
  302. sizeerr:=true;
  303. end;
  304. end;
  305. if sizeerr then
  306. begin
  307. { if range checks are on then generate an error }
  308. if (cs_compilesystem in aktmoduleswitches) or
  309. not (cs_check_range in aktlocalswitches) then
  310. Message(asmr_w_size_suffix_and_dest_dont_match)
  311. else
  312. Message(asmr_e_size_suffix_and_dest_dont_match);
  313. end;
  314. end;
  315. { This check must be done with the operand in ATT order
  316. i.e.after swapping in the intel reader
  317. but before swapping in the NASM and TASM writers PM }
  318. procedure T386Instruction.CheckNonCommutativeOpcodes;
  319. begin
  320. if ((ops=2) and
  321. (operands[1]^.opr.typ=OPR_REGISTER) and
  322. (operands[2]^.opr.typ=OPR_REGISTER) and
  323. { if the first is ST and the second is also a register
  324. it is necessarily ST1 .. ST7 }
  325. (operands[1]^.opr.reg=R_ST)) or
  326. ((ops=1) and
  327. (operands[1]^.opr.typ=OPR_REGISTER) and
  328. (operands[1]^.opr.reg in [R_ST1..R_ST7])) or
  329. (ops=0) then
  330. if opcode=A_FSUBR then
  331. opcode:=A_FSUB
  332. else if opcode=A_FSUB then
  333. opcode:=A_FSUBR
  334. else if opcode=A_FDIVR then
  335. opcode:=A_FDIV
  336. else if opcode=A_FDIV then
  337. opcode:=A_FDIVR
  338. else if opcode=A_FSUBRP then
  339. opcode:=A_FSUBP
  340. else if opcode=A_FSUBP then
  341. opcode:=A_FSUBRP
  342. else if opcode=A_FDIVRP then
  343. opcode:=A_FDIVP
  344. else if opcode=A_FDIVP then
  345. opcode:=A_FDIVRP;
  346. end;
  347. {*****************************************************************************
  348. opcode Adding
  349. *****************************************************************************}
  350. procedure T386Instruction.ConcatInstruction(p : paasmoutput);
  351. var
  352. siz : topsize;
  353. i : longint;
  354. ai : paicpu;
  355. begin
  356. { Get Opsize }
  357. if (opsize<>S_NO) or (Ops=0) then
  358. siz:=opsize
  359. else
  360. begin
  361. if (Ops=2) and (operands[1]^.opr.typ=OPR_REGISTER) then
  362. siz:=operands[1]^.size
  363. else
  364. siz:=operands[Ops]^.size;
  365. end;
  366. { NASM does not support FADD without args
  367. as alias of FADDP
  368. and GNU AS interprets FADD without operand differently
  369. for version 2.9.1 and 2.9.5 !! }
  370. if (opcode=A_FADD) and (ops=0) then
  371. begin
  372. opcode:=A_FADDP;
  373. message(asmr_w_fadd_to_faddp);
  374. end;
  375. { I tried to convince Linus Torwald to add
  376. code to support ENTER instruction
  377. (when raising a stack page fault)
  378. but he replied that ENTER is a bad instruction and
  379. Linux does not need to support it
  380. So I think its at least a good idea to add a warning
  381. if someone uses this in assembler code
  382. FPC itself does not use it at all PM }
  383. if (opcode=A_ENTER) and (target_info.target=target_i386_linux) then
  384. begin
  385. message(asmr_w_enter_not_supported_by_linux);
  386. end;
  387. ai:=new(paicpu,op_none(opcode,siz));
  388. ai^.Ops:=Ops;
  389. for i:=1to Ops do
  390. begin
  391. case operands[i]^.opr.typ of
  392. OPR_CONSTANT :
  393. ai^.loadconst(i-1,operands[i]^.opr.val);
  394. OPR_REGISTER:
  395. ai^.loadreg(i-1,operands[i]^.opr.reg);
  396. OPR_SYMBOL:
  397. ai^.loadsymbol(i-1,operands[i]^.opr.symbol,operands[i]^.opr.symofs);
  398. OPR_REFERENCE:
  399. ai^.loadref(i-1,newreference(operands[i]^.opr.ref));
  400. end;
  401. end;
  402. { Condition ? }
  403. if condition<>C_None then
  404. ai^.SetCondition(condition);
  405. { Concat the opcode or give an error }
  406. if assigned(ai) then
  407. p^.concat(ai)
  408. else
  409. Message(asmr_e_invalid_opcode_and_operand);
  410. end;
  411. end.
  412. {
  413. $Log$
  414. Revision 1.3 2000-09-03 11:44:00 peter
  415. * error for not specified operand size, which is now required for
  416. newer binutils (merged)
  417. * previous commit fix for tcflw (merged)
  418. Revision 1.2 2000/07/13 11:32:47 michael
  419. + removed logs
  420. }