agas.pas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. unit agas;
  19. interface
  20. uses
  21. cpubase,dos,globals,systems,{errors,}cobjects,aasm,strings,files,
  22. agatt
  23. {$ifdef GDB}
  24. ,gdb
  25. {$endif GDB}
  26. ;
  27. type
  28. paicpuattasmlist=^taicpuattasmlist;
  29. taicpuattasmlist=object(tattasmlist)
  30. function getreferencestring(var ref : treference) : string; Virtual;
  31. function getopstr_jmp(const o:toper) : string; Virtual;
  32. procedure WriteInstruction (HP : Pai); virtual;
  33. function cond2str(op: tasmop; c: tasmcond): string;
  34. { to construct the output for conditional branches }
  35. function branchmode(o: tasmop): string[4];
  36. end;
  37. implementation
  38. uses cpuasm;
  39. const
  40. gas_op2str : array[tasmop] of string[14] = ('<none>',
  41. 'add','add.','addo','addo.','addc','addc.','addco','addco.',
  42. 'adde','adde.','addeo','addeo.','addi','addic','addic.','addis',
  43. 'addme','addme.','addmeo','addmeo.','addze','addze.','addzeo',
  44. 'addzeo.','and','and.','andc','andc.','andi.','andis.','b',
  45. 'ba','bl','bla','bc','bca','bcl','bcla','bcctr','bcctrl','bclr',
  46. 'bclrl','cmp','cmpi','cmpl','cmpli','cntlzw','cntlzw.','crand',
  47. 'crandc','creqv','crnand','crnor','cror','crorc','crxor','dcba',
  48. 'dcbf','dcbi','dcbst','dcbt','divw','divw.','divwo','divwo.',
  49. 'divwu','divwu.','divwuo','divwuo.','eciwx','ecowx','eieio','eqv',
  50. 'eqv.','extsb','extsb.','extsh','extsh.','fabs','fabs.','fadd',
  51. 'fadd.','fadds','fadds.','fcompo','fcmpu','fctiw','fctw.','fctwz',
  52. 'fctwz.','fdiv','fdiv.','fdivs','fdivs.','fmadd','fmadd.','fmadds',
  53. 'fmadds.','fmr','fmsub','fmsub.','fmsubs','fmsubs.','fmul','fmul.',
  54. 'fmuls','fmuls.','fnabs','fnabs.','fneg','fneg.','fnmadd',
  55. 'fnmadd.','fnmadds','fnmadds.','fnmsub','fnmsub.','fnmsubs',
  56. 'fnmsubs.','fres','fres.','frsp','frsp.','frsqrte','frsqrte.',
  57. 'fsel','fsel.','fsqrt','fsqrt.','fsqrts','fsqrts.','fsub','fsub.',
  58. 'fsubs','fsubs.','icbi','isync','lbz','lbzu','lbzux','lbzx',
  59. 'lfd','lfdu','lfdux','lfdx','lfs','lfsu','lfsux','lfsx','lha',
  60. 'lhau','lhaux','lhax','hbrx','lhz','lhzu','lhzux','lhzx','lmw',
  61. 'lswi','lswx','lwarx','lwbrx','lwz','lwzu','lwzux','lwzx','mcrf',
  62. 'mcrfs','mcrxr','lcrxe','mfcr','mffs','maffs.','mfmsr','mfspr','mfsr',
  63. 'mfsrin','mftb','mtfcrf','mtfd0','mtfsb1','mtfsf','mtfsf.',
  64. 'mtfsfi','mtfsfi.','mtmsr','mtspr','mtsr','mtsrin','mulhw',
  65. 'mulhw.','mulhwu','mulhwu.','mulli','mullh','mullw.','mullwo',
  66. 'mullwo.','nand','nand.','neg','neg.','nego','nego.','nor','nor.',
  67. 'or','or.','orc','orc.','ori','oris', 'rfi', 'rlwimi', 'rlwimi.',
  68. 'rlwinm', 'tlwinm.','rlwnm','sc','slw', 'slw.', 'sraw', 'sraw.',
  69. 'srawi', 'srawi.','srw', 'srw.', 'stb', 'stbu', 'stbux','stbx','stfd',
  70. 'stfdu', 'stfdux', 'stfdx', 'stfiwx', 'stfs', 'stfsu', 'stfsux', 'stfsx',
  71. 'sth', 'sthbrx', 'sthu', 'sthux', 'sthx', 'stmw', 'stswi', 'stswx', 'stw',
  72. 'stwbrx', 'stwx.', 'stwu', 'stwux', 'stwx', 'subf', 'subf.', 'subfo',
  73. 'subfo.', 'subfc', 'subc.', 'subfco', 'subfco.', 'subfe', 'subfe.',
  74. 'subfeo', 'subfeo.', 'subfic', 'subfme', 'subfme.', 'subfmeo', 'subfmeo.',
  75. 'subfze', 'subfze.', 'subfzeo', 'subfzeo.', 'sync', 'tlbia', 'tlbie',
  76. 'tlbsync', 'tw', 'twi', 'xor', 'xor.', 'xori', 'xoris',
  77. { some simplified mnemonics }
  78. 'subi', 'subis', 'subic', 'subic.', 'sub', 'sub.', 'subo', 'subo.',
  79. 'subc', 'subc.', 'subco', '.subco.', 'cmpwi', 'cmpw', 'cmplwi', 'cmplw',
  80. 'extlwi', 'extlwi.', 'extrwi', 'extrwi.', 'inslwi', 'inslwi.', 'insrwi',
  81. 'insrwi.', 'rotlwi', 'rotlwi.', 'rotlw', 'rotlw.', 'slwi', 'slwi.',
  82. 'srwi', 'srwi.', 'clrlwi', 'clrlwi.', 'clrrwi', 'clrrwi.', 'clrslwi',
  83. 'clrslwi.', 'blr', 'bctr', 'blrl', 'bctrl', 'crset', 'crclr', 'crmove',
  84. 'crnot', 'mt', 'mf','nop', 'li', 'lis', 'la', 'mr','not', 'mtcr');
  85. function taicpuattasmlist.getreferencestring(var ref : treference) : string;
  86. var
  87. s : string;
  88. begin
  89. if ref.is_immediate then
  90. begin
  91. {$ifndef testing}
  92. internalerror(1000101);
  93. exit;
  94. {$else testing}
  95. writeln('internalerror 1000101');
  96. halt(1);
  97. {$endif testing}
  98. end
  99. else
  100. begin
  101. with ref do
  102. begin
  103. inc(offset,offsetfixup);
  104. if (offset < -32768) or (offset > 32767) then
  105. {$ifndef testing}
  106. internalerror(19991);
  107. {$else testing}
  108. begin
  109. writeln('internalerror 19991');
  110. halt(1);
  111. end;
  112. {$endif testing}
  113. s:='';
  114. if assigned(symbol) then
  115. s:=s+symbol^.name + symaddr2str[symaddr];
  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 (index=R_NO) and (base<>R_NO) then
  127. s:=s+'('+att_reg2str[base]+')'
  128. else if (index<>R_NO) and (base<>R_NO) and (offset = 0) then
  129. s:=s+att_reg2str[base]+','+att_reg2str[index]
  130. else if ((index<>R_NO) or (base<>R_NO)) then
  131. {$ifndef testing}
  132. internalerror(19992);
  133. {$else testing}
  134. begin
  135. writeln('internalerror 19992');
  136. halt(1);
  137. end;
  138. {$endif testing}
  139. end;
  140. end;
  141. getreferencestring:=s;
  142. end;
  143. function taicpuattasmlist.getopstr_jmp(const o:toper) : string;
  144. var
  145. hs : string;
  146. begin
  147. case o.typ of
  148. top_reg :
  149. getopstr_jmp:=att_reg2str[o.reg];
  150. { no top_ref jumping for powerpc }
  151. top_const :
  152. getopstr_jmp:=tostr(o.val);
  153. top_symbol :
  154. begin
  155. hs:=o.sym^.name;
  156. if o.symofs>0 then
  157. hs:=hs+'+'+tostr(o.symofs)
  158. else
  159. if o.symofs<0 then
  160. hs:=hs+tostr(o.symofs);
  161. getopstr_jmp:=hs;
  162. end;
  163. else
  164. {$ifndef testing}
  165. internalerror(10001);
  166. {$else testing}
  167. begin
  168. writeln('internalerror 10001');
  169. halt(1);
  170. end;
  171. {$endif testing}
  172. end;
  173. end;
  174. Procedure taicpuattasmlist.WriteInstruction (HP : Pai);
  175. var op: TAsmOp;
  176. s: string;
  177. i: byte;
  178. sep: string[3];
  179. begin
  180. op:=paicpu(hp)^.opcode;
  181. if is_calljmp(op) then
  182. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  183. s:=s+cond2str(op,paicpu(hp)^.condition)+
  184. getopstr_jmp(paicpu(hp)^.oper[0])
  185. else
  186. { process operands }
  187. begin
  188. s:=#9+gas_op2str[op];
  189. if paicpu(hp)^.ops<>0 then
  190. begin
  191. if not is_calljmp(op) then
  192. sep := ','
  193. else sep := '#9';
  194. for i:=0 to paicpu(hp)^.ops-1 do
  195. begin
  196. s:=s+sep+getopstr(paicpu(hp)^.oper[i])
  197. sep:=',';
  198. end;
  199. end;
  200. end;
  201. AsmWriteLn(s);
  202. end;
  203. function taicpuattasmlist.cond2str(op: tasmop; c: tasmcond): string;
  204. { note: no checking is performed whether the given combination of }
  205. { conditions is valid }
  206. var tempstr: sintrg;
  207. begin
  208. tempstr := '#9';
  209. case c.simple of
  210. false: cond2str := tempstr+gas_op2str[op]+'#9'+tostr(c.bo)+','+
  211. tostr(c.bi);
  212. true:
  213. if (op >= A_B) and (op <= A_BCLRL) then
  214. case c.cond of
  215. { unconditional branch }
  216. CF_NONE: condstr := tempstr+op2str(op);
  217. { bdnzt etc }
  218. else
  219. begin
  220. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  221. branchmode(op)+'#9';
  222. case op of
  223. CF_LT..CF_NU:
  224. cond2str := tempstr+att_reg2str[c.cr];
  225. CF_T..CF_DZF:
  226. cond2str := tempstr+tostr(c.crbit);
  227. end;
  228. end;
  229. end
  230. { we have a trap instruction }
  231. { not yet implementer !!!!!!!!!!!!!!!!!!!!! }
  232. { else
  233. begin
  234. case tempstr := 'tw';}
  235. end;
  236. end;
  237. function taicpuattasmlist.branchmode(o: tasmop): string[4];
  238. var tempstr: string[4];
  239. begin
  240. tempstr := '';
  241. case o of
  242. A_BCCTR,A_BCCTRL: tempstr := 'ctr'
  243. A_BCLR,A_BCLRL: tempstr := 'lr'
  244. case o of
  245. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  246. end;
  247. case o of
  248. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  249. end;
  250. branchmode := tempstr;
  251. end;
  252. end.
  253. {
  254. $Log$
  255. Revision 1.6 2002-05-18 13:34:26 peter
  256. * readded missing revisions
  257. Revision 1.5 2002/05/16 19:46:52 carl
  258. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  259. + try to fix temp allocation (still in ifdef)
  260. + generic constructor calls
  261. + start of tassembler / tmodulebase class cleanup
  262. Revision 1.3 2002/04/20 21:41:51 carl
  263. * renamed some constants
  264. Revision 1.2 2002/04/06 18:13:01 jonas
  265. * several powerpc-related additions and fixes
  266. }