agppcgas.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements an asm for the PowerPC
  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 the GNU Assembler writer for the PowerPC
  19. }
  20. unit agppcgas;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. aasmtai,
  25. aggas,
  26. cpubase;
  27. type
  28. PPPCGNUAssembler=^TPPCGNUAssembler;
  29. TPPCGNUAssembler=class(TGNUassembler)
  30. procedure WriteExtraHeader;override;
  31. procedure WriteInstruction(hp : tai);override;
  32. end;
  33. implementation
  34. uses
  35. cutils,globals,verbose,
  36. cgbase,systems,
  37. assemble,
  38. itcpugas,
  39. aasmcpu;
  40. procedure TPPCGNUAssembler.WriteExtraHeader;
  41. var
  42. i : longint;
  43. begin
  44. for i:=0 to 31 do
  45. AsmWriteln(#9'.set'#9'r'+tostr(i)+','+tostr(i));
  46. for i:=0 to 31 do
  47. AsmWriteln(#9'.set'#9'f'+tostr(i)+','+tostr(i));
  48. end;
  49. const
  50. as_ppc_gas_info : tasminfo =
  51. (
  52. id : as_gas;
  53. idtxt : 'AS';
  54. asmbin : 'as';
  55. asmcmd : '-o $OBJ $ASM';
  56. supported_target : system_any;
  57. outputbinary: false;
  58. allowdirect : true;
  59. needar : true;
  60. labelprefix_only_inside_procedure : false;
  61. labelprefix : '.L';
  62. comment : '# ';
  63. secnames : ('',
  64. '.text','.data','.text',
  65. '','','','','','',
  66. '.stab','.stabstr','COMMON')
  67. );
  68. symaddr2str: array[trefsymaddr] of string[3] = ('','@ha','@l');
  69. function getreferencestring(var ref : treference) : string;
  70. var
  71. s : string;
  72. begin
  73. with ref do
  74. begin
  75. inc(offset,offsetfixup);
  76. if ((offset < -32768) or (offset > 32767)) and
  77. (symaddr = refs_full) then
  78. internalerror(19991);
  79. if (symaddr = refs_full) then
  80. s := ''
  81. else if not assigned(symbol) then
  82. s := '('
  83. else
  84. s:='('+symbol.name;
  85. if offset<0 then
  86. s:=s+tostr(offset)
  87. else
  88. if (offset>0) then
  89. begin
  90. if assigned(symbol) then
  91. s:=s+'+'+tostr(offset)
  92. else
  93. s:=s+tostr(offset);
  94. end;
  95. if (symaddr <> refs_full) then
  96. s := s+')'+symaddr2str[symaddr];
  97. if (index=NR_NO) and (base<>NR_NO) then
  98. begin
  99. if offset=0 then
  100. begin
  101. if assigned(symbol) then
  102. s:=s+'+0'
  103. else
  104. s:=s+'0';
  105. end;
  106. s:=s+'('+gas_regname(base)+')';
  107. end
  108. else if (index<>NR_NO) and (base<>NR_NO) then
  109. begin
  110. if (offset=0) then
  111. s:=s+gas_regname(base)+','+gas_regname(index)
  112. else
  113. internalerror(19992);
  114. end;
  115. end;
  116. getreferencestring:=s;
  117. end;
  118. function getopstr_jmp(const o:toper) : string;
  119. var
  120. hs : string;
  121. begin
  122. case o.typ of
  123. top_reg :
  124. getopstr_jmp:=gas_regname(o.reg);
  125. { no top_ref jumping for powerpc }
  126. top_const :
  127. getopstr_jmp:=tostr(o.val);
  128. top_symbol :
  129. begin
  130. hs:=o.sym.name;
  131. if o.symofs>0 then
  132. hs:=hs+'+'+tostr(o.symofs)
  133. else
  134. if o.symofs<0 then
  135. hs:=hs+tostr(o.symofs);
  136. getopstr_jmp:=hs;
  137. end;
  138. top_none:
  139. getopstr_jmp:='';
  140. else
  141. internalerror(2002070603);
  142. end;
  143. end;
  144. function getopstr(const o:toper) : string;
  145. var
  146. hs : string;
  147. begin
  148. case o.typ of
  149. top_reg:
  150. getopstr:=gas_regname(o.reg);
  151. top_const:
  152. getopstr:=tostr(longint(o.val));
  153. top_ref:
  154. getopstr:=getreferencestring(o.ref^);
  155. top_symbol:
  156. begin
  157. hs:=o.sym.name;
  158. if o.symofs>0 then
  159. hs:=hs+'+'+tostr(o.symofs)
  160. else
  161. if o.symofs<0 then
  162. hs:=hs+tostr(o.symofs);
  163. getopstr:=hs;
  164. end;
  165. else
  166. internalerror(2002070604);
  167. end;
  168. end;
  169. function branchmode(o: tasmop): string[4];
  170. var tempstr: string[4];
  171. begin
  172. tempstr := '';
  173. case o of
  174. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  175. A_BCLR,A_BCLRL: tempstr := 'lr';
  176. end;
  177. case o of
  178. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  179. end;
  180. case o of
  181. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  182. end;
  183. branchmode := tempstr;
  184. end;
  185. function cond2str(op: tasmop; c: tasmcond): string;
  186. { note: no checking is performed whether the given combination of }
  187. { conditions is valid }
  188. var tempstr: string;
  189. begin
  190. tempstr:=#9;
  191. case c.simple of
  192. false: cond2str := tempstr+gas_op2str[op]+#9+tostr(c.bo)+','+
  193. tostr(c.bi);
  194. true:
  195. if (op >= A_B) and (op <= A_BCLRL) then
  196. case c.cond of
  197. { unconditional branch }
  198. C_NONE:
  199. cond2str := tempstr+gas_op2str[op];
  200. { bdnzt etc }
  201. else
  202. begin
  203. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  204. branchmode(op)+#9;
  205. case c.cond of
  206. C_LT..C_NU:
  207. cond2str := tempstr+gas_regname(newreg(R_SPECIALREGISTER,c.cr,R_SUBWHOLE));
  208. C_T..C_DZF:
  209. cond2str := tempstr+tostr(c.crbit);
  210. end;
  211. end;
  212. end
  213. { we have a trap instruction }
  214. else
  215. begin
  216. internalerror(2002070601);
  217. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  218. { case tempstr := 'tw';}
  219. end;
  220. end;
  221. case c.dirhint of
  222. DH_Minus:
  223. cond2str:=cond2str+'-';
  224. end;
  225. end;
  226. Procedure TPPCGNUAssembler.WriteInstruction(hp : tai);
  227. var op: TAsmOp;
  228. s: string;
  229. i: byte;
  230. sep: string[3];
  231. begin
  232. op:=taicpu(hp).opcode;
  233. if is_calljmp(op) then
  234. begin
  235. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  236. case op of
  237. A_B,A_BA,A_BL,A_BLA:
  238. s:=#9+gas_op2str[op]+#9;
  239. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  240. s:=#9+gas_op2str[op]
  241. else
  242. s:=cond2str(op,taicpu(hp).condition)+',';
  243. end;
  244. if (taicpu(hp).oper[0]^.typ <> top_none) then
  245. s:=s+getopstr_jmp(taicpu(hp).oper[0]^);
  246. end
  247. else
  248. { process operands }
  249. begin
  250. s:=#9+gas_op2str[op];
  251. if taicpu(hp).ops<>0 then
  252. begin
  253. {
  254. if not is_calljmp(op) then
  255. sep:=','
  256. else
  257. }
  258. sep:=#9;
  259. for i:=0 to taicpu(hp).ops-1 do
  260. begin
  261. // debug code
  262. // writeln(s);
  263. // writeln(taicpu(hp).fileinfo.line);
  264. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  265. sep:=',';
  266. end;
  267. end;
  268. end;
  269. AsmWriteLn(s);
  270. end;
  271. begin
  272. RegisterAssembler(as_ppc_gas_info,TPPCGNUAssembler);
  273. end.
  274. {
  275. $Log$
  276. Revision 1.33 2003-11-12 16:05:40 florian
  277. * assembler readers OOPed
  278. + typed currency constants
  279. + typed 128 bit float constants if the CPU supports it
  280. Revision 1.32 2003/10/25 10:37:26 florian
  281. * fixed compilation of ppc compiler
  282. Revision 1.31 2003/10/01 20:34:49 peter
  283. * procinfo unit contains tprocinfo
  284. * cginfo renamed to cgbase
  285. * moved cgmessage to verbose
  286. * fixed ppc and sparc compiles
  287. Revision 1.30 2003/09/03 19:35:24 peter
  288. * powerpc compiles again
  289. Revision 1.29 2003/08/20 14:28:52 daniel
  290. * Fixed PowerPC compilation
  291. Revision 1.28 2003/08/19 11:53:03 daniel
  292. * Fixed PowerPC compilation
  293. Revision 1.27 2003/08/18 11:58:14 daniel
  294. * Improved -sr on PowerPC ATT asm writer
  295. Revision 1.26 2003/08/17 21:11:00 daniel
  296. * Now -sr works...
  297. Revision 1.25 2003/08/17 20:47:47 daniel
  298. * Notranslation changed into -sr functionality
  299. Revision 1.24 2003/08/17 16:59:20 jonas
  300. * fixed regvars so they work with newra (at least for ppc)
  301. * fixed some volatile register bugs
  302. + -dnotranslation option for -dnewra, which causes the registers not to
  303. be translated from virtual to normal registers. Requires support in
  304. the assembler writer as well, which is only implemented in aggas/
  305. agppcgas currently
  306. Revision 1.23 2003/04/24 22:29:58 florian
  307. * fixed a lot of PowerPC related stuff
  308. Revision 1.22 2003/04/23 12:35:35 florian
  309. * fixed several issues with powerpc
  310. + applied a patch from Jonas for nested function calls (PowerPC only)
  311. * ...
  312. Revision 1.21 2003/03/12 22:43:38 jonas
  313. * more powerpc and generic fixes related to the new register allocator
  314. Revision 1.20 2003/01/08 18:43:57 daniel
  315. * Tregister changed into a record
  316. Revision 1.19 2002/11/07 15:50:23 jonas
  317. * fixed bctr(l) problems
  318. Revision 1.18 2002/09/08 13:03:26 jonas
  319. * several large offset-related fixes
  320. Revision 1.17 2002/09/01 21:04:48 florian
  321. * several powerpc related stuff fixed
  322. Revision 1.16 2002/08/31 19:27:48 jonas
  323. + support top_none for branches
  324. Revision 1.15 2002/08/20 21:40:44 florian
  325. + target macos for ppc added
  326. + frame work for mpw assembler output
  327. Revision 1.14 2002/08/18 22:16:14 florian
  328. + the ppc gas assembler writer adds now registers aliases
  329. to the assembler file
  330. Revision 1.13 2002/08/18 21:36:42 florian
  331. + handling of local variables in direct reader implemented
  332. Revision 1.12 2002/08/18 10:34:30 florian
  333. * more ppc assembling fixes
  334. Revision 1.11 2002/08/17 18:23:53 florian
  335. * some assembler writer bugs fixed
  336. Revision 1.10 2002/08/12 15:08:44 carl
  337. + stab register indexes for powerpc (moved from gdb to cpubase)
  338. + tprocessor enumeration moved to cpuinfo
  339. + linker in target_info is now a class
  340. * many many updates for m68k (will soon start to compile)
  341. - removed some ifdef or correct them for correct cpu
  342. Revision 1.9 2002/07/27 19:57:18 jonas
  343. * some typo corrections in the instruction tables
  344. * renamed the m* registers to v*
  345. Revision 1.8 2002/07/26 21:15:45 florian
  346. * rewrote the system handling
  347. Revision 1.7 2002/07/26 11:19:57 jonas
  348. * fixed range errors
  349. Revision 1.6 2002/07/21 16:56:20 jonas
  350. * fixed bugs with writing out unconditinal jumps
  351. Revision 1.5 2002/07/12 10:10:01 jonas
  352. * changed motorola syntax of references with symbols to GNU syntax
  353. Revision 1.4 2002/07/11 14:41:34 florian
  354. * start of the new generic parameter handling
  355. Revision 1.3 2002/07/11 07:34:55 jonas
  356. * fixed mullw entry in instruction list
  357. Revision 1.2 2002/07/09 19:45:01 jonas
  358. * unarynminus and shlshr node fixed for 32bit and smaller ordinals
  359. * small fixes in the assembler writer
  360. * changed scratch registers, because they were used by the linker (r11
  361. and r12) and by the abi under linux (r31)
  362. Revision 1.1 2002/07/07 09:44:31 florian
  363. * powerpc target fixed, very simple units can be compiled
  364. }