ag68kgas.pas 13 KB

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