agppcgas.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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_powerpc_as;
  41. idtxt : 'AS';
  42. asmbin : 'as';
  43. asmcmd : '-o $OBJ $ASM';
  44. supported_target : target_any;
  45. outputbinary: false;
  46. allowdirect : true;
  47. externals : false;
  48. needar : true;
  49. labelprefix_only_inside_procedure : false;
  50. labelprefix : '.L';
  51. comment : '# ';
  52. secnames : ('',
  53. '.text','.data','.text',
  54. '','','','','','',
  55. '.stab','.stabstr','COMMON')
  56. );
  57. op2str : array[tasmop] of string[14] = ('<none>',
  58. 'add','add.','addo','addo.','addc','addc.','addco','addco.',
  59. 'adde','adde.','addeo','addeo.','addi','addic','addic.','addis',
  60. 'addme','addme.','addmeo','addmeo.','addze','addze.','addzeo',
  61. 'addzeo.','and','and.','andc','andc.','andi.','andis.','b',
  62. 'ba','bl','bla','bc','bca','bcl','bcla','bcctr','bcctrl','bclr',
  63. 'bclrl','cmp','cmpi','cmpl','cmpli','cntlzw','cntlzw.','crand',
  64. 'crandc','creqv','crnand','crnor','cror','crorc','crxor','dcba',
  65. 'dcbf','dcbi','dcbst','dcbt','divw','divw.','divwo','divwo.',
  66. 'divwu','divwu.','divwuo','divwuo.','eciwx','ecowx','eieio','eqv',
  67. 'eqv.','extsb','extsb.','extsh','extsh.','fabs','fabs.','fadd',
  68. 'fadd.','fadds','fadds.','fcompo','fcmpu','fctiw','fctw.','fctwz',
  69. 'fctwz.','fdiv','fdiv.','fdivs','fdivs.','fmadd','fmadd.','fmadds',
  70. 'fmadds.','fmr','fmsub','fmsub.','fmsubs','fmsubs.','fmul','fmul.',
  71. 'fmuls','fmuls.','fnabs','fnabs.','fneg','fneg.','fnmadd',
  72. 'fnmadd.','fnmadds','fnmadds.','fnmsub','fnmsub.','fnmsubs',
  73. 'fnmsubs.','fres','fres.','frsp','frsp.','frsqrte','frsqrte.',
  74. 'fsel','fsel.','fsqrt','fsqrt.','fsqrts','fsqrts.','fsub','fsub.',
  75. 'fsubs','fsubs.','icbi','isync','lbz','lbzu','lbzux','lbzx',
  76. 'lfd','lfdu','lfdux','lfdx','lfs','lfsu','lfsux','lfsx','lha',
  77. 'lhau','lhaux','lhax','hbrx','lhz','lhzu','lhzux','lhzx','lmw',
  78. 'lswi','lswx','lwarx','lwbrx','lwz','lwzu','lwzux','lwzx','mcrf',
  79. 'mcrfs','mcrxr','lcrxe','mfcr','mffs','maffs.','mfmsr','mfspr','mfsr',
  80. 'mfsrin','mftb','mtfcrf','mtfd0','mtfsb1','mtfsf','mtfsf.',
  81. 'mtfsfi','mtfsfi.','mtmsr','mtspr','mtsr','mtsrin','mulhw',
  82. 'mulhw.','mulhwu','mulhwu.','mulli','mullw','mullw.','mullwo',
  83. 'mullwo.','nand','nand.','neg','neg.','nego','nego.','nor','nor.',
  84. 'or','or.','orc','orc.','ori','oris', 'rfi', 'rlwimi', 'rlwimi.',
  85. 'rlwinm', 'tlwinm.','rlwnm','sc','slw', 'slw.', 'sraw', 'sraw.',
  86. 'srawi', 'srawi.','srw', 'srw.', 'stb', 'stbu', 'stbux','stbx','stfd',
  87. 'stfdu', 'stfdux', 'stfdx', 'stfiwx', 'stfs', 'stfsu', 'stfsux', 'stfsx',
  88. 'sth', 'sthbrx', 'sthu', 'sthux', 'sthx', 'stmw', 'stswi', 'stswx', 'stw',
  89. 'stwbrx', 'stwx.', 'stwu', 'stwux', 'stwx', 'subf', 'subf.', 'subfo',
  90. 'subfo.', 'subfc', 'subc.', 'subfco', 'subfco.', 'subfe', 'subfe.',
  91. 'subfeo', 'subfeo.', 'subfic', 'subfme', 'subfme.', 'subfmeo', 'subfmeo.',
  92. 'subfze', 'subfze.', 'subfzeo', 'subfzeo.', 'sync', 'tlbia', 'tlbie',
  93. 'tlbsync', 'tw', 'twi', 'xor', 'xor.', 'xori', 'xoris',
  94. { some simplified mnemonics }
  95. 'subi', 'subis', 'subic', 'subic.', 'sub', 'sub.', 'subo', 'subo.',
  96. 'subc', 'subc.', 'subco', '.subco.', 'cmpwi', 'cmpw', 'cmplwi', 'cmplw',
  97. 'extlwi', 'extlwi.', 'extrwi', 'extrwi.', 'inslwi', 'inslwi.', 'insrwi',
  98. 'insrwi.', 'rotlwi', 'rotlwi.', 'rotlw', 'rotlw.', 'slwi', 'slwi.',
  99. 'srwi', 'srwi.', 'clrlwi', 'clrlwi.', 'clrrwi', 'clrrwi.', 'clrslwi',
  100. 'clrslwi.', 'blr', 'bctr', 'blrl', 'bctrl', 'crset', 'crclr', 'crmove',
  101. 'crnot', 'mt', 'mf','nop', 'li', 'lis', 'la', 'mr','mr.','not', 'mtcr');
  102. reg2str : reg2strtable = ('',
  103. '0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
  104. '17','18','19','20','21','22','23','24','25','26','27','28','29','30','31',
  105. 'F0','F1','F2','F3','F4','F5','F6','F7', 'F8','F9','F10','F11','F12',
  106. 'F13','F14','F15','F16','F17', 'F18','F19','F20','F21','F22', 'F23','F24',
  107. 'F25','F26','F27','F28','F29','F30','F31',
  108. 'M0','M1','M2','M3','M4','M5','M6','M7','M8','M9','M10','M11','M12',
  109. 'M13','M14','M15','M16','M17','M18','M19','M20','M21','M22', 'M23','M24',
  110. 'M25','M26','M27','M28','M29','M30','M31',
  111. 'CR','CR0','CR1','CR2','CR3','CR4','CR5','CR6','CR7',
  112. 'XER','LR','CTR','FPSCR'
  113. );
  114. symaddr2str: array[trefsymaddr] of string[4] = ('','ha16','lo16');
  115. function getreferencestring(var ref : treference) : string;
  116. var
  117. s : string;
  118. begin
  119. with ref do
  120. begin
  121. inc(offset,offsetfixup);
  122. if (offset < -32768) or (offset > 32767) then
  123. {$ifndef testing}
  124. internalerror(19991);
  125. {$else testing}
  126. begin
  127. writeln('internalerror 19991');
  128. halt(1);
  129. end;
  130. {$endif testing}
  131. if not assigned(symbol) then
  132. s:=''
  133. else
  134. s:=symaddr2str[symaddr]+'('+symbol.name + ')';
  135. if offset<0 then
  136. s:=s+tostr(offset)
  137. else
  138. if (offset>0) then
  139. begin
  140. if assigned(symbol) then
  141. s:=s+'+'+tostr(offset)
  142. else
  143. s:=s+tostr(offset);
  144. end;
  145. if (index=R_NO) and (base<>R_NO) then
  146. s:=s+'('+reg2str[base]+')'
  147. else if (index<>R_NO) and (base<>R_NO) and (offset = 0) then
  148. s:=s+reg2str[base]+','+reg2str[index]
  149. else if ((index<>R_NO) or (base<>R_NO)) then
  150. {$ifndef testing}
  151. internalerror(19992);
  152. {$else testing}
  153. begin
  154. writeln('internalerror 19992');
  155. halt(1);
  156. end;
  157. {$endif testing}
  158. end;
  159. getreferencestring:=s;
  160. end;
  161. function getopstr_jmp(const o:toper) : string;
  162. var
  163. hs : string;
  164. begin
  165. case o.typ of
  166. top_reg :
  167. getopstr_jmp:=reg2str[o.reg];
  168. { no top_ref jumping for powerpc }
  169. top_const :
  170. getopstr_jmp:=tostr(o.val);
  171. top_symbol :
  172. begin
  173. hs:=o.sym.name;
  174. if o.symofs>0 then
  175. hs:=hs+'+'+tostr(o.symofs)
  176. else
  177. if o.symofs<0 then
  178. hs:=hs+tostr(o.symofs);
  179. getopstr_jmp:=hs;
  180. end;
  181. else
  182. {$ifndef testing}
  183. internalerror(2002070603);
  184. {$else testing}
  185. begin
  186. writeln('internalerror 10001');
  187. halt(1);
  188. end;
  189. {$endif testing}
  190. end;
  191. end;
  192. function getopstr(const o:toper) : string;
  193. var
  194. hs : string;
  195. begin
  196. case o.typ of
  197. top_reg:
  198. getopstr:=reg2str[o.reg];
  199. { no top_ref jumping for powerpc }
  200. top_const:
  201. getopstr:=tostr(o.val);
  202. top_ref:
  203. getopstr:=getreferencestring(o.ref^);
  204. top_symbol:
  205. begin
  206. hs:=o.sym.name;
  207. if o.symofs>0 then
  208. hs:=hs+'+'+tostr(o.symofs)
  209. else
  210. if o.symofs<0 then
  211. hs:=hs+tostr(o.symofs);
  212. getopstr:=hs;
  213. end;
  214. else
  215. {$ifndef testing}
  216. internalerror(2002070604);
  217. {$else testing}
  218. begin
  219. writeln('internalerror 10001');
  220. halt(1);
  221. end;
  222. {$endif testing}
  223. end;
  224. end;
  225. function branchmode(o: tasmop): string[4];
  226. var tempstr: string[4];
  227. begin
  228. tempstr := '';
  229. case o of
  230. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  231. A_BCLR,A_BCLRL: tempstr := 'lr';
  232. end;
  233. case o of
  234. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  235. end;
  236. case o of
  237. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  238. end;
  239. branchmode := tempstr;
  240. end;
  241. function cond2str(op: tasmop; c: tasmcond): string;
  242. { note: no checking is performed whether the given combination of }
  243. { conditions is valid }
  244. var tempstr: string;
  245. begin
  246. tempstr:=#9;
  247. case c.simple of
  248. false: cond2str := tempstr+op2str[op]+#9+tostr(c.bo)+','+
  249. tostr(c.bi);
  250. true:
  251. if (op >= A_B) and (op <= A_BCLRL) then
  252. case c.cond of
  253. { unconditional branch }
  254. C_NONE:
  255. cond2str := tempstr+op2str[op];
  256. { bdnzt etc }
  257. else
  258. begin
  259. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  260. branchmode(op)+#9;
  261. case c.cond of
  262. C_LT..C_NU:
  263. cond2str := tempstr+reg2str[c.cr];
  264. C_T..C_DZF:
  265. cond2str := tempstr+tostr(c.crbit);
  266. end;
  267. end;
  268. end
  269. { we have a trap instruction }
  270. else
  271. begin
  272. internalerror(2002070601);
  273. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  274. { case tempstr := 'tw';}
  275. end;
  276. end;
  277. end;
  278. Procedure TPPCGNUAssembler.WriteInstruction(hp : tai);
  279. var op: TAsmOp;
  280. s: string;
  281. i: byte;
  282. sep: string[3];
  283. begin
  284. op:=taicpu(hp).opcode;
  285. if is_calljmp(op) then
  286. begin
  287. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  288. if op <> A_B then
  289. s:=cond2str(op,taicpu(hp).condition)+','
  290. else
  291. s:=#9'b'#9;
  292. s := s+getopstr_jmp(taicpu(hp).oper[0]);
  293. end
  294. else
  295. { process operands }
  296. begin
  297. s:=#9+op2str[op];
  298. if taicpu(hp).ops<>0 then
  299. begin
  300. {
  301. if not is_calljmp(op) then
  302. sep:=','
  303. else
  304. }
  305. sep:=#9;
  306. for i:=0 to taicpu(hp).ops-1 do
  307. begin
  308. s:=s+sep+getopstr(taicpu(hp).oper[i]);
  309. sep:=',';
  310. end;
  311. end;
  312. end;
  313. AsmWriteLn(s);
  314. end;
  315. begin
  316. RegisterAssembler(as_ppc_gas_info,TPPCGNUAssembler);
  317. end.
  318. {
  319. $Log$
  320. Revision 1.6 2002-07-21 16:56:20 jonas
  321. * fixed bugs with writing out unconditinal jumps
  322. Revision 1.5 2002/07/12 10:10:01 jonas
  323. * changed motorola syntax of references with symbols to GNU syntax
  324. Revision 1.4 2002/07/11 14:41:34 florian
  325. * start of the new generic parameter handling
  326. Revision 1.3 2002/07/11 07:34:55 jonas
  327. * fixed mullw entry in instruction list
  328. Revision 1.2 2002/07/09 19:45:01 jonas
  329. * unarynminus and shlshr node fixed for 32bit and smaller ordinals
  330. * small fixes in the assembler writer
  331. * changed scratch registers, because they were used by the linker (r11
  332. and r12) and by the abi under linux (r31)
  333. Revision 1.1 2002/07/07 09:44:31 florian
  334. * powerpc target fixed, very simple units can be compiled
  335. }