ag68kgas.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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,
  22. globals,globtype,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,assemble,aggas;
  24. type
  25. Tm68kGNUAssembler=class(TGNUassembler)
  26. constructor create(smart: boolean); override;
  27. function MakeCmdLine : TCmdStr; override;
  28. end;
  29. type
  30. Tm68kInstrWriter=class(TCPUInstrWriter)
  31. procedure WriteInstruction(hp: tai);override;
  32. end;
  33. const
  34. gas_opsize2str : array[topsize] of string[2] =
  35. ('','.b','.w','.l','.s','.d','.x','');
  36. implementation
  37. uses
  38. cutils,systems,
  39. cgbase,cgutils,cpuinfo,
  40. verbose,itcpugas;
  41. {****************************************************************************}
  42. { GNU m68k Assembler writer }
  43. {****************************************************************************}
  44. constructor Tm68kGNUAssembler.create(smart: boolean);
  45. begin
  46. inherited create(smart);
  47. InstrWriter := Tm68kInstrWriter.create(self);
  48. end;
  49. function Tm68kGNUAssembler.MakeCmdLine: TCmdStr;
  50. begin
  51. result:=inherited MakeCmdLine;
  52. // Use old -m option for Amiga system
  53. if target_info.system=system_m68k_amiga then
  54. Replace(result,'$ARCH','-m'+GasCpuTypeStr[current_settings.cputype])
  55. else
  56. Replace(result,'$ARCH','-march='+GasCpuTypeStr[current_settings.cputype]);
  57. end;
  58. function getreferencestring(var ref : treference) : string;
  59. var
  60. s,basestr,indexstr : string;
  61. begin
  62. s:='';
  63. with ref do
  64. begin
  65. basestr:=gas_regname(base);
  66. indexstr:=gas_regname(index);
  67. if assigned(symbol) then
  68. s:=s+symbol.name;
  69. if offset<0 then s:=s+tostr(offset)
  70. else if (offset>0) then
  71. begin
  72. if (symbol=nil) then s:=tostr(offset)
  73. else s:=s+'+'+tostr(offset);
  74. end
  75. else if (index=NR_NO) and (base=NR_NO) and not assigned(symbol) then
  76. s:=s+'0';
  77. if (index<>NR_NO) and (base=NR_NO) and (direction=dir_none) then
  78. begin
  79. if (scalefactor = 1) or (scalefactor = 0) then
  80. s:=s+'('+indexstr+'.l)'
  81. else
  82. s:=s+'('+indexstr+'.l*'+tostr(scalefactor)+')'
  83. end
  84. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_inc) then
  85. begin
  86. if (scalefactor = 1) or (scalefactor = 0) then
  87. s:=s+'('+basestr+')+'
  88. else
  89. InternalError(10002);
  90. end
  91. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_dec) then
  92. begin
  93. if (scalefactor = 1) or (scalefactor = 0) then
  94. s:=s+'-('+basestr+')'
  95. else
  96. InternalError(10003);
  97. end
  98. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_none) then
  99. begin
  100. s:=s+'('+basestr+')'
  101. end
  102. else if (index<>NR_NO) and (base<>NR_NO) and (direction=dir_none) then
  103. begin
  104. if (scalefactor = 1) or (scalefactor = 0) then
  105. s:=s+'('+basestr+','+indexstr+'.l)'
  106. else
  107. s:=s+'('+basestr+','+indexstr+'.l*'+tostr(scalefactor)+')';
  108. end;
  109. end;
  110. getreferencestring:=s;
  111. end;
  112. function getopstr(const o:toper) : string;
  113. var
  114. hs : string;
  115. i : tsuperregister;
  116. begin
  117. case o.typ of
  118. top_reg:
  119. getopstr:=gas_regname(o.reg);
  120. top_ref:
  121. if o.ref^.refaddr=addr_full then
  122. begin
  123. if assigned(o.ref^.symbol) then
  124. hs:=o.ref^.symbol.name
  125. else
  126. hs:='#';
  127. if o.ref^.offset>0 then
  128. hs:=hs+'+'+tostr(o.ref^.offset)
  129. else
  130. if o.ref^.offset<0 then
  131. hs:=hs+tostr(o.ref^.offset)
  132. else
  133. if not(assigned(o.ref^.symbol)) then
  134. hs:=hs+'0';
  135. getopstr:=hs;
  136. end
  137. else
  138. getopstr:=getreferencestring(o.ref^);
  139. top_regset:
  140. begin
  141. hs:='';
  142. for i:=RS_D0 to RS_D7 do
  143. begin
  144. if i in o.dataregset^ then
  145. hs:=hs+gas_regname(newreg(R_INTREGISTER,i,R_SUBWHOLE))+'/';
  146. end;
  147. for i:=RS_A0 to RS_SP do
  148. begin
  149. if i in o.addrregset^ then
  150. hs:=hs+gas_regname(newreg(R_ADDRESSREGISTER,i,R_SUBWHOLE))+'/';
  151. end;
  152. delete(hs,length(hs),1);
  153. getopstr := hs;
  154. end;
  155. top_const:
  156. getopstr:='#'+tostr(longint(o.val));
  157. else internalerror(200405021);
  158. end;
  159. end;
  160. function getopstr_jmp(const o:toper) : string;
  161. var
  162. hs : string;
  163. begin
  164. case o.typ of
  165. top_reg:
  166. getopstr_jmp:=gas_regname(o.reg);
  167. top_ref:
  168. if o.ref^.refaddr=addr_no then
  169. getopstr_jmp:=getreferencestring(o.ref^)
  170. else
  171. begin
  172. if assigned(o.ref^.symbol) then
  173. hs:=o.ref^.symbol.name
  174. else
  175. hs:='';
  176. if o.ref^.offset>0 then
  177. hs:=hs+'+'+tostr(o.ref^.offset)
  178. else
  179. if o.ref^.offset<0 then
  180. hs:=hs+tostr(o.ref^.offset)
  181. else
  182. if not(assigned(o.ref^.symbol)) then
  183. hs:=hs+'0';
  184. getopstr_jmp:=hs;
  185. end;
  186. top_const:
  187. getopstr_jmp:=tostr(o.val);
  188. else internalerror(200405022);
  189. end;
  190. end;
  191. {****************************************************************************
  192. TM68kASMOUTPUT
  193. ****************************************************************************}
  194. { returns the opcode string }
  195. function getopcodestring(hp : tai) : string;
  196. var
  197. op : tasmop;
  198. s : string;
  199. begin
  200. op:=taicpu(hp).opcode;
  201. { old versions of GAS don't like PEA.L and LEA.L }
  202. if (op in [
  203. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  204. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  205. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  206. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  207. s:=gas_op2str[op]
  208. else
  209. if op = A_SXX then
  210. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  211. else
  212. { size of DBRA is always WORD, doesn't need opsize (KB) }
  213. if op = A_DBRA then
  214. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  215. else
  216. if op in [a_dbxx,a_bxx,a_fbxx] then
  217. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  218. else
  219. s:=gas_op2str[op]+gas_opsize2str[taicpu(hp).opsize];
  220. if op = A_FMOVE then
  221. begin
  222. {$ifdef DEBUG_CHARLIE}
  223. writeln('fmove! opsize:',dword(taicpu(hp).opsize));
  224. {$endif DEBUG_CHARLIE}
  225. end;
  226. getopcodestring:=s;
  227. end;
  228. procedure Tm68kInstrWriter.WriteInstruction(hp: tai);
  229. var
  230. op : tasmop;
  231. s : string;
  232. sep : char;
  233. calljmp : boolean;
  234. i : integer;
  235. begin
  236. if hp.typ <> ait_instruction then exit;
  237. op:=taicpu(hp).opcode;
  238. calljmp:=is_calljmp(op);
  239. { call maybe not translated to call }
  240. s:=#9+getopcodestring(hp);
  241. { process operands }
  242. if taicpu(hp).ops<>0 then
  243. begin
  244. { call and jmp need an extra handling }
  245. { this code is only called if jmp isn't a labeled instruction }
  246. { quick hack to overcome a problem with manglednames=255 chars }
  247. if calljmp then
  248. begin
  249. owner.AsmWrite(s+#9);
  250. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  251. { dbcc dx,<sym> has two operands! (KB) }
  252. if (taicpu(hp).ops>1) then
  253. s:=s+','+getopstr_jmp(taicpu(hp).oper[1]^);
  254. if (taicpu(hp).ops>2) then
  255. internalerror(2006120501);
  256. end
  257. else
  258. begin
  259. for i:=0 to taicpu(hp).ops-1 do
  260. begin
  261. if i=0 then
  262. sep:=#9
  263. else
  264. if ((op = A_DIVSL) or
  265. (op = A_DIVUL) or
  266. (op = A_MULU) or
  267. (op = A_MULS) or
  268. (op = A_DIVS) or
  269. (op = A_DIVU)) and (i=2) then
  270. begin
  271. sep:=':'
  272. end else
  273. sep:=',';
  274. s:=s+sep+getopstr(taicpu(hp).oper[i]^)
  275. end;
  276. end;
  277. end;
  278. owner.AsmWriteLn(s);
  279. end;
  280. {*****************************************************************************
  281. Initialize
  282. *****************************************************************************}
  283. const
  284. as_m68k_as_info : tasminfo =
  285. (
  286. id : as_gas;
  287. idtxt : 'AS';
  288. asmbin : 'as';
  289. asmcmd : '$ARCH -o $OBJ $EXTRAOPT $ASM';
  290. supported_targets : [system_m68k_Amiga,system_m68k_Atari,system_m68k_Mac,system_m68k_linux,system_m68k_PalmOS,system_m68k_netbsd,system_m68k_openbsd,system_m68k_embedded];
  291. flags : [af_needar,af_smartlink_sections];
  292. labelprefix : '.L';
  293. comment : '# ';
  294. dollarsign: '$';
  295. );
  296. initialization
  297. RegisterAssembler(as_m68k_as_info,Tm68kGNUAssembler);
  298. end.