agppcgas.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. interface
  22. uses
  23. aasmtai,
  24. aggas;
  25. type
  26. PPPCGNUAssembler=^TPPCGNUAssembler;
  27. TPPCGNUAssembler=class(TGNUassembler)
  28. procedure WriteInstruction(hp : tai);override;
  29. end;
  30. implementation
  31. uses
  32. cutils,globals,verbose,
  33. systems,
  34. assemble,
  35. aasmcpu,cpubase;
  36. const
  37. as_ppc_gas_info : tasminfo =
  38. (
  39. id : as_powerpc_as;
  40. idtxt : 'AS';
  41. asmbin : 'as';
  42. asmcmd : '-o $OBJ $ASM';
  43. supported_target : target_any;
  44. outputbinary: false;
  45. allowdirect : true;
  46. externals : false;
  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.','fcompo','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','mullh','mullw.','mullwo',
  82. 'mullwo.','nand','nand.','neg','neg.','nego','nego.','nor','nor.',
  83. 'or','or.','orc','orc.','ori','oris', 'rfi', 'rlwimi', 'rlwimi.',
  84. 'rlwinm', 'tlwinm.','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. '0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
  103. '17','18','19','20','21','22','23','24','25','26','27','28','29','30','31',
  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. 'M0','M1','M2','M3','M4','M5','M6','M7','M8','M9','M10','M11','M12',
  108. 'M13','M14','M15','M16','M17','M18','M19','M20','M21','M22', 'M23','M24',
  109. 'M25','M26','M27','M28','M29','M30','M31',
  110. 'CR','CR0','CR1','CR2','CR3','CR4','CR5','CR6','CR7',
  111. 'XER','LR','CTR','FPSCR'
  112. );
  113. function getreferencestring(var ref : treference) : string;
  114. var
  115. s : string;
  116. begin
  117. with ref do
  118. begin
  119. inc(offset,offsetfixup);
  120. if (offset < -32768) or (offset > 32767) then
  121. {$ifndef testing}
  122. internalerror(19991);
  123. {$else testing}
  124. begin
  125. writeln('internalerror 19991');
  126. halt(1);
  127. end;
  128. {$endif testing}
  129. s:='';
  130. if assigned(symbol) then
  131. s:=s+symbol.name + symaddr2str[symaddr];
  132. if offset<0 then
  133. s:=s+tostr(offset)
  134. else
  135. if (offset>0) then
  136. begin
  137. if assigned(symbol) then
  138. s:=s+'+'+tostr(offset)
  139. else
  140. s:=s+tostr(offset);
  141. end;
  142. if (index=R_NO) and (base<>R_NO) then
  143. s:=s+'('+reg2str[base]+')'
  144. else if (index<>R_NO) and (base<>R_NO) and (offset = 0) then
  145. s:=s+reg2str[base]+','+reg2str[index]
  146. else if ((index<>R_NO) or (base<>R_NO)) then
  147. {$ifndef testing}
  148. internalerror(19992);
  149. {$else testing}
  150. begin
  151. writeln('internalerror 19992');
  152. halt(1);
  153. end;
  154. {$endif testing}
  155. end;
  156. getreferencestring:=s;
  157. end;
  158. function getopstr_jmp(const o:toper) : string;
  159. var
  160. hs : string;
  161. begin
  162. case o.typ of
  163. top_reg :
  164. getopstr_jmp:=reg2str[o.reg];
  165. { no top_ref jumping for powerpc }
  166. top_const :
  167. getopstr_jmp:=tostr(o.val);
  168. top_symbol :
  169. begin
  170. hs:=o.sym.name;
  171. if o.symofs>0 then
  172. hs:=hs+'+'+tostr(o.symofs)
  173. else
  174. if o.symofs<0 then
  175. hs:=hs+tostr(o.symofs);
  176. getopstr_jmp:=hs;
  177. end;
  178. else
  179. {$ifndef testing}
  180. internalerror(2002070603);
  181. {$else testing}
  182. begin
  183. writeln('internalerror 10001');
  184. halt(1);
  185. end;
  186. {$endif testing}
  187. end;
  188. end;
  189. function getopstr(const o:toper) : string;
  190. var
  191. hs : string;
  192. begin
  193. case o.typ of
  194. top_reg:
  195. getopstr:=reg2str[o.reg];
  196. { no top_ref jumping for powerpc }
  197. top_const:
  198. getopstr:=tostr(o.val);
  199. top_ref:
  200. getopstr:=getreferencestring(o.ref^);
  201. top_symbol:
  202. begin
  203. hs:=o.sym.name;
  204. if o.symofs>0 then
  205. hs:=hs+'+'+tostr(o.symofs)
  206. else
  207. if o.symofs<0 then
  208. hs:=hs+tostr(o.symofs);
  209. getopstr:=hs;
  210. end;
  211. else
  212. {$ifndef testing}
  213. internalerror(2002070604);
  214. {$else testing}
  215. begin
  216. writeln('internalerror 10001');
  217. halt(1);
  218. end;
  219. {$endif testing}
  220. end;
  221. end;
  222. function branchmode(o: tasmop): string[4];
  223. var tempstr: string[4];
  224. begin
  225. tempstr := '';
  226. case o of
  227. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  228. A_BCLR,A_BCLRL: tempstr := 'lr';
  229. end;
  230. case o of
  231. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  232. end;
  233. case o of
  234. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  235. end;
  236. branchmode := tempstr;
  237. end;
  238. function cond2str(op: tasmop; c: tasmcond): string;
  239. { note: no checking is performed whether the given combination of }
  240. { conditions is valid }
  241. var tempstr: string;
  242. begin
  243. tempstr:=#9;
  244. case c.simple of
  245. false: cond2str := tempstr+op2str[op]+#9+tostr(c.bo)+','+
  246. tostr(c.bi);
  247. true:
  248. if (op >= A_B) and (op <= A_BCLRL) then
  249. case c.cond of
  250. { unconditional branch }
  251. C_NONE:
  252. cond2str := tempstr+op2str[op];
  253. { bdnzt etc }
  254. else
  255. begin
  256. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  257. branchmode(op)+#9;
  258. case c.cond of
  259. C_LT..C_NU:
  260. cond2str := tempstr+reg2str[c.cr];
  261. C_T..C_DZF:
  262. cond2str := tempstr+tostr(c.crbit);
  263. end;
  264. end;
  265. end
  266. { we have a trap instruction }
  267. else
  268. begin
  269. internalerror(2002070601);
  270. { not yet implementer !!!!!!!!!!!!!!!!!!!!! }
  271. { case tempstr := 'tw';}
  272. end;
  273. end;
  274. end;
  275. Procedure TPPCGNUAssembler.WriteInstruction(hp : tai);
  276. var op: TAsmOp;
  277. s: string;
  278. i: byte;
  279. sep: string[3];
  280. begin
  281. op:=taicpu(hp).opcode;
  282. if is_calljmp(op) then
  283. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  284. s:=cond2str(op,taicpu(hp).condition)+
  285. getopstr_jmp(taicpu(hp).oper[0])
  286. else
  287. { process operands }
  288. begin
  289. s:=#9+op2str[op];
  290. if taicpu(hp).ops<>0 then
  291. begin
  292. {
  293. if not is_calljmp(op) then
  294. sep:=','
  295. else
  296. }
  297. sep:=#9;
  298. for i:=0 to taicpu(hp).ops-1 do
  299. begin
  300. s:=s+sep+getopstr(taicpu(hp).oper[i]);
  301. sep:=',';
  302. end;
  303. end;
  304. end;
  305. AsmWriteLn(s);
  306. end;
  307. begin
  308. RegisterAssembler(as_ppc_gas_info,TPPCGNUAssembler);
  309. end.
  310. {
  311. $Log$
  312. Revision 1.1 2002-07-07 09:44:31 florian
  313. * powerpc target fixed, very simple units can be compiled
  314. Revision 1.6 2002/05/18 13:34:26 peter
  315. * readded missing revisions
  316. Revision 1.5 2002/05/16 19:46:52 carl
  317. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  318. + try to fix temp allocation (still in ifdef)
  319. + generic constructor calls
  320. + start of tassembler / tmodulebase class cleanup
  321. Revision 1.3 2002/04/20 21:41:51 carl
  322. * renamed some constants
  323. Revision 1.2 2002/04/06 18:13:01 jonas
  324. * several powerpc-related additions and fixes
  325. }