ag68kgas.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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(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_const:
  158. getopstr:='#'+tostr(longint(o.val));
  159. top_realconst:
  160. begin
  161. str(o.val_real,getopstr);
  162. if getopstr[1]=' ' then
  163. getopstr[1]:='+';
  164. getopstr:='#0d'+getopstr;
  165. end;
  166. else internalerror(200405021);
  167. end;
  168. end;
  169. function getopstr_jmp(var o:toper) : string;
  170. begin
  171. case o.typ of
  172. top_reg:
  173. getopstr_jmp:=gas_regname(o.reg);
  174. top_ref:
  175. if o.ref^.refaddr=addr_no then
  176. getopstr_jmp:=getreferencestring(o.ref^)
  177. else
  178. begin
  179. if assigned(o.ref^.symbol) then
  180. getopstr_jmp:=o.ref^.symbol.name
  181. else
  182. getopstr_jmp:='';
  183. if o.ref^.offset>0 then
  184. getopstr_jmp:=getopstr_jmp+'+'+tostr(o.ref^.offset)
  185. else
  186. if o.ref^.offset<0 then
  187. getopstr_jmp:=getopstr_jmp+tostr(o.ref^.offset)
  188. else
  189. if not(assigned(o.ref^.symbol)) then
  190. getopstr_jmp:=getopstr_jmp+'0';
  191. end;
  192. top_const:
  193. getopstr_jmp:=tostr(o.val);
  194. else
  195. internalerror(200405022);
  196. end;
  197. end;
  198. {****************************************************************************
  199. TM68kASMOUTPUT
  200. ****************************************************************************}
  201. { returns the opcode string }
  202. function getopcodestring(hp : tai) : string;
  203. var
  204. op : tasmop;
  205. begin
  206. op:=taicpu(hp).opcode;
  207. { old versions of GAS don't like PEA.L and LEA.L }
  208. if (op in [
  209. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  210. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  211. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  212. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  213. result:=gas_op2str[op]
  214. else
  215. { Scc/FScc is always BYTE, DBRA/DBcc is always WORD, doesn't need opsize (KB) }
  216. if op in [A_SXX, A_FSXX, A_DBXX, A_DBRA] then
  217. result:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  218. else
  219. { fix me: a fugly hack to utilize GNU AS pseudo instructions for more optimal branching }
  220. if op in [A_JSR] then
  221. result:='jbsr'
  222. else
  223. if op in [A_JMP] then
  224. result:='jra'
  225. else
  226. if op in [A_BXX] then
  227. result:='j'+cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  228. else
  229. if op in [A_FBXX] then
  230. result:='fj'+{gas_op2str[op]+}cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  231. else
  232. result:=gas_op2str[op]+gas_opsize2str[taicpu(hp).opsize];
  233. end;
  234. procedure Tm68kInstrWriter.WriteInstruction(hp: tai);
  235. var
  236. op : tasmop;
  237. s : string;
  238. sep : char;
  239. i : integer;
  240. begin
  241. if hp.typ <> ait_instruction then exit;
  242. op:=taicpu(hp).opcode;
  243. { call maybe not translated to call }
  244. s:=#9+getopcodestring(hp);
  245. { process operands }
  246. if taicpu(hp).ops<>0 then
  247. begin
  248. { call and jmp need an extra handling }
  249. { this code is only called if jmp isn't a labeled instruction }
  250. { quick hack to overcome a problem with manglednames=255 chars }
  251. if is_calljmp(op) then
  252. begin
  253. s:=s+#9+getopstr_jmp(taicpu(hp).oper[0]^);
  254. { dbcc dx,<sym> has two operands! (KB) }
  255. if (taicpu(hp).ops>1) then
  256. s:=s+','+getopstr_jmp(taicpu(hp).oper[1]^);
  257. if (taicpu(hp).ops>2) then
  258. internalerror(2006120501);
  259. end
  260. else
  261. begin
  262. for i:=0 to taicpu(hp).ops-1 do
  263. begin
  264. if i=0 then
  265. sep:=#9
  266. else
  267. if (i=2) and
  268. (op in [A_DIVSL,A_DIVUL,A_MULS,A_MULU,A_DIVS,A_DIVU,A_REMS,A_REMU]) then
  269. sep:=':'
  270. else
  271. sep:=',';
  272. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  273. end;
  274. end;
  275. end;
  276. owner.writer.AsmWriteLn(s);
  277. end;
  278. {*****************************************************************************
  279. Initialize
  280. *****************************************************************************}
  281. const
  282. as_m68k_as_info : tasminfo =
  283. (
  284. id : as_gas;
  285. idtxt : 'AS';
  286. asmbin : 'as';
  287. asmcmd : '$ARCH -o $OBJ $EXTRAOPT $ASM';
  288. 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];
  289. flags : [af_needar,af_smartlink_sections];
  290. labelprefix : '.L';
  291. comment : '# ';
  292. dollarsign: '$';
  293. );
  294. initialization
  295. RegisterAssembler(as_m68k_as_info,Tm68kGNUAssembler);
  296. end.