ag68kgas.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. {
  2. Copyright (c) 1998-2006 by the Free Pascal development team
  3. This unit implements an asmoutput class for m68k GAS syntax
  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 ag68kgas;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,cpubase,systems,
  22. globals,globtype,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,assemble,aggas;
  24. type
  25. Tm68kGNUAssembler=class(TGNUassembler)
  26. constructor create(info: pasminfo; smart: boolean); override;
  27. function MakeCmdLine : TCmdStr; override;
  28. end;
  29. type
  30. Tm68kInstrWriter=class(TCPUInstrWriter)
  31. procedure WriteInstruction(hp: tai);override;
  32. end;
  33. const
  34. gas_opsize2str : array[topsize] of string[2] =
  35. ('','.b','.w','.l','.s','.d','.x','');
  36. implementation
  37. uses
  38. cutils,
  39. cgbase,cgutils,cpuinfo,
  40. verbose,itcpugas;
  41. {****************************************************************************}
  42. { GNU m68k Assembler writer }
  43. {****************************************************************************}
  44. constructor Tm68kGNUAssembler.create(info: pasminfo; smart: boolean);
  45. begin
  46. inherited;
  47. InstrWriter := Tm68kInstrWriter.create(self);
  48. end;
  49. function Tm68kGNUAssembler.MakeCmdLine: TCmdStr;
  50. begin
  51. result:=inherited MakeCmdLine;
  52. // Use old -m option for Amiga system
  53. if target_info.system=system_m68k_amiga then
  54. Replace(result,'$ARCH','-m'+GasCpuTypeStr[current_settings.cputype])
  55. else
  56. Replace(result,'$ARCH','-march='+GasCpuTypeStr[current_settings.cputype]);
  57. end;
  58. function getreferencestring(var ref : treference) : string;
  59. var
  60. s,basestr,indexstr : string;
  61. begin
  62. s:='';
  63. with ref do
  64. begin
  65. basestr:=gas_regname(base);
  66. indexstr:=gas_regname(index);
  67. if assigned(symbol) then
  68. s:=s+symbol.name;
  69. if offset<0 then s:=s+tostr(offset)
  70. else if (offset>0) then
  71. begin
  72. if (symbol=nil) then s:=tostr(offset)
  73. else s:=s+'+'+tostr(offset);
  74. end
  75. else if (index=NR_NO) and (base=NR_NO) and not assigned(symbol) then
  76. s:=s+'0';
  77. if (index<>NR_NO) and (base=NR_NO) and (direction=dir_none) then
  78. begin
  79. if (scalefactor = 1) or (scalefactor = 0) then
  80. s:=s+'('+indexstr+'.l)'
  81. else
  82. s:=s+'('+indexstr+'.l*'+tostr(scalefactor)+')'
  83. end
  84. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_inc) then
  85. begin
  86. if (scalefactor = 1) or (scalefactor = 0) then
  87. s:=s+'('+basestr+')+'
  88. else
  89. InternalError(10002);
  90. end
  91. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_dec) then
  92. begin
  93. if (scalefactor = 1) or (scalefactor = 0) then
  94. s:=s+'-('+basestr+')'
  95. else
  96. InternalError(10003);
  97. end
  98. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_none) then
  99. begin
  100. s:=s+'('+basestr+')'
  101. end
  102. else if (index<>NR_NO) and (base<>NR_NO) and (direction=dir_none) then
  103. begin
  104. if (scalefactor = 1) or (scalefactor = 0) then
  105. s:=s+'('+basestr+','+indexstr+'.l)'
  106. else
  107. s:=s+'('+basestr+','+indexstr+'.l*'+tostr(scalefactor)+')';
  108. end;
  109. end;
  110. getreferencestring:=s;
  111. end;
  112. function getopstr(const o:toper) : string;
  113. var
  114. hs : string;
  115. i : tsuperregister;
  116. begin
  117. case o.typ of
  118. top_reg:
  119. getopstr:=gas_regname(o.reg);
  120. top_ref:
  121. if o.ref^.refaddr=addr_full then
  122. begin
  123. if assigned(o.ref^.symbol) then
  124. hs:=o.ref^.symbol.name
  125. else
  126. hs:='#';
  127. if o.ref^.offset>0 then
  128. hs:=hs+'+'+tostr(o.ref^.offset)
  129. else
  130. if o.ref^.offset<0 then
  131. hs:=hs+tostr(o.ref^.offset)
  132. else
  133. if not(assigned(o.ref^.symbol)) then
  134. hs:=hs+'0';
  135. getopstr:=hs;
  136. end
  137. else
  138. getopstr:=getreferencestring(o.ref^);
  139. top_regset:
  140. begin
  141. hs:='';
  142. for i:=RS_D0 to RS_D7 do
  143. begin
  144. if i in o.dataregset^ then
  145. hs:=hs+gas_regname(newreg(R_INTREGISTER,i,R_SUBWHOLE))+'/';
  146. end;
  147. for i:=RS_A0 to RS_SP do
  148. begin
  149. if i in o.addrregset^ then
  150. hs:=hs+gas_regname(newreg(R_ADDRESSREGISTER,i,R_SUBWHOLE))+'/';
  151. end;
  152. for i:=RS_FP0 to RS_FP7 do
  153. begin
  154. if i in o.fpuregset^ then
  155. hs:=hs+gas_regname(newreg(R_FPUREGISTER,i,R_SUBWHOLE))+'/';
  156. end;
  157. delete(hs,length(hs),1);
  158. getopstr := hs;
  159. end;
  160. top_const:
  161. getopstr:='#'+tostr(longint(o.val));
  162. else internalerror(200405021);
  163. end;
  164. end;
  165. function getopstr_jmp(const o:toper) : string;
  166. var
  167. hs : string;
  168. begin
  169. case o.typ of
  170. top_reg:
  171. getopstr_jmp:=gas_regname(o.reg);
  172. top_ref:
  173. if o.ref^.refaddr=addr_no then
  174. getopstr_jmp:=getreferencestring(o.ref^)
  175. else
  176. begin
  177. if assigned(o.ref^.symbol) then
  178. hs:=o.ref^.symbol.name
  179. else
  180. hs:='';
  181. if o.ref^.offset>0 then
  182. hs:=hs+'+'+tostr(o.ref^.offset)
  183. else
  184. if o.ref^.offset<0 then
  185. hs:=hs+tostr(o.ref^.offset)
  186. else
  187. if not(assigned(o.ref^.symbol)) then
  188. hs:=hs+'0';
  189. getopstr_jmp:=hs;
  190. end;
  191. top_const:
  192. getopstr_jmp:=tostr(o.val);
  193. else internalerror(200405022);
  194. end;
  195. end;
  196. {****************************************************************************
  197. TM68kASMOUTPUT
  198. ****************************************************************************}
  199. { returns the opcode string }
  200. function getopcodestring(hp : tai) : string;
  201. var
  202. op : tasmop;
  203. s : string;
  204. begin
  205. op:=taicpu(hp).opcode;
  206. { old versions of GAS don't like PEA.L and LEA.L }
  207. if (op in [
  208. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  209. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  210. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  211. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  212. s:=gas_op2str[op]
  213. else
  214. if op in [A_SXX, A_FSXX] then
  215. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  216. else
  217. { size of DBRA is always WORD, doesn't need opsize (KB) }
  218. if op = A_DBRA then
  219. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  220. else
  221. if op in [a_dbxx,a_bxx,a_fbxx] then
  222. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  223. else
  224. s:=gas_op2str[op]+gas_opsize2str[taicpu(hp).opsize];
  225. if op = A_FMOVE then
  226. begin
  227. {$ifdef DEBUG_CHARLIE}
  228. writeln('fmove! opsize:',dword(taicpu(hp).opsize));
  229. {$endif DEBUG_CHARLIE}
  230. end;
  231. getopcodestring:=s;
  232. end;
  233. procedure Tm68kInstrWriter.WriteInstruction(hp: tai);
  234. var
  235. op : tasmop;
  236. s : string;
  237. sep : char;
  238. calljmp : boolean;
  239. i : integer;
  240. begin
  241. if hp.typ <> ait_instruction then exit;
  242. op:=taicpu(hp).opcode;
  243. calljmp:=is_calljmp(op);
  244. { call maybe not translated to call }
  245. s:=#9+getopcodestring(hp);
  246. { process operands }
  247. if taicpu(hp).ops<>0 then
  248. begin
  249. { call and jmp need an extra handling }
  250. { this code is only called if jmp isn't a labeled instruction }
  251. { quick hack to overcome a problem with manglednames=255 chars }
  252. if calljmp then
  253. begin
  254. owner.writer.AsmWrite(s+#9);
  255. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  256. { dbcc dx,<sym> has two operands! (KB) }
  257. if (taicpu(hp).ops>1) then
  258. s:=s+','+getopstr_jmp(taicpu(hp).oper[1]^);
  259. if (taicpu(hp).ops>2) then
  260. internalerror(2006120501);
  261. end
  262. else
  263. begin
  264. for i:=0 to taicpu(hp).ops-1 do
  265. begin
  266. if i=0 then
  267. sep:=#9
  268. else
  269. if ((op = A_DIVSL) or
  270. (op = A_DIVUL) or
  271. (op = A_MULU) or
  272. (op = A_MULS) or
  273. (op = A_DIVS) or
  274. (op = A_DIVU)) and (i=2) then
  275. begin
  276. sep:=':'
  277. end else
  278. sep:=',';
  279. s:=s+sep+getopstr(taicpu(hp).oper[i]^)
  280. end;
  281. end;
  282. end;
  283. owner.writer.AsmWriteLn(s);
  284. end;
  285. {*****************************************************************************
  286. Initialize
  287. *****************************************************************************}
  288. const
  289. as_m68k_as_info : tasminfo =
  290. (
  291. id : as_gas;
  292. idtxt : 'AS';
  293. asmbin : 'as';
  294. asmcmd : '$ARCH -o $OBJ $EXTRAOPT $ASM';
  295. supported_targets : [system_m68k_Amiga,system_m68k_Atari,system_m68k_Mac,system_m68k_linux,system_m68k_PalmOS,system_m68k_netbsd,system_m68k_openbsd,system_m68k_embedded];
  296. flags : [af_needar,af_smartlink_sections];
  297. labelprefix : '.L';
  298. comment : '# ';
  299. dollarsign: '$';
  300. );
  301. initialization
  302. RegisterAssembler(as_m68k_as_info,Tm68kGNUAssembler);
  303. end.