agcpugas.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements an asmoutput class for m68k GAS syntax
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { This unit implements an asmoutput class for i386 AT&T syntax
  19. }
  20. unit agcpugas;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cclasses,cpubase,
  25. globals,
  26. aasmbase,aasmtai,aasmcpu,assemble,aggas;
  27. type
  28. TM68kAssembler=class(TGNUassembler)
  29. public
  30. procedure WriteInstruction(hp: tai);override;
  31. end;
  32. const
  33. gas_opsize2str : array[topsize] of string[2] =
  34. ('','.b','.w','.l','.s','.d','.x',''
  35. );
  36. implementation
  37. uses
  38. cutils,systems,
  39. cgbase,
  40. verbose,itcpugas;
  41. function getreferencestring(var ref : treference) : string;
  42. var
  43. s,basestr,indexstr : string;
  44. begin
  45. s:='';
  46. with ref do
  47. begin
  48. basestr:=gas_regname(base);
  49. indexstr:=gas_regname(index);
  50. if assigned(symbol) then
  51. s:=s+symbol.name;
  52. if offset<0 then s:=s+tostr(offset)
  53. else if (offset>0) then
  54. begin
  55. if (symbol=nil) then s:=tostr(offset)
  56. else s:=s+'+'+tostr(offset);
  57. end
  58. else if (index=NR_NO) and (base=NR_NO) and not assigned(symbol) then
  59. s:=s+'0';
  60. if (index<>NR_NO) and (base=NR_NO) and (direction=dir_none) then
  61. begin
  62. if (scalefactor = 1) or (scalefactor = 0) then
  63. s:=s+'(,'+indexstr+'.l)'
  64. else
  65. s:=s+'(,'+indexstr+'.l*'+tostr(scalefactor)+')'
  66. end
  67. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_inc) then
  68. begin
  69. if (scalefactor = 1) or (scalefactor = 0) then
  70. s:=s+'('+basestr+')+'
  71. else
  72. InternalError(10002);
  73. end
  74. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_dec) then
  75. begin
  76. if (scalefactor = 1) or (scalefactor = 0) then
  77. s:=s+'-('+basestr+')'
  78. else
  79. InternalError(10003);
  80. end
  81. else if (index=NR_NO) and (base<>NR_NO) and (direction=dir_none) then
  82. begin
  83. s:=s+'('+basestr+')'
  84. end
  85. else if (index<>NR_NO) and (base<>NR_NO) and (direction=dir_none) then
  86. begin
  87. if (scalefactor = 1) or (scalefactor = 0) then
  88. s:=s+'('+basestr+','+indexstr+'.l)'
  89. else
  90. s:=s+'('+basestr+','+indexstr+'.l*'+tostr(scalefactor)+')';
  91. end;
  92. end;
  93. getreferencestring:=s;
  94. end;
  95. function getopstr(const o:toper) : string;
  96. var
  97. hs : string;
  98. i : tsuperregister;
  99. begin
  100. case o.typ of
  101. top_reg:
  102. getopstr:=gas_regname(o.reg);
  103. top_ref:
  104. if o.ref^.refaddr=addr_full then
  105. begin
  106. if assigned(o.ref^.symbol) then
  107. hs:='#'+o.ref^.symbol.name
  108. else
  109. hs:='#';
  110. if o.ref^.offset>0 then
  111. hs:=hs+'+'+tostr(o.ref^.offset)
  112. else
  113. if o.ref^.offset<0 then
  114. hs:=hs+tostr(o.ref^.offset)
  115. else
  116. if not(assigned(o.ref^.symbol)) then
  117. hs:=hs+'0';
  118. getopstr:=hs;
  119. end
  120. else
  121. getopstr:=getreferencestring(o.ref^);
  122. top_reglist:
  123. begin
  124. hs:='';
  125. for i:=RS_D0 to RS_D7 do
  126. begin
  127. if i in o.regset^ then
  128. hs:=hs+gas_regname(newreg(R_INTREGISTER,i,R_SUBWHOLE))+'/';
  129. end;
  130. for i:=RS_A0 to RS_SP do
  131. begin
  132. if i in o.regset^ then
  133. hs:=hs+gas_regname(newreg(R_INTREGISTER,i,R_SUBWHOLE))+'/';
  134. end;
  135. delete(hs,length(hs),1);
  136. getopstr := hs;
  137. end;
  138. top_const:
  139. getopstr:='#'+tostr(longint(o.val));
  140. else internalerror(200405021);
  141. end;
  142. end;
  143. function getopstr_jmp(const o:toper) : string;
  144. var
  145. hs : string;
  146. begin
  147. case o.typ of
  148. top_reg:
  149. getopstr_jmp:=gas_regname(o.reg);
  150. top_ref:
  151. if o.ref^.refaddr=addr_no then
  152. getopstr_jmp:=getreferencestring(o.ref^)
  153. else
  154. begin
  155. if assigned(o.ref^.symbol) then
  156. hs:=o.ref^.symbol.name
  157. else
  158. hs:='';
  159. if o.ref^.offset>0 then
  160. hs:=hs+'+'+tostr(o.ref^.offset)
  161. else
  162. if o.ref^.offset<0 then
  163. hs:=hs+tostr(o.ref^.offset)
  164. else
  165. if not(assigned(o.ref^.symbol)) then
  166. hs:=hs+'0';
  167. getopstr_jmp:=hs;
  168. end;
  169. top_const:
  170. getopstr_jmp:=tostr(o.val);
  171. else internalerror(200405022);
  172. end;
  173. end;
  174. {****************************************************************************
  175. TM68kASMOUTPUT
  176. ****************************************************************************}
  177. { returns the opcode string }
  178. function getopcodestring(hp : tai) : string;
  179. var
  180. op : tasmop;
  181. s : string;
  182. begin
  183. op:=taicpu(hp).opcode;
  184. { old versions of GAS don't like PEA.L and LEA.L }
  185. if (op in [
  186. A_LEA,A_PEA,A_ABCD,A_BCHG,A_BCLR,A_BSET,A_BTST,
  187. A_EXG,A_NBCD,A_SBCD,A_SWAP,A_TAS,A_SCC,A_SCS,
  188. A_SEQ,A_SGE,A_SGT,A_SHI,A_SLE,A_SLS,A_SLT,A_SMI,
  189. A_SNE,A_SPL,A_ST,A_SVC,A_SVS,A_SF]) then
  190. s:=gas_op2str[op]
  191. else
  192. if op = A_SXX then
  193. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]
  194. else
  195. if op in [a_dbxx,a_bxx,a_fbxx] then
  196. s:=gas_op2str[op]+cond2str[taicpu(hp).condition]+gas_opsize2str[taicpu(hp).opsize]
  197. else
  198. s:=gas_op2str[op]+gas_opsize2str[taicpu(hp).opsize];
  199. getopcodestring:=s;
  200. end;
  201. procedure TM68kAssembler.WriteInstruction(hp: tai);
  202. var
  203. op : tasmop;
  204. s : string;
  205. sep : char;
  206. calljmp : boolean;
  207. i : integer;
  208. begin
  209. if hp.typ <> ait_instruction then exit;
  210. op:=taicpu(hp).opcode;
  211. calljmp:=is_calljmp(op);
  212. { call maybe not translated to call }
  213. s:=#9+getopcodestring(hp);
  214. { process operands }
  215. if taicpu(hp).ops<>0 then
  216. begin
  217. { call and jmp need an extra handling }
  218. { this code is only called if jmp isn't a labeled instruction }
  219. { quick hack to overcome a problem with manglednames=255 chars }
  220. if calljmp then
  221. begin
  222. AsmWrite(s+#9);
  223. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  224. end
  225. else
  226. begin
  227. for i:=0 to taicpu(hp).ops-1 do
  228. begin
  229. if i=0 then
  230. sep:=#9
  231. else
  232. if ((op = A_DIVSL) or
  233. (op = A_DIVUL) or
  234. (op = A_MULU) or
  235. (op = A_MULS) or
  236. (op = A_DIVS) or
  237. (op = A_DIVU)) and (i=1) then
  238. sep:=':'
  239. else
  240. sep:=',';
  241. s:=s+sep+getopstr(taicpu(hp).oper[i]^)
  242. end;
  243. end;
  244. end;
  245. AsmWriteLn(s);
  246. end;
  247. {*****************************************************************************
  248. Initialize
  249. *****************************************************************************}
  250. const
  251. as_m68k_as_info : tasminfo =
  252. (
  253. id : as_gas;
  254. idtxt : 'AS';
  255. asmbin : 'as';
  256. asmcmd : '-o $OBJ $ASM';
  257. supported_target : system_any;
  258. outputbinary: false;
  259. allowdirect : true;
  260. needar : true;
  261. labelprefix_only_inside_procedure : false;
  262. labelprefix : '.L';
  263. comment : '# ';
  264. secnames : ('',
  265. '.text','.data','.bss',
  266. '','','','','','',
  267. '.stab','.stabstr','COMMON')
  268. );
  269. initialization
  270. RegisterAssembler(as_m68k_as_info,TM68kAssembler);
  271. end.
  272. {
  273. $Log$
  274. Revision 1.11 2004-05-01 23:29:01 florian
  275. * continued to fix m68k compiler compilation
  276. Revision 1.10 2004/04/27 15:46:01 florian
  277. * several updates for compilation
  278. Revision 1.9 2004/04/27 15:00:37 florian
  279. - removed offsetfixup reference
  280. Revision 1.8 2004/04/25 21:26:16 florian
  281. * some m68k stuff fixed
  282. Revision 1.7 2003/02/19 22:00:16 daniel
  283. * Code generator converted to new register notation
  284. - Horribily outdated todo.txt removed
  285. Revision 1.6 2003/02/15 22:19:40 carl
  286. * bugfix of emissions of jmp instructions
  287. Revision 1.5 2003/01/08 18:43:57 daniel
  288. * Tregister changed into a record
  289. Revision 1.4 2002/11/30 23:33:02 carl
  290. * merges from Pierre's fixes in m68k fixes branch
  291. Revision 1.3 2002/09/07 15:25:11 peter
  292. * old logs removed and tabs fixed
  293. Revision 1.2 2002/08/13 18:58:54 carl
  294. + m68k problems with cvs fixed?()!
  295. }