agppcgas.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. type
  27. PPPCGNUAssembler=^TPPCGNUAssembler;
  28. TPPCGNUAssembler=class(TGNUassembler)
  29. procedure WriteInstruction(hp : tai);override;
  30. end;
  31. implementation
  32. uses
  33. cutils,globals,verbose,
  34. systems,
  35. assemble,
  36. aasmcpu,cpubase;
  37. const
  38. as_ppc_gas_info : tasminfo =
  39. (
  40. id : as_gas;
  41. idtxt : 'AS';
  42. asmbin : 'as';
  43. asmcmd : '-o $OBJ $ASM';
  44. supported_target : system_any;
  45. outputbinary: false;
  46. allowdirect : true;
  47. needar : true;
  48. labelprefix_only_inside_procedure : false;
  49. labelprefix : '.L';
  50. comment : '# ';
  51. secnames : ('',
  52. '.text','.data','.text',
  53. '','','','','','',
  54. '.stab','.stabstr','COMMON')
  55. );
  56. op2str : array[tasmop] of string[14] = ('<none>',
  57. 'add','add.','addo','addo.','addc','addc.','addco','addco.',
  58. 'adde','adde.','addeo','addeo.','addi','addic','addic.','addis',
  59. 'addme','addme.','addmeo','addmeo.','addze','addze.','addzeo',
  60. 'addzeo.','and','and.','andc','andc.','andi.','andis.','b',
  61. 'ba','bl','bla','bc','bca','bcl','bcla','bcctr','bcctrl','bclr',
  62. 'bclrl','cmp','cmpi','cmpl','cmpli','cntlzw','cntlzw.','crand',
  63. 'crandc','creqv','crnand','crnor','cror','crorc','crxor','dcba',
  64. 'dcbf','dcbi','dcbst','dcbt','divw','divw.','divwo','divwo.',
  65. 'divwu','divwu.','divwuo','divwuo.','eciwx','ecowx','eieio','eqv',
  66. 'eqv.','extsb','extsb.','extsh','extsh.','fabs','fabs.','fadd',
  67. 'fadd.','fadds','fadds.','fcmpo','fcmpu','fctiw','fctw.','fctwz',
  68. 'fctwz.','fdiv','fdiv.','fdivs','fdivs.','fmadd','fmadd.','fmadds',
  69. 'fmadds.','fmr','fmsub','fmsub.','fmsubs','fmsubs.','fmul','fmul.',
  70. 'fmuls','fmuls.','fnabs','fnabs.','fneg','fneg.','fnmadd',
  71. 'fnmadd.','fnmadds','fnmadds.','fnmsub','fnmsub.','fnmsubs',
  72. 'fnmsubs.','fres','fres.','frsp','frsp.','frsqrte','frsqrte.',
  73. 'fsel','fsel.','fsqrt','fsqrt.','fsqrts','fsqrts.','fsub','fsub.',
  74. 'fsubs','fsubs.','icbi','isync','lbz','lbzu','lbzux','lbzx',
  75. 'lfd','lfdu','lfdux','lfdx','lfs','lfsu','lfsux','lfsx','lha',
  76. 'lhau','lhaux','lhax','hbrx','lhz','lhzu','lhzux','lhzx','lmw',
  77. 'lswi','lswx','lwarx','lwbrx','lwz','lwzu','lwzux','lwzx','mcrf',
  78. 'mcrfs','mcrxr','lcrxe','mfcr','mffs','maffs.','mfmsr','mfspr','mfsr',
  79. 'mfsrin','mftb','mtfcrf','mtfd0','mtfsb1','mtfsf','mtfsf.',
  80. 'mtfsfi','mtfsfi.','mtmsr','mtspr','mtsr','mtsrin','mulhw',
  81. 'mulhw.','mulhwu','mulhwu.','mulli','mullw','mullw.','mullwo',
  82. 'mullwo.','nand','nand.','neg','neg.','nego','nego.','nor','nor.',
  83. 'or','or.','orc','orc.','ori','oris', 'rfi', 'rlwimi', 'rlwimi.',
  84. 'rlwinm', 'rlwinm.','rlwnm','sc','slw', 'slw.', 'sraw', 'sraw.',
  85. 'srawi', 'srawi.','srw', 'srw.', 'stb', 'stbu', 'stbux','stbx','stfd',
  86. 'stfdu', 'stfdux', 'stfdx', 'stfiwx', 'stfs', 'stfsu', 'stfsux', 'stfsx',
  87. 'sth', 'sthbrx', 'sthu', 'sthux', 'sthx', 'stmw', 'stswi', 'stswx', 'stw',
  88. 'stwbrx', 'stwx.', 'stwu', 'stwux', 'stwx', 'subf', 'subf.', 'subfo',
  89. 'subfo.', 'subfc', 'subc.', 'subfco', 'subfco.', 'subfe', 'subfe.',
  90. 'subfeo', 'subfeo.', 'subfic', 'subfme', 'subfme.', 'subfmeo', 'subfmeo.',
  91. 'subfze', 'subfze.', 'subfzeo', 'subfzeo.', 'sync', 'tlbia', 'tlbie',
  92. 'tlbsync', 'tw', 'twi', 'xor', 'xor.', 'xori', 'xoris',
  93. { some simplified mnemonics }
  94. 'subi', 'subis', 'subic', 'subic.', 'sub', 'sub.', 'subo', 'subo.',
  95. 'subc', 'subc.', 'subco', 'subco.', 'cmpwi', 'cmpw', 'cmplwi', 'cmplw',
  96. 'extlwi', 'extlwi.', 'extrwi', 'extrwi.', 'inslwi', 'inslwi.', 'insrwi',
  97. 'insrwi.', 'rotlwi', 'rotlwi.', 'rotlw', 'rotlw.', 'slwi', 'slwi.',
  98. 'srwi', 'srwi.', 'clrlwi', 'clrlwi.', 'clrrwi', 'clrrwi.', 'clrslwi',
  99. 'clrslwi.', 'blr', 'bctr', 'blrl', 'bctrl', 'crset', 'crclr', 'crmove',
  100. 'crnot', 'mt', 'mf','nop', 'li', 'lis', 'la', 'mr','mr.','not', 'mtcr');
  101. reg2str : reg2strtable = ('',
  102. 'r0','r1','r2','r3','r4','r5','r6','r7','r8','r9','r10','r11','r12','r13','r14','r15','r16',
  103. 'r17','r18','r19','r20','r21','r22','r23','r24','r25','r26','r27','r28','r29','r30','r31',
  104. 'f0','f1','f2','f3','f4','f5','f6','f7', 'f8','f9','f10','f11','f12',
  105. 'f13','f14','f15','f16','f17', 'f18','f19','f20','f21','f22', 'f23','f24',
  106. 'f25','f26','f27','f28','f29','f30','f31',
  107. 'v0','v1','v2','v3','v4','v5','v6','v7','v8','v9','v10','v11','v12',
  108. 'v13','v14','v15','v16','v17','v18','v19','v20','v21','v22', 'v23','v24',
  109. 'v25','v26','v27','v28','v29','v30','v31',
  110. 'cR','cr0','cr1','cr2','cr3','cr4','cr5','cr6','cr7',
  111. 'xer','lr','ctr','fpscr'
  112. );
  113. symaddr2str: array[trefsymaddr] of string[4] = ('','ha16','lo16');
  114. function getreferencestring(var ref : treference) : string;
  115. var
  116. s : string;
  117. begin
  118. with ref do
  119. begin
  120. inc(offset,offsetfixup);
  121. if (offset < -32768) or (offset > 32767) then
  122. {$ifndef testing}
  123. internalerror(19991);
  124. {$else testing}
  125. begin
  126. writeln('internalerror 19991');
  127. halt(1);
  128. end;
  129. {$endif testing}
  130. if not assigned(symbol) then
  131. s:=''
  132. else
  133. s:=symaddr2str[symaddr]+'('+symbol.name + ')';
  134. if offset<0 then
  135. s:=s+tostr(offset)
  136. else
  137. if (offset>0) then
  138. begin
  139. if assigned(symbol) then
  140. s:=s+'+'+tostr(offset)
  141. else
  142. s:=s+tostr(offset);
  143. end;
  144. if (index=R_NO) and (base<>R_NO) then
  145. s:=s+'('+reg2str[base]+')'
  146. else if (index<>R_NO) and (base<>R_NO) and (offset = 0) then
  147. s:=s+reg2str[base]+','+reg2str[index]
  148. else if ((index<>R_NO) or (base<>R_NO)) then
  149. {$ifndef testing}
  150. internalerror(19992);
  151. {$else testing}
  152. begin
  153. writeln('internalerror 19992');
  154. halt(1);
  155. end;
  156. {$endif testing}
  157. end;
  158. getreferencestring:=s;
  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:=reg2str[o.reg];
  167. { no top_ref jumping for powerpc }
  168. top_const :
  169. getopstr_jmp:=tostr(o.val);
  170. top_symbol :
  171. begin
  172. hs:=o.sym.name;
  173. if o.symofs>0 then
  174. hs:=hs+'+'+tostr(o.symofs)
  175. else
  176. if o.symofs<0 then
  177. hs:=hs+tostr(o.symofs);
  178. getopstr_jmp:=hs;
  179. end;
  180. else
  181. {$ifndef testing}
  182. internalerror(2002070603);
  183. {$else testing}
  184. begin
  185. writeln('internalerror 10001');
  186. halt(1);
  187. end;
  188. {$endif testing}
  189. end;
  190. end;
  191. function getopstr(const o:toper) : string;
  192. var
  193. hs : string;
  194. begin
  195. case o.typ of
  196. top_reg:
  197. getopstr:=reg2str[o.reg];
  198. { no top_ref jumping for powerpc }
  199. top_const:
  200. getopstr:=tostr(longint(o.val));
  201. top_ref:
  202. getopstr:=getreferencestring(o.ref^);
  203. top_symbol:
  204. begin
  205. hs:=o.sym.name;
  206. if o.symofs>0 then
  207. hs:=hs+'+'+tostr(o.symofs)
  208. else
  209. if o.symofs<0 then
  210. hs:=hs+tostr(o.symofs);
  211. getopstr:=hs;
  212. end;
  213. else
  214. {$ifndef testing}
  215. internalerror(2002070604);
  216. {$else testing}
  217. begin
  218. writeln('internalerror 10001');
  219. halt(1);
  220. end;
  221. {$endif testing}
  222. end;
  223. end;
  224. function branchmode(o: tasmop): string[4];
  225. var tempstr: string[4];
  226. begin
  227. tempstr := '';
  228. case o of
  229. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  230. A_BCLR,A_BCLRL: tempstr := 'lr';
  231. end;
  232. case o of
  233. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  234. end;
  235. case o of
  236. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  237. end;
  238. branchmode := tempstr;
  239. end;
  240. function cond2str(op: tasmop; c: tasmcond): string;
  241. { note: no checking is performed whether the given combination of }
  242. { conditions is valid }
  243. var tempstr: string;
  244. begin
  245. tempstr:=#9;
  246. case c.simple of
  247. false: cond2str := tempstr+op2str[op]+#9+tostr(c.bo)+','+
  248. tostr(c.bi);
  249. true:
  250. if (op >= A_B) and (op <= A_BCLRL) then
  251. case c.cond of
  252. { unconditional branch }
  253. C_NONE:
  254. cond2str := tempstr+op2str[op];
  255. { bdnzt etc }
  256. else
  257. begin
  258. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  259. branchmode(op)+#9;
  260. case c.cond of
  261. C_LT..C_NU:
  262. cond2str := tempstr+reg2str[c.cr];
  263. C_T..C_DZF:
  264. cond2str := tempstr+tostr(c.crbit);
  265. end;
  266. end;
  267. end
  268. { we have a trap instruction }
  269. else
  270. begin
  271. internalerror(2002070601);
  272. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  273. { case tempstr := 'tw';}
  274. end;
  275. end;
  276. end;
  277. Procedure TPPCGNUAssembler.WriteInstruction(hp : tai);
  278. var op: TAsmOp;
  279. s: string;
  280. i: byte;
  281. sep: string[3];
  282. begin
  283. op:=taicpu(hp).opcode;
  284. if is_calljmp(op) then
  285. begin
  286. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  287. if op <> A_B then
  288. s:=cond2str(op,taicpu(hp).condition)+','
  289. else
  290. s:=#9'b'#9;
  291. s := s+getopstr_jmp(taicpu(hp).oper[0]);
  292. end
  293. else
  294. { process operands }
  295. begin
  296. s:=#9+op2str[op];
  297. if taicpu(hp).ops<>0 then
  298. begin
  299. {
  300. if not is_calljmp(op) then
  301. sep:=','
  302. else
  303. }
  304. sep:=#9;
  305. for i:=0 to taicpu(hp).ops-1 do
  306. begin
  307. s:=s+sep+getopstr(taicpu(hp).oper[i]);
  308. sep:=',';
  309. end;
  310. end;
  311. end;
  312. AsmWriteLn(s);
  313. end;
  314. begin
  315. RegisterAssembler(as_ppc_gas_info,TPPCGNUAssembler);
  316. end.
  317. {
  318. $Log$
  319. Revision 1.10 2002-08-12 15:08:44 carl
  320. + stab register indexes for powerpc (moved from gdb to cpubase)
  321. + tprocessor enumeration moved to cpuinfo
  322. + linker in target_info is now a class
  323. * many many updates for m68k (will soon start to compile)
  324. - removed some ifdef or correct them for correct cpu
  325. Revision 1.9 2002/07/27 19:57:18 jonas
  326. * some typo corrections in the instruction tables
  327. * renamed the m* registers to v*
  328. Revision 1.8 2002/07/26 21:15:45 florian
  329. * rewrote the system handling
  330. Revision 1.7 2002/07/26 11:19:57 jonas
  331. * fixed range errors
  332. Revision 1.6 2002/07/21 16:56:20 jonas
  333. * fixed bugs with writing out unconditinal jumps
  334. Revision 1.5 2002/07/12 10:10:01 jonas
  335. * changed motorola syntax of references with symbols to GNU syntax
  336. Revision 1.4 2002/07/11 14:41:34 florian
  337. * start of the new generic parameter handling
  338. Revision 1.3 2002/07/11 07:34:55 jonas
  339. * fixed mullw entry in instruction list
  340. Revision 1.2 2002/07/09 19:45:01 jonas
  341. * unarynminus and shlshr node fixed for 32bit and smaller ordinals
  342. * small fixes in the assembler writer
  343. * changed scratch registers, because they were used by the linker (r11
  344. and r12) and by the abi under linux (r31)
  345. Revision 1.1 2002/07/07 09:44:31 florian
  346. * powerpc target fixed, very simple units can be compiled
  347. }