ag68kgas.pas 13 KB

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