agppcgas.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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
  189. tempstr: string;
  190. begin
  191. tempstr:=#9;
  192. case c.simple of
  193. false:
  194. begin
  195. cond2str := tempstr+gas_op2str[op];
  196. case c.dirhint of
  197. DH_None:;
  198. DH_Minus:
  199. cond2str:=cond2str+'-';
  200. DH_Plus:
  201. cond2str:=cond2str+'+';
  202. else
  203. internalerror(2003112901);
  204. end;
  205. cond2str:=cond2str+#9+tostr(c.bo)+','+tostr(c.bi)+',';
  206. end;
  207. true:
  208. if (op >= A_B) and (op <= A_BCLRL) then
  209. case c.cond of
  210. { unconditional branch }
  211. C_NONE:
  212. cond2str := tempstr+gas_op2str[op];
  213. { bdnzt etc }
  214. else
  215. begin
  216. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  217. branchmode(op);
  218. case c.dirhint of
  219. DH_None:
  220. tempstr:=tempstr+#9;
  221. DH_Minus:
  222. tempstr:=tempstr+('-'+#9);
  223. DH_Plus:
  224. tempstr:=tempstr+('+'+#9);
  225. else
  226. internalerror(2003112901);
  227. end;
  228. case c.cond of
  229. C_LT..C_NU:
  230. cond2str := tempstr+gas_regname(newreg(R_SPECIALREGISTER,c.cr,R_SUBWHOLE));
  231. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  232. cond2str := tempstr+tostr(c.crbit);
  233. else
  234. cond2str := tempstr;
  235. end;
  236. end;
  237. end
  238. { we have a trap instruction }
  239. else
  240. begin
  241. internalerror(2002070601);
  242. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  243. { case tempstr := 'tw';}
  244. end;
  245. end;
  246. end;
  247. Procedure TPPCGNUAssembler.WriteInstruction(hp : tai);
  248. var op: TAsmOp;
  249. s: string;
  250. i: byte;
  251. sep: string[3];
  252. begin
  253. op:=taicpu(hp).opcode;
  254. if is_calljmp(op) then
  255. begin
  256. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  257. case op of
  258. A_B,A_BA,A_BL,A_BLA:
  259. s:=#9+gas_op2str[op]+#9;
  260. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  261. s:=#9+gas_op2str[op]
  262. else
  263. begin
  264. s:=cond2str(op,taicpu(hp).condition);
  265. if (s[length(s)] <> #9) and
  266. (taicpu(hp).ops>0) then
  267. s := s + ',';
  268. end;
  269. end;
  270. if (taicpu(hp).ops>0) and (taicpu(hp).oper[0]^.typ<>top_none) then
  271. s:=s+getopstr_jmp(taicpu(hp).oper[0]^);
  272. end
  273. else
  274. { process operands }
  275. begin
  276. s:=#9+gas_op2str[op];
  277. if taicpu(hp).ops<>0 then
  278. begin
  279. {
  280. if not is_calljmp(op) then
  281. sep:=','
  282. else
  283. }
  284. sep:=#9;
  285. for i:=0 to taicpu(hp).ops-1 do
  286. begin
  287. // debug code
  288. // writeln(s);
  289. // writeln(taicpu(hp).fileinfo.line);
  290. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  291. sep:=',';
  292. end;
  293. end;
  294. end;
  295. AsmWriteLn(s);
  296. end;
  297. begin
  298. RegisterAssembler(as_ppc_gas_info,TPPCGNUAssembler);
  299. end.
  300. {
  301. $Log$
  302. Revision 1.37 2003-11-29 22:54:32 jonas
  303. * more ppc fixes, hello world works again under linuxppc
  304. Revision 1.36 2003/11/29 18:17:26 jonas
  305. * fixed writing of conditional branches which only depend on the value
  306. of ctr
  307. Revision 1.35 2003/11/29 16:27:19 jonas
  308. * fixed several ppc assembler reader related problems
  309. * local vars in assembler procedures now start at offset 4
  310. * fixed second_int_to_bool (apparently an integer can be in LOC_JUMP??)
  311. Revision 1.34 2003/11/15 19:00:10 florian
  312. * fixed ppc assembler reader
  313. Revision 1.33 2003/11/12 16:05:40 florian
  314. * assembler readers OOPed
  315. + typed currency constants
  316. + typed 128 bit float constants if the CPU supports it
  317. Revision 1.32 2003/10/25 10:37:26 florian
  318. * fixed compilation of ppc compiler
  319. Revision 1.31 2003/10/01 20:34:49 peter
  320. * procinfo unit contains tprocinfo
  321. * cginfo renamed to cgbase
  322. * moved cgmessage to verbose
  323. * fixed ppc and sparc compiles
  324. Revision 1.30 2003/09/03 19:35:24 peter
  325. * powerpc compiles again
  326. Revision 1.29 2003/08/20 14:28:52 daniel
  327. * Fixed PowerPC compilation
  328. Revision 1.28 2003/08/19 11:53:03 daniel
  329. * Fixed PowerPC compilation
  330. Revision 1.27 2003/08/18 11:58:14 daniel
  331. * Improved -sr on PowerPC ATT asm writer
  332. Revision 1.26 2003/08/17 21:11:00 daniel
  333. * Now -sr works...
  334. Revision 1.25 2003/08/17 20:47:47 daniel
  335. * Notranslation changed into -sr functionality
  336. Revision 1.24 2003/08/17 16:59:20 jonas
  337. * fixed regvars so they work with newra (at least for ppc)
  338. * fixed some volatile register bugs
  339. + -dnotranslation option for -dnewra, which causes the registers not to
  340. be translated from virtual to normal registers. Requires support in
  341. the assembler writer as well, which is only implemented in aggas/
  342. agppcgas currently
  343. Revision 1.23 2003/04/24 22:29:58 florian
  344. * fixed a lot of PowerPC related stuff
  345. Revision 1.22 2003/04/23 12:35:35 florian
  346. * fixed several issues with powerpc
  347. + applied a patch from Jonas for nested function calls (PowerPC only)
  348. * ...
  349. Revision 1.21 2003/03/12 22:43:38 jonas
  350. * more powerpc and generic fixes related to the new register allocator
  351. Revision 1.20 2003/01/08 18:43:57 daniel
  352. * Tregister changed into a record
  353. Revision 1.19 2002/11/07 15:50:23 jonas
  354. * fixed bctr(l) problems
  355. Revision 1.18 2002/09/08 13:03:26 jonas
  356. * several large offset-related fixes
  357. Revision 1.17 2002/09/01 21:04:48 florian
  358. * several powerpc related stuff fixed
  359. Revision 1.16 2002/08/31 19:27:48 jonas
  360. + support top_none for branches
  361. Revision 1.15 2002/08/20 21:40:44 florian
  362. + target macos for ppc added
  363. + frame work for mpw assembler output
  364. Revision 1.14 2002/08/18 22:16:14 florian
  365. + the ppc gas assembler writer adds now registers aliases
  366. to the assembler file
  367. Revision 1.13 2002/08/18 21:36:42 florian
  368. + handling of local variables in direct reader implemented
  369. Revision 1.12 2002/08/18 10:34:30 florian
  370. * more ppc assembling fixes
  371. Revision 1.11 2002/08/17 18:23:53 florian
  372. * some assembler writer bugs fixed
  373. Revision 1.10 2002/08/12 15:08:44 carl
  374. + stab register indexes for powerpc (moved from gdb to cpubase)
  375. + tprocessor enumeration moved to cpuinfo
  376. + linker in target_info is now a class
  377. * many many updates for m68k (will soon start to compile)
  378. - removed some ifdef or correct them for correct cpu
  379. Revision 1.9 2002/07/27 19:57:18 jonas
  380. * some typo corrections in the instruction tables
  381. * renamed the m* registers to v*
  382. Revision 1.8 2002/07/26 21:15:45 florian
  383. * rewrote the system handling
  384. Revision 1.7 2002/07/26 11:19:57 jonas
  385. * fixed range errors
  386. Revision 1.6 2002/07/21 16:56:20 jonas
  387. * fixed bugs with writing out unconditinal jumps
  388. Revision 1.5 2002/07/12 10:10:01 jonas
  389. * changed motorola syntax of references with symbols to GNU syntax
  390. Revision 1.4 2002/07/11 14:41:34 florian
  391. * start of the new generic parameter handling
  392. Revision 1.3 2002/07/11 07:34:55 jonas
  393. * fixed mullw entry in instruction list
  394. Revision 1.2 2002/07/09 19:45:01 jonas
  395. * unarynminus and shlshr node fixed for 32bit and smaller ordinals
  396. * small fixes in the assembler writer
  397. * changed scratch registers, because they were used by the linker (r11
  398. and r12) and by the abi under linux (r31)
  399. Revision 1.1 2002/07/07 09:44:31 florian
  400. * powerpc target fixed, very simple units can be compiled
  401. }