ra68k.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. {
  2. Copyright (c) 1998-2003 by Carl Eric Codere and Peter Vreman
  3. Handles the common 68k assembler reader routines
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ra68k;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. aasmbase,aasmtai,aasmdata,aasmcpu,
  22. cpubase,rautils,cclasses;
  23. type
  24. Tm68kOperand=class(TOperand)
  25. end;
  26. Tm68kInstruction=class(TInstruction)
  27. opsize : topsize;
  28. function ConcatInstruction(p : TAsmList):tai;override;
  29. function ConcatLabeledInstr(p : TAsmList):tai;
  30. end;
  31. implementation
  32. uses
  33. verbose,cgbase;
  34. {*****************************************************************************
  35. TM68kInstruction
  36. *****************************************************************************}
  37. function TM68kInstruction.ConcatInstruction(p : TAsmList):tai;
  38. begin
  39. result:=inherited ConcatInstruction(p);
  40. if assigned(result) then
  41. taicpu(result).opsize:=opsize;
  42. end;
  43. (*
  44. function TM68kInstruction.ConcatInstruction(p : TAsmList):tai;
  45. var
  46. fits : boolean;
  47. begin
  48. result:=nil;
  49. fits := FALSE;
  50. { setup specific opcodetions for first pass }
  51. { Setup special operands }
  52. { Convert to general form as to conform to the m68k opcode table }
  53. if (opcode = A_ADDA) or (opcode = A_ADDI)
  54. then opcode := A_ADD
  55. else
  56. { CMPM excluded because of GAS v1.34 BUG }
  57. if (opcode = A_CMPA) or
  58. (opcode = A_CMPI) then
  59. opcode := A_CMP
  60. else
  61. if opcode = A_EORI then
  62. opcode := A_EOR
  63. else
  64. if opcode = A_MOVEA then
  65. opcode := A_MOVE
  66. else
  67. if opcode = A_ORI then
  68. opcode := A_OR
  69. else
  70. if (opcode = A_SUBA) or (opcode = A_SUBI) then
  71. opcode := A_SUB;
  72. { Setup operand types }
  73. {*
  74. in opcode <> A_MOVEM then
  75. begin
  76. while not(fits) do
  77. begin
  78. { set the opcodetion cache, if the opcodetion }
  79. { occurs the first time }
  80. if (it[i].i=opcode) and (ins_cache[opcode]=-1) then
  81. ins_cache[opcode]:=i;
  82. if (it[i].i=opcode) and (instr.ops=it[i].ops) then
  83. begin
  84. { first fit }
  85. case instr.ops of
  86. 0 : begin
  87. fits:=true;
  88. break;
  89. end;
  90. 1 :
  91. begin
  92. if (optyp1 and it[i].o1)<>0 then
  93. begin
  94. fits:=true;
  95. break;
  96. end;
  97. end;
  98. 2 : if ((optyp1 and it[i].o1)<>0) and
  99. ((optyp2 and it[i].o2)<>0) then
  100. begin
  101. fits:=true;
  102. break;
  103. end
  104. 3 : if ((optyp1 and it[i].o1)<>0) and
  105. ((optyp2 and it[i].o2)<>0) and
  106. ((optyp3 and it[i].o3)<>0) then
  107. begin
  108. fits:=true;
  109. break;
  110. end;
  111. end; { end case }
  112. end; { endif }
  113. if it[i].i=A_NONE then
  114. begin
  115. { NO MATCH! }
  116. Message(asmr_e_invalid_combination_opcode_and_operand);
  117. exit;
  118. end;
  119. inc(i);
  120. end; { end while }
  121. *}
  122. fits:=TRUE;
  123. { We add the opcode to the opcode linked list }
  124. if fits then
  125. begin
  126. case ops of
  127. 0:
  128. if opsize <> S_NO then
  129. result:=(taicpu.op_none(opcode,opsize))
  130. else
  131. result:=(taicpu.op_none(opcode,S_NO));
  132. 1: begin
  133. case operands[1].opr.typ of
  134. OPR_SYMBOL:
  135. begin
  136. result:=(taicpu.op_sym_ofs(opcode,
  137. opsize, operands[1].opr.symbol,operands[1].opr.symofs));
  138. end;
  139. OPR_CONSTANT:
  140. begin
  141. result:=(taicpu.op_const(opcode,
  142. opsize, operands[1].opr.val));
  143. end;
  144. OPR_REGISTER:
  145. result:=(taicpu.op_reg(opcode,opsize,operands[1].opr.reg));
  146. OPR_REFERENCE:
  147. if opsize <> S_NO then
  148. begin
  149. result:=(taicpu.op_ref(opcode,
  150. opsize,operands[1].opr.ref));
  151. end
  152. else
  153. begin
  154. { special jmp and call case with }
  155. { symbolic references. }
  156. if opcode in [A_BSR,A_JMP,A_JSR,A_BRA,A_PEA] then
  157. begin
  158. result:=(taicpu.op_ref(opcode,
  159. S_NO,operands[1].opr.ref));
  160. end
  161. else
  162. Message(asmr_e_invalid_opcode_and_operand);
  163. end;
  164. OPR_NONE:
  165. Message(asmr_e_invalid_opcode_and_operand);
  166. else
  167. begin
  168. Message(asmr_e_invalid_opcode_and_operand);
  169. end;
  170. end;
  171. end;
  172. 2: begin
  173. { source }
  174. case operands[1].opr.typ of
  175. { reg,reg }
  176. { reg,ref }
  177. OPR_REGISTER:
  178. begin
  179. case operands[2].opr.typ of
  180. OPR_REGISTER:
  181. begin
  182. result:=(taicpu.op_reg_reg(opcode,
  183. opsize,operands[1].opr.reg,operands[2].opr.reg));
  184. end;
  185. OPR_REFERENCE:
  186. result:=(taicpu.op_reg_ref(opcode,
  187. opsize,operands[1].opr.reg,operands[2].opr.ref));
  188. else { else case }
  189. begin
  190. Message(asmr_e_invalid_opcode_and_operand);
  191. end;
  192. end; { end second operand case for OPR_REGISTER }
  193. end;
  194. { regset, ref }
  195. OPR_regset:
  196. begin
  197. case operands[2].opr.typ of
  198. OPR_REFERENCE :
  199. result:=(taicpu.op_regset_ref(opcode,
  200. opsize,operands[1].opr.regset,operands[2].opr.ref));
  201. else
  202. begin
  203. Message(asmr_e_invalid_opcode_and_operand);
  204. end;
  205. end; { end second operand case for OPR_regset }
  206. end;
  207. { const,reg }
  208. { const,const }
  209. { const,ref }
  210. OPR_CONSTANT:
  211. case operands[2].opr.typ of
  212. { constant, constant does not have a specific size. }
  213. OPR_CONSTANT:
  214. result:=(taicpu.op_const_const(opcode,
  215. S_NO,operands[1].opr.val,operands[2].opr.val));
  216. OPR_REFERENCE:
  217. begin
  218. result:=(taicpu.op_const_ref(opcode,
  219. opsize,operands[1].opr.val,
  220. operands[2].opr.ref))
  221. end;
  222. OPR_REGISTER:
  223. begin
  224. result:=(taicpu.op_const_reg(opcode,
  225. opsize,operands[1].opr.val,
  226. operands[2].opr.reg))
  227. end;
  228. else
  229. begin
  230. Message(asmr_e_invalid_opcode_and_operand);
  231. end;
  232. end; { end second operand case for OPR_CONSTANT }
  233. { ref,reg }
  234. { ref,ref }
  235. OPR_REFERENCE:
  236. case operands[2].opr.typ of
  237. OPR_REGISTER:
  238. begin
  239. result:=(taicpu.op_ref_reg(opcode,
  240. opsize,operands[1].opr.ref,
  241. operands[2].opr.reg));
  242. end;
  243. OPR_regset:
  244. begin
  245. result:=(taicpu.op_ref_regset(opcode,
  246. opsize,operands[1].opr.ref,
  247. operands[2].opr.regset));
  248. end;
  249. OPR_REFERENCE: { special opcodes }
  250. result:=(taicpu.op_ref_ref(opcode,
  251. opsize,operands[1].opr.ref,
  252. operands[2].opr.ref));
  253. else
  254. begin
  255. Message(asmr_e_invalid_opcode_and_operand);
  256. end;
  257. end; { end second operand case for OPR_REFERENCE }
  258. OPR_SYMBOL: case operands[2].opr.typ of
  259. OPR_REFERENCE:
  260. begin
  261. result:=(taicpu.op_sym_ofs_ref(opcode,
  262. opsize,operands[1].opr.symbol,operands[1].opr.symofs,
  263. operands[2].opr.ref))
  264. end;
  265. OPR_REGISTER:
  266. begin
  267. result:=(taicpu.op_sym_ofs_reg(opcode,
  268. opsize,operands[1].opr.symbol,operands[1].opr.symofs,
  269. operands[2].opr.reg))
  270. end;
  271. else
  272. begin
  273. Message(asmr_e_invalid_opcode_and_operand);
  274. end;
  275. end; { end second operand case for OPR_SYMBOL }
  276. else
  277. begin
  278. Message(asmr_e_invalid_opcode_and_operand);
  279. end;
  280. end; { end first operand case }
  281. end;
  282. 3: begin
  283. if (opcode = A_DIVSL) or (opcode = A_DIVUL) or (opcode = A_MULU)
  284. or (opcode = A_MULS) or (opcode = A_DIVS) or (opcode = A_DIVU) then
  285. begin
  286. if (operands[1].opr.typ <> OPR_REGISTER)
  287. or (operands[2].opr.typ <> OPR_REGISTER)
  288. or (operands[3].opr.typ <> OPR_REGISTER) then
  289. begin
  290. Message(asmr_e_invalid_opcode_and_operand);
  291. end
  292. else
  293. begin
  294. result:=(taicpu. op_reg_reg_reg(opcode,opsize,
  295. operands[1].opr.reg,operands[2].opr.reg,operands[3].opr.reg));
  296. end;
  297. end
  298. else
  299. Message(asmr_e_invalid_opcode_and_operand);
  300. end;
  301. end; { end case }
  302. end;
  303. if assigned(result) then
  304. p.concat(result);
  305. end;
  306. *)
  307. function TM68kInstruction.ConcatLabeledInstr(p : TAsmList):tai;
  308. begin
  309. result:=nil;
  310. if ((opcode >= A_BCC) and (opcode <= A_BVS)) or
  311. (opcode = A_BRA) or (opcode = A_BSR) or
  312. (opcode = A_JMP) or (opcode = A_JSR) or
  313. ((opcode >= A_FBEQ) and (opcode <= A_FBNGLE)) then
  314. begin
  315. if ops > 2 then
  316. Message(asmr_e_invalid_opcode_and_operand)
  317. else if operands[1].opr.typ <> OPR_SYMBOL then
  318. Message(asmr_e_invalid_opcode_and_operand)
  319. else if (operands[1].opr.typ = OPR_SYMBOL) and
  320. (ops = 1) then
  321. if assigned(operands[1].opr.symbol) and
  322. (operands[1].opr.symofs=0) then
  323. result:=taicpu.op_sym(opcode,S_NO,
  324. operands[1].opr.symbol)
  325. else
  326. Message(asmr_e_invalid_opcode_and_operand);
  327. end
  328. else if ((opcode >= A_DBCC) and (opcode <= A_DBF))
  329. or ((opcode >= A_FDBEQ) and (opcode <= A_FDBNGLE)) then
  330. begin
  331. if (ops<>2) or
  332. (operands[1].opr.typ <> OPR_REGISTER) or
  333. (operands[2].opr.typ <> OPR_SYMBOL) or
  334. (operands[2].opr.symofs <> 0) then
  335. Message(asmr_e_invalid_opcode_and_operand)
  336. else
  337. result:=taicpu.op_reg_sym(opcode,opsize,operands[1].opr.reg,
  338. operands[2].opr.symbol);
  339. end
  340. else
  341. Message(asmr_e_invalid_opcode_and_operand);
  342. if assigned(result) then
  343. p.concat(result);
  344. end;
  345. end.