ag68kgas.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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(size: topsize; 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. case size of
  193. S_FS:
  194. getopstr:='#0x'+hexstr(longint(single(o.val_real)),sizeof(single)*2);
  195. S_FD:
  196. getopstr:='#0x'+hexstr(BestRealRec(o.val_real).Data,sizeof(bestreal)*2);
  197. else
  198. internalerror(2021020801);
  199. end;
  200. end;
  201. else internalerror(200405021);
  202. end;
  203. end;
  204. function getopstr_jmp(var o:toper) : string;
  205. begin
  206. case o.typ of
  207. top_reg:
  208. getopstr_jmp:=gas_regname(o.reg);
  209. top_ref:
  210. if o.ref^.refaddr=addr_no then
  211. getopstr_jmp:=getreferencestring(o.ref^)
  212. else
  213. begin
  214. if assigned(o.ref^.symbol) then
  215. getopstr_jmp:=o.ref^.symbol.name
  216. else
  217. getopstr_jmp:='';
  218. if o.ref^.offset>0 then
  219. getopstr_jmp:=getopstr_jmp+'+'+tostr(o.ref^.offset)
  220. else
  221. if o.ref^.offset<0 then
  222. getopstr_jmp:=getopstr_jmp+tostr(o.ref^.offset)
  223. else
  224. if not(assigned(o.ref^.symbol)) then
  225. getopstr_jmp:=getopstr_jmp+'0';
  226. end;
  227. top_const:
  228. getopstr_jmp:=tostr(o.val);
  229. else
  230. internalerror(200405022);
  231. end;
  232. end;
  233. {****************************************************************************
  234. TM68kASMOUTPUT
  235. ****************************************************************************}
  236. { returns the opcode string }
  237. function getopcodestring(hp : tai) : string;
  238. var
  239. op : tasmop;
  240. begin
  241. op:=taicpu(hp).opcode;
  242. { old versions of GAS don't like PEA.L and LEA.L }
  243. if (op in [
  244. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  245. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  246. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  247. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  248. result:=gas_op2str[op]
  249. else
  250. { Scc/FScc is always BYTE, DBRA/DBcc is always WORD, doesn't need opsize (KB) }
  251. if op in [A_SXX, A_FSXX, A_DBXX, A_DBRA] then
  252. result:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  253. else
  254. { fix me: a fugly hack to utilize GNU AS pseudo instructions for more optimal branching }
  255. if op in [A_JSR] then
  256. result:='jbsr'
  257. else
  258. if op in [A_JMP] then
  259. result:='jra'
  260. else
  261. if op in [A_BXX] then
  262. result:='j'+cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  263. else
  264. if op in [A_FBXX] then
  265. result:='fj'+{gas_op2str[op]+}cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  266. else
  267. result:=gas_op2str[op]+gas_opsize2str[taicpu(hp).opsize];
  268. end;
  269. procedure Tm68kInstrWriter.WriteInstruction(hp: tai);
  270. var
  271. op : tasmop;
  272. s : string;
  273. sep : char;
  274. i : integer;
  275. begin
  276. if hp.typ <> ait_instruction then exit;
  277. op:=taicpu(hp).opcode;
  278. { call maybe not translated to call }
  279. s:=#9+getopcodestring(hp);
  280. { process operands }
  281. if taicpu(hp).ops<>0 then
  282. begin
  283. { call and jmp need an extra handling }
  284. { this code is only called if jmp isn't a labeled instruction }
  285. { quick hack to overcome a problem with manglednames=255 chars }
  286. if is_calljmp(op) then
  287. begin
  288. s:=s+#9+getopstr_jmp(taicpu(hp).oper[0]^);
  289. { dbcc dx,<sym> has two operands! (KB) }
  290. if (taicpu(hp).ops>1) then
  291. s:=s+','+getopstr_jmp(taicpu(hp).oper[1]^);
  292. if (taicpu(hp).ops>2) then
  293. internalerror(2006120501);
  294. end
  295. else
  296. begin
  297. for i:=0 to taicpu(hp).ops-1 do
  298. begin
  299. if i=0 then
  300. sep:=#9
  301. else
  302. if (i=2) and
  303. (op in [A_DIVSL,A_DIVUL,A_MULS,A_MULU,A_DIVS,A_DIVU,A_REMS,A_REMU]) then
  304. sep:=':'
  305. else
  306. sep:=',';
  307. s:=s+sep+getopstr(taicpu(hp).opsize,taicpu(hp).oper[i]^);
  308. end;
  309. end;
  310. end;
  311. owner.writer.AsmWriteLn(s);
  312. end;
  313. {*****************************************************************************
  314. Initialize
  315. *****************************************************************************}
  316. const
  317. as_m68k_as_info : tasminfo =
  318. (
  319. id : as_gas;
  320. idtxt : 'AS';
  321. asmbin : 'as';
  322. asmcmd : '$ARCH -o $OBJ $EXTRAOPT $ASM';
  323. supported_targets : [system_m68k_macosclassic,system_m68k_linux,system_m68k_PalmOS,system_m68k_netbsd,system_m68k_embedded];
  324. flags : [af_needar,af_smartlink_sections];
  325. labelprefix : '.L';
  326. comment : '# ';
  327. dollarsign: '$';
  328. );
  329. as_m68k_as_aout_info : tasminfo =
  330. (
  331. id : as_m68k_as_aout;
  332. idtxt : 'AS-AOUT';
  333. asmbin : 'as';
  334. asmcmd : '$ARCH -o $OBJ $EXTRAOPT $ASM';
  335. supported_targets : [system_m68k_Amiga,system_m68k_Atari];
  336. flags : [af_needar];
  337. labelprefix : '.L';
  338. comment : '# ';
  339. dollarsign: '$';
  340. );
  341. initialization
  342. RegisterAssembler(as_m68k_as_info,Tm68kGNUAssembler);
  343. RegisterAssembler(as_m68k_as_aout_info,Tm68kAoutGNUAssembler);
  344. end.