ag68kgas.pas 14 KB

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