agppcgas.pas 14 KB

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