ag68kgas.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, 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.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, 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(var o:toper) : string;
  113. var
  114. i : tsuperregister;
  115. begin
  116. case o.typ of
  117. top_reg:
  118. getopstr:=gas_regname(o.reg);
  119. top_ref:
  120. if o.ref^.refaddr=addr_full then
  121. begin
  122. if assigned(o.ref^.symbol) then
  123. getopstr:=o.ref^.symbol.name
  124. else
  125. getopstr:='#';
  126. if o.ref^.offset>0 then
  127. getopstr:=getopstr+'+'+tostr(o.ref^.offset)
  128. else
  129. if o.ref^.offset<0 then
  130. getopstr:=getopstr+tostr(o.ref^.offset)
  131. else
  132. if not(assigned(o.ref^.symbol)) then
  133. getopstr:=getopstr+'0';
  134. end
  135. else
  136. getopstr:=getreferencestring(o.ref^);
  137. top_regset:
  138. begin
  139. getopstr:='';
  140. for i:=RS_D0 to RS_D7 do
  141. begin
  142. if i in o.dataregset then
  143. getopstr:=getopstr+gas_regname(newreg(R_INTREGISTER,i,R_SUBWHOLE))+'/';
  144. end;
  145. for i:=RS_A0 to RS_SP do
  146. begin
  147. if i in o.addrregset then
  148. getopstr:=getopstr+gas_regname(newreg(R_ADDRESSREGISTER,i,R_SUBWHOLE))+'/';
  149. end;
  150. for i:=RS_FP0 to RS_FP7 do
  151. begin
  152. if i in o.fpuregset then
  153. getopstr:=getopstr+gas_regname(newreg(R_FPUREGISTER,i,R_SUBNONE))+'/';
  154. end;
  155. delete(getopstr,length(getopstr),1);
  156. end;
  157. top_regpair:
  158. getopstr:=gas_regname(o.reghi)+':'+gas_regname(o.reglo);
  159. top_const:
  160. getopstr:='#'+tostr(longint(o.val));
  161. top_realconst:
  162. begin
  163. str(o.val_real,getopstr);
  164. if getopstr[1]=' ' then
  165. getopstr[1]:='+';
  166. getopstr:='#0d'+getopstr;
  167. end;
  168. else internalerror(200405021);
  169. end;
  170. end;
  171. function getopstr_jmp(var o:toper) : string;
  172. begin
  173. case o.typ of
  174. top_reg:
  175. getopstr_jmp:=gas_regname(o.reg);
  176. top_ref:
  177. if o.ref^.refaddr=addr_no then
  178. getopstr_jmp:=getreferencestring(o.ref^)
  179. else
  180. begin
  181. if assigned(o.ref^.symbol) then
  182. getopstr_jmp:=o.ref^.symbol.name
  183. else
  184. getopstr_jmp:='';
  185. if o.ref^.offset>0 then
  186. getopstr_jmp:=getopstr_jmp+'+'+tostr(o.ref^.offset)
  187. else
  188. if o.ref^.offset<0 then
  189. getopstr_jmp:=getopstr_jmp+tostr(o.ref^.offset)
  190. else
  191. if not(assigned(o.ref^.symbol)) then
  192. getopstr_jmp:=getopstr_jmp+'0';
  193. end;
  194. top_const:
  195. getopstr_jmp:=tostr(o.val);
  196. else
  197. internalerror(200405022);
  198. end;
  199. end;
  200. {****************************************************************************
  201. TM68kASMOUTPUT
  202. ****************************************************************************}
  203. { returns the opcode string }
  204. function getopcodestring(hp : tai) : string;
  205. var
  206. op : tasmop;
  207. begin
  208. op:=taicpu(hp).opcode;
  209. { old versions of GAS don't like PEA.L and LEA.L }
  210. if (op in [
  211. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  212. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  213. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  214. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  215. result:=gas_op2str[op]
  216. else
  217. { Scc/FScc is always BYTE, DBRA/DBcc is always WORD, doesn't need opsize (KB) }
  218. if op in [A_SXX, A_FSXX, A_DBXX, A_DBRA] then
  219. result:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  220. else
  221. { fix me: a fugly hack to utilize GNU AS pseudo instructions for more optimal branching }
  222. if op in [A_JSR] then
  223. result:='jbsr'
  224. else
  225. if op in [A_JMP] then
  226. result:='jra'
  227. else
  228. if op in [A_BXX] then
  229. result:='j'+cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  230. else
  231. if op in [A_FBXX] then
  232. result:='fj'+{gas_op2str[op]+}cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  233. else
  234. result:=gas_op2str[op]+gas_opsize2str[taicpu(hp).opsize];
  235. end;
  236. procedure Tm68kInstrWriter.WriteInstruction(hp: tai);
  237. var
  238. op : tasmop;
  239. s : string;
  240. sep : char;
  241. i : integer;
  242. begin
  243. if hp.typ <> ait_instruction then exit;
  244. op:=taicpu(hp).opcode;
  245. { call maybe not translated to call }
  246. s:=#9+getopcodestring(hp);
  247. { process operands }
  248. if taicpu(hp).ops<>0 then
  249. begin
  250. { call and jmp need an extra handling }
  251. { this code is only called if jmp isn't a labeled instruction }
  252. { quick hack to overcome a problem with manglednames=255 chars }
  253. if is_calljmp(op) then
  254. begin
  255. s:=s+#9+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 (i=2) and
  270. (op in [A_DIVSL,A_DIVUL,A_MULS,A_MULU,A_DIVS,A_DIVU,A_REMS,A_REMU]) then
  271. sep:=':'
  272. else
  273. sep:=',';
  274. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  275. end;
  276. end;
  277. end;
  278. owner.writer.AsmWriteLn(s);
  279. end;
  280. {*****************************************************************************
  281. Initialize
  282. *****************************************************************************}
  283. const
  284. as_m68k_as_info : tasminfo =
  285. (
  286. id : as_gas;
  287. idtxt : 'AS';
  288. asmbin : 'as';
  289. asmcmd : '$ARCH -o $OBJ $EXTRAOPT $ASM';
  290. 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];
  291. flags : [af_needar,af_smartlink_sections];
  292. labelprefix : '.L';
  293. comment : '# ';
  294. dollarsign: '$';
  295. );
  296. initialization
  297. RegisterAssembler(as_m68k_as_info,Tm68kGNUAssembler);
  298. end.