ag68kgas.pas 11 KB

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