agppcgas.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. if (target_info.system <> system_powerpc_darwin) then
  45. begin
  46. for i:=0 to 31 do
  47. AsmWriteln(#9'.set'#9'r'+tostr(i)+','+tostr(i));
  48. for i:=0 to 31 do
  49. AsmWriteln(#9'.set'#9'f'+tostr(i)+','+tostr(i));
  50. end;
  51. end;
  52. const
  53. as_ppc_gas_info : tasminfo =
  54. (
  55. id : as_gas;
  56. idtxt : 'AS';
  57. asmbin : 'as';
  58. asmcmd : '-o $OBJ $ASM';
  59. supported_target : system_any;
  60. outputbinary: false;
  61. allowdirect : true;
  62. needar : true;
  63. labelprefix_only_inside_procedure : false;
  64. labelprefix : '.L';
  65. comment : '# ';
  66. secnames : ('',
  67. '.text','.data','.text',
  68. '','','','','','',
  69. '.stab','.stabstr','COMMON')
  70. );
  71. as_ppc_gas_darwin_info : tasminfo =
  72. (
  73. id : as_darwin;
  74. idtxt : 'AS-Darwin';
  75. asmbin : 'as';
  76. asmcmd : '-o $OBJ $ASM';
  77. supported_target : system_any;
  78. outputbinary: false;
  79. allowdirect : true;
  80. needar : true;
  81. labelprefix_only_inside_procedure : false;
  82. labelprefix : 'L';
  83. comment : '# ';
  84. secnames : ('',
  85. '.text','.data','.text',
  86. '','','','','','',
  87. '.stab','.stabstr','COMMON')
  88. );
  89. refaddr2str: array[trefaddr] of string[3] = ('','','@ha','@l');
  90. refaddr2str_darwin: array[trefaddr] of string[4] = ('','','ha16','lo16');
  91. function getreferencestring(var ref : treference) : string;
  92. var
  93. s : string;
  94. begin
  95. with ref do
  96. begin
  97. if ((offset < -32768) or (offset > 32767)) and
  98. (refaddr = addr_no) then
  99. internalerror(19991);
  100. if (refaddr = addr_no) then
  101. s := ''
  102. else
  103. begin
  104. if target_info.system = system_powerpc_darwin then
  105. s := refaddr2str_darwin[refaddr]
  106. else
  107. s :='';
  108. s := s+'(';
  109. if assigned(symbol) then
  110. begin
  111. s:=s+symbol.name;
  112. if assigned(relsymbol) then
  113. s:=s+'-'+relsymbol.name;
  114. end;
  115. end;
  116. if offset<0 then
  117. s:=s+tostr(offset)
  118. else
  119. if (offset>0) then
  120. begin
  121. if assigned(symbol) then
  122. s:=s+'+'+tostr(offset)
  123. else
  124. s:=s+tostr(offset);
  125. end;
  126. if (refaddr in [addr_lo,addr_hi]) then
  127. begin
  128. s := s+')';
  129. if (target_info.system <> system_powerpc_darwin) then
  130. s := s+refaddr2str[refaddr];
  131. end;
  132. if (index=NR_NO) and (base<>NR_NO) then
  133. begin
  134. if offset=0 then
  135. begin
  136. if assigned(symbol) then
  137. begin
  138. if target_info.system <> system_powerpc_darwin then
  139. s:=s+'+0'
  140. end
  141. else
  142. s:=s+'0';
  143. end;
  144. s:=s+'('+gas_regname(base)+')';
  145. end
  146. else if (index<>NR_NO) and (base<>NR_NO) then
  147. begin
  148. if (offset=0) then
  149. s:=s+gas_regname(base)+','+gas_regname(index)
  150. else
  151. internalerror(19992);
  152. end;
  153. end;
  154. getreferencestring:=s;
  155. end;
  156. function getopstr_jmp(const o:toper) : string;
  157. var
  158. hs : string;
  159. begin
  160. case o.typ of
  161. top_reg :
  162. getopstr_jmp:=gas_regname(o.reg);
  163. { no top_ref jumping for powerpc }
  164. top_const :
  165. getopstr_jmp:=tostr(o.val);
  166. top_ref :
  167. begin
  168. if o.ref^.refaddr<>addr_full then
  169. internalerror(200402262);
  170. hs:=o.ref^.symbol.name;
  171. if o.ref^.offset>0 then
  172. hs:=hs+'+'+tostr(o.ref^.offset)
  173. else
  174. if o.ref^.offset<0 then
  175. hs:=hs+tostr(o.ref^.offset);
  176. getopstr_jmp:=hs;
  177. end;
  178. top_none:
  179. getopstr_jmp:='';
  180. else
  181. internalerror(2002070603);
  182. end;
  183. end;
  184. function getopstr(const o:toper) : string;
  185. var
  186. hs : string;
  187. begin
  188. case o.typ of
  189. top_reg:
  190. getopstr:=gas_regname(o.reg);
  191. top_const:
  192. getopstr:=tostr(longint(o.val));
  193. top_ref:
  194. if o.ref^.refaddr=addr_full then
  195. begin
  196. hs:=o.ref^.symbol.name;
  197. if o.ref^.offset>0 then
  198. hs:=hs+'+'+tostr(o.ref^.offset)
  199. else
  200. if o.ref^.offset<0 then
  201. hs:=hs+tostr(o.ref^.offset);
  202. getopstr:=hs;
  203. end
  204. else
  205. getopstr:=getreferencestring(o.ref^);
  206. else
  207. internalerror(2002070604);
  208. end;
  209. end;
  210. function branchmode(o: tasmop): string[4];
  211. var tempstr: string[4];
  212. begin
  213. tempstr := '';
  214. case o of
  215. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  216. A_BCLR,A_BCLRL: tempstr := 'lr';
  217. end;
  218. case o of
  219. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  220. end;
  221. case o of
  222. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  223. end;
  224. branchmode := tempstr;
  225. end;
  226. function cond2str(op: tasmop; c: tasmcond): string;
  227. { note: no checking is performed whether the given combination of }
  228. { conditions is valid }
  229. var
  230. tempstr: string;
  231. begin
  232. tempstr:=#9;
  233. case c.simple of
  234. false:
  235. begin
  236. cond2str := tempstr+gas_op2str[op];
  237. case c.dirhint of
  238. DH_None:;
  239. DH_Minus:
  240. cond2str:=cond2str+'-';
  241. DH_Plus:
  242. cond2str:=cond2str+'+';
  243. else
  244. internalerror(2003112901);
  245. end;
  246. cond2str:=cond2str+#9+tostr(c.bo)+','+tostr(c.bi);
  247. end;
  248. true:
  249. if (op >= A_B) and (op <= A_BCLRL) then
  250. case c.cond of
  251. { unconditional branch }
  252. C_NONE:
  253. cond2str := tempstr+gas_op2str[op];
  254. { bdnzt etc }
  255. else
  256. begin
  257. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  258. branchmode(op);
  259. case c.dirhint of
  260. DH_None:
  261. tempstr:=tempstr+#9;
  262. DH_Minus:
  263. tempstr:=tempstr+('-'+#9);
  264. DH_Plus:
  265. tempstr:=tempstr+('+'+#9);
  266. else
  267. internalerror(2003112901);
  268. end;
  269. case c.cond of
  270. C_LT..C_NU:
  271. cond2str := tempstr+gas_regname(newreg(R_SPECIALREGISTER,c.cr,R_SUBWHOLE));
  272. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  273. cond2str := tempstr+tostr(c.crbit);
  274. else
  275. cond2str := tempstr;
  276. end;
  277. end;
  278. end
  279. { we have a trap instruction }
  280. else
  281. begin
  282. internalerror(2002070601);
  283. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  284. { case tempstr := 'tw';}
  285. end;
  286. end;
  287. end;
  288. Procedure TPPCGNUAssembler.WriteInstruction(hp : tai);
  289. var op: TAsmOp;
  290. s: string;
  291. i: byte;
  292. sep: string[3];
  293. begin
  294. op:=taicpu(hp).opcode;
  295. if is_calljmp(op) then
  296. begin
  297. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  298. case op of
  299. A_B,A_BA,A_BL,A_BLA:
  300. s:=#9+gas_op2str[op]+#9;
  301. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  302. s:=#9+gas_op2str[op]
  303. else
  304. begin
  305. s:=cond2str(op,taicpu(hp).condition);
  306. if (s[length(s)] <> #9) and
  307. (taicpu(hp).ops>0) then
  308. s := s + ',';
  309. end;
  310. end;
  311. if (taicpu(hp).ops>0) and (taicpu(hp).oper[0]^.typ<>top_none) then
  312. begin
  313. { first write the current contents of s, because the symbol }
  314. { may be 255 characters }
  315. asmwrite(s);
  316. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  317. end;
  318. end
  319. else
  320. { process operands }
  321. begin
  322. s:=#9+gas_op2str[op];
  323. if taicpu(hp).ops<>0 then
  324. begin
  325. {
  326. if not is_calljmp(op) then
  327. sep:=','
  328. else
  329. }
  330. sep:=#9;
  331. for i:=0 to taicpu(hp).ops-1 do
  332. begin
  333. // debug code
  334. // writeln(s);
  335. // writeln(taicpu(hp).fileinfo.line);
  336. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  337. sep:=',';
  338. end;
  339. end;
  340. end;
  341. AsmWriteLn(s);
  342. end;
  343. begin
  344. RegisterAssembler(as_ppc_gas_info,TPPCGNUAssembler);
  345. RegisterAssembler(as_ppc_gas_darwin_info,TPPCGNUAssembler);
  346. end.
  347. {
  348. $Log$
  349. Revision 1.42 2004-03-02 17:32:12 florian
  350. * make cycle fixed
  351. + pic support for darwin
  352. + support of importing vars from shared libs on darwin implemented
  353. Revision 1.41 2004/02/27 10:21:05 florian
  354. * top_symbol killed
  355. + refaddr to treference added
  356. + refsymbol to treference added
  357. * top_local stuff moved to an extra record to save memory
  358. + aint introduced
  359. * tppufile.get/putint64/aint implemented
  360. Revision 1.40 2004/01/04 21:17:51 jonas
  361. + added log message for last commit
  362. Revision 1.39 2004/01/04 21:12:47 jonas
  363. + as_darwin assembler type (labels start with L)
  364. * never generate register->number mappings for Darwin
  365. * always use real register names for Darwin
  366. Revision 1.38 2003/12/09 20:09:09 jonas
  367. * support writing of symbols with length 255
  368. Revision 1.37 2003/11/29 22:54:32 jonas
  369. * more ppc fixes, hello world works again under linuxppc
  370. Revision 1.36 2003/11/29 18:17:26 jonas
  371. * fixed writing of conditional branches which only depend on the value
  372. of ctr
  373. Revision 1.35 2003/11/29 16:27:19 jonas
  374. * fixed several ppc assembler reader related problems
  375. * local vars in assembler procedures now start at offset 4
  376. * fixed second_int_to_bool (apparently an integer can be in LOC_JUMP??)
  377. Revision 1.34 2003/11/15 19:00:10 florian
  378. * fixed ppc assembler reader
  379. Revision 1.33 2003/11/12 16:05:40 florian
  380. * assembler readers OOPed
  381. + typed currency constants
  382. + typed 128 bit float constants if the CPU supports it
  383. Revision 1.32 2003/10/25 10:37:26 florian
  384. * fixed compilation of ppc compiler
  385. Revision 1.31 2003/10/01 20:34:49 peter
  386. * procinfo unit contains tprocinfo
  387. * cginfo renamed to cgbase
  388. * moved cgmessage to verbose
  389. * fixed ppc and sparc compiles
  390. Revision 1.30 2003/09/03 19:35:24 peter
  391. * powerpc compiles again
  392. Revision 1.29 2003/08/20 14:28:52 daniel
  393. * Fixed PowerPC compilation
  394. Revision 1.28 2003/08/19 11:53:03 daniel
  395. * Fixed PowerPC compilation
  396. Revision 1.27 2003/08/18 11:58:14 daniel
  397. * Improved -sr on PowerPC ATT asm writer
  398. Revision 1.26 2003/08/17 21:11:00 daniel
  399. * Now -sr works...
  400. Revision 1.25 2003/08/17 20:47:47 daniel
  401. * Notranslation changed into -sr functionality
  402. Revision 1.24 2003/08/17 16:59:20 jonas
  403. * fixed regvars so they work with newra (at least for ppc)
  404. * fixed some volatile register bugs
  405. + -dnotranslation option for -dnewra, which causes the registers not to
  406. be translated from virtual to normal registers. Requires support in
  407. the assembler writer as well, which is only implemented in aggas/
  408. agppcgas currently
  409. Revision 1.23 2003/04/24 22:29:58 florian
  410. * fixed a lot of PowerPC related stuff
  411. Revision 1.22 2003/04/23 12:35:35 florian
  412. * fixed several issues with powerpc
  413. + applied a patch from Jonas for nested function calls (PowerPC only)
  414. * ...
  415. Revision 1.21 2003/03/12 22:43:38 jonas
  416. * more powerpc and generic fixes related to the new register allocator
  417. Revision 1.20 2003/01/08 18:43:57 daniel
  418. * Tregister changed into a record
  419. Revision 1.19 2002/11/07 15:50:23 jonas
  420. * fixed bctr(l) problems
  421. Revision 1.18 2002/09/08 13:03:26 jonas
  422. * several large offset-related fixes
  423. Revision 1.17 2002/09/01 21:04:48 florian
  424. * several powerpc related stuff fixed
  425. Revision 1.16 2002/08/31 19:27:48 jonas
  426. + support top_none for branches
  427. Revision 1.15 2002/08/20 21:40:44 florian
  428. + target macos for ppc added
  429. + frame work for mpw assembler output
  430. Revision 1.14 2002/08/18 22:16:14 florian
  431. + the ppc gas assembler writer adds now registers aliases
  432. to the assembler file
  433. Revision 1.13 2002/08/18 21:36:42 florian
  434. + handling of local variables in direct reader implemented
  435. Revision 1.12 2002/08/18 10:34:30 florian
  436. * more ppc assembling fixes
  437. Revision 1.11 2002/08/17 18:23:53 florian
  438. * some assembler writer bugs fixed
  439. Revision 1.10 2002/08/12 15:08:44 carl
  440. + stab register indexes for powerpc (moved from gdb to cpubase)
  441. + tprocessor enumeration moved to cpuinfo
  442. + linker in target_info is now a class
  443. * many many updates for m68k (will soon start to compile)
  444. - removed some ifdef or correct them for correct cpu
  445. Revision 1.9 2002/07/27 19:57:18 jonas
  446. * some typo corrections in the instruction tables
  447. * renamed the m* registers to v*
  448. Revision 1.8 2002/07/26 21:15:45 florian
  449. * rewrote the system handling
  450. Revision 1.7 2002/07/26 11:19:57 jonas
  451. * fixed range errors
  452. Revision 1.6 2002/07/21 16:56:20 jonas
  453. * fixed bugs with writing out unconditinal jumps
  454. Revision 1.5 2002/07/12 10:10:01 jonas
  455. * changed motorola syntax of references with symbols to GNU syntax
  456. Revision 1.4 2002/07/11 14:41:34 florian
  457. * start of the new generic parameter handling
  458. Revision 1.3 2002/07/11 07:34:55 jonas
  459. * fixed mullw entry in instruction list
  460. Revision 1.2 2002/07/09 19:45:01 jonas
  461. * unarynminus and shlshr node fixed for 32bit and smaller ordinals
  462. * small fixes in the assembler writer
  463. * changed scratch registers, because they were used by the linker (r11
  464. and r12) and by the abi under linux (r31)
  465. Revision 1.1 2002/07/07 09:44:31 florian
  466. * powerpc target fixed, very simple units can be compiled
  467. }