ag68kgas.pas 13 KB

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