ag68kgas.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. 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+'.l)'
  113. else
  114. s:=s+'('+basestr+','+indexstr+'.l*'+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+'.l)'
  121. else
  122. s:=s+'('+indexstr+'.l*'+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. { old versions of GAS don't like PEA.L and LEA.L }
  239. if (op in [
  240. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  241. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  242. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  243. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  244. result:=gas_op2str[op]
  245. else
  246. { Scc/FScc is always BYTE, DBRA/DBcc is always WORD, doesn't need opsize (KB) }
  247. if op in [A_SXX, A_FSXX, A_DBXX, A_DBRA] then
  248. result:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  249. else
  250. { fix me: a fugly hack to utilize GNU AS pseudo instructions for more optimal branching }
  251. if op in [A_JSR] then
  252. result:='jbsr'
  253. else
  254. if op in [A_JMP] then
  255. result:='jra'
  256. else
  257. if op in [A_BXX] then
  258. result:='j'+cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  259. else
  260. if op in [A_FBXX] then
  261. result:='fj'+{gas_op2str[op]+}cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  262. else
  263. result:=gas_op2str[op]+gas_opsize2str[taicpu(hp).opsize];
  264. end;
  265. procedure Tm68kInstrWriter.WriteInstruction(hp: tai);
  266. var
  267. op : tasmop;
  268. s : string;
  269. sep : char;
  270. i : integer;
  271. begin
  272. if hp.typ <> ait_instruction then exit;
  273. op:=taicpu(hp).opcode;
  274. { call maybe not translated to call }
  275. s:=#9+getopcodestring(hp);
  276. { process operands }
  277. if taicpu(hp).ops<>0 then
  278. begin
  279. { call and jmp need an extra handling }
  280. { this code is only called if jmp isn't a labeled instruction }
  281. { quick hack to overcome a problem with manglednames=255 chars }
  282. if is_calljmp(op) then
  283. begin
  284. s:=s+#9+getopstr_jmp(taicpu(hp).oper[0]^);
  285. { dbcc dx,<sym> has two operands! (KB) }
  286. if (taicpu(hp).ops>1) then
  287. s:=s+','+getopstr_jmp(taicpu(hp).oper[1]^);
  288. if (taicpu(hp).ops>2) then
  289. internalerror(2006120501);
  290. end
  291. else
  292. begin
  293. for i:=0 to taicpu(hp).ops-1 do
  294. begin
  295. if i=0 then
  296. sep:=#9
  297. else
  298. if (i=2) and
  299. (op in [A_DIVSL,A_DIVUL,A_MULS,A_MULU,A_DIVS,A_DIVU,A_REMS,A_REMU]) then
  300. sep:=':'
  301. else
  302. sep:=',';
  303. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  304. end;
  305. end;
  306. end;
  307. owner.writer.AsmWriteLn(s);
  308. end;
  309. {*****************************************************************************
  310. Initialize
  311. *****************************************************************************}
  312. const
  313. as_m68k_as_info : tasminfo =
  314. (
  315. id : as_gas;
  316. idtxt : 'AS';
  317. asmbin : 'as';
  318. asmcmd : '$ARCH -o $OBJ $EXTRAOPT $ASM';
  319. supported_targets : [system_m68k_macos,system_m68k_linux,system_m68k_PalmOS,system_m68k_netbsd,system_m68k_embedded];
  320. flags : [af_needar,af_smartlink_sections];
  321. labelprefix : '.L';
  322. comment : '# ';
  323. dollarsign: '$';
  324. );
  325. as_m68k_as_aout_info : tasminfo =
  326. (
  327. id : as_m68k_as_aout;
  328. idtxt : 'AS-AOUT';
  329. asmbin : 'as';
  330. asmcmd : '$ARCH -o $OBJ $EXTRAOPT $ASM';
  331. supported_targets : [system_m68k_Amiga,system_m68k_Atari];
  332. flags : [af_needar];
  333. labelprefix : '.L';
  334. comment : '# ';
  335. dollarsign: '$';
  336. );
  337. initialization
  338. RegisterAssembler(as_m68k_as_info,Tm68kGNUAssembler);
  339. RegisterAssembler(as_m68k_as_aout_info,Tm68kAoutGNUAssembler);
  340. end.