agppcvasm.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit is the VASM writer for PowerPC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {****************************************************************************}
  18. { Helper routines for Instruction Writer }
  19. {****************************************************************************}
  20. unit agppcvasm;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. aasmbase,
  25. aasmtai,aasmdata,
  26. aggas,
  27. cpubase,cgutils,
  28. globtype;
  29. type
  30. TPPCInstrWriter=class(TCPUInstrWriter)
  31. procedure WriteInstruction(hp : tai);override;
  32. end;
  33. TPPCVASM=class(TGNUassembler)
  34. constructor create(smart: boolean); override;
  35. function MakeCmdLine: TCmdStr; override;
  36. end;
  37. topstr = string[4];
  38. function getreferencestring(var ref : treference) : string;
  39. function getopstr_jmp(const o:toper) : string;
  40. function getopstr(const o:toper) : string;
  41. function branchmode(o: tasmop): topstr;
  42. function cond2str(op: tasmop; c: tasmcond): string;
  43. implementation
  44. uses
  45. cutils,cfileutl,globals,verbose,
  46. cgbase,systems,
  47. assemble,script,
  48. itcpugas,cpuinfo,
  49. aasmcpu;
  50. {$ifdef cpu64bitaddr}
  51. const
  52. refaddr2str: array[trefaddr] of string[9] = ('', '', '', '', '@l', '@h', '@higher', '@highest', '@ha', '@highera', '@highesta');
  53. verbose_refaddrs = [addr_low, addr_high, addr_higher, addr_highest, addr_higha, addr_highera, addr_highesta];
  54. refaddr2str_darwin: array[trefaddr] of string[4] = ('','','','','lo16', 'hi16', '@err', '@err', 'ha16', '@err', '@err');
  55. {$else cpu64bitaddr}
  56. const
  57. refaddr2str: array[trefaddr] of string[3] = ('','','','','@l','@h','@ha');
  58. refaddr2str_darwin: array[trefaddr] of string[4] = ('','','','','lo16','hi16','ha16');
  59. verbose_refaddrs = [addr_low,addr_high,addr_higha];
  60. {$endif cpu64bitaddr}
  61. function getreferencestring(var ref : treference) : string;
  62. var
  63. s : string;
  64. begin
  65. with ref do
  66. begin
  67. if ((offset < -32768) or (offset > 32767)) and
  68. (refaddr = addr_no) then
  69. internalerror(2006052501);
  70. if (refaddr = addr_no) then
  71. s := ''
  72. else
  73. begin
  74. if target_info.system in [system_powerpc_darwin,system_powerpc64_darwin] then
  75. s := refaddr2str_darwin[refaddr]
  76. else
  77. s :='';
  78. s := s+'(';
  79. if assigned(symbol) then
  80. begin
  81. s:=s+symbol.name;
  82. if assigned(relsymbol) then
  83. s:=s+'-'+relsymbol.name;
  84. end;
  85. end;
  86. if offset<0 then
  87. s:=s+tostr(offset)
  88. else
  89. if (offset>0) then
  90. begin
  91. if assigned(symbol) then
  92. s:=s+'+'+tostr(offset)
  93. else
  94. s:=s+tostr(offset);
  95. end;
  96. if (refaddr in verbose_refaddrs) then
  97. begin
  98. s := s+')';
  99. if not(target_info.system in [system_powerpc_darwin,system_powerpc64_darwin]) then
  100. s := s+refaddr2str[refaddr];
  101. end;
  102. {$ifdef cpu64bitaddr}
  103. if (refaddr = addr_pic) then
  104. if (target_info.system <> system_powerpc64_linux) then
  105. s := s + ')'
  106. else
  107. s := s + ')@got';
  108. {$endif cpu64bitaddr}
  109. if (index=NR_NO) then
  110. begin
  111. if offset=0 then
  112. begin
  113. if not (assigned(symbol)) then
  114. s:=s+'0';
  115. end;
  116. if (base<>NR_NO) then
  117. s:=s+'('+gas_regname(base)+')'
  118. else if not assigned(symbol) and
  119. not(refaddr in verbose_refaddrs) then
  120. s:=s+'(0)';
  121. end
  122. else if (index<>NR_NO) and (base<>NR_NO) then
  123. begin
  124. if (offset=0) then
  125. s:=s+gas_regname(base)+','+gas_regname(index)
  126. else
  127. internalerror(2006052502);
  128. end;
  129. end;
  130. getreferencestring:=s;
  131. end;
  132. function getopstr_jmp(const o:toper) : string;
  133. var
  134. hs : string;
  135. begin
  136. case o.typ of
  137. top_reg :
  138. getopstr_jmp:=gas_regname(o.reg);
  139. { no top_ref jumping for powerpc }
  140. top_const :
  141. getopstr_jmp:=tostr(o.val);
  142. top_ref :
  143. begin
  144. if o.ref^.refaddr<>addr_full then
  145. internalerror(200402262);
  146. hs:=o.ref^.symbol.name;
  147. if o.ref^.offset>0 then
  148. hs:=hs+'+'+tostr(o.ref^.offset)
  149. else
  150. if o.ref^.offset<0 then
  151. hs:=hs+tostr(o.ref^.offset);
  152. getopstr_jmp:=hs;
  153. end;
  154. top_none:
  155. getopstr_jmp:='';
  156. else
  157. internalerror(2002070603);
  158. end;
  159. end;
  160. function getopstr(const o:toper) : string;
  161. var
  162. hs : string;
  163. begin
  164. case o.typ of
  165. top_reg:
  166. getopstr:=gas_regname(o.reg);
  167. top_const:
  168. getopstr:=tostr(longint(o.val));
  169. top_ref:
  170. if o.ref^.refaddr=addr_full then
  171. begin
  172. hs:=o.ref^.symbol.name;
  173. if o.ref^.offset>0 then
  174. hs:=hs+'+'+tostr(o.ref^.offset)
  175. else
  176. if o.ref^.offset<0 then
  177. hs:=hs+tostr(o.ref^.offset);
  178. getopstr:=hs;
  179. end
  180. else
  181. getopstr:=getreferencestring(o.ref^);
  182. else
  183. internalerror(2002070604);
  184. end;
  185. end;
  186. function branchmode(o: tasmop): topstr;
  187. var tempstr: topstr;
  188. begin
  189. tempstr := '';
  190. case o of
  191. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  192. A_BCLR,A_BCLRL: tempstr := 'lr';
  193. end;
  194. case o of
  195. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  196. end;
  197. case o of
  198. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  199. end;
  200. branchmode := tempstr;
  201. end;
  202. function cond2str(op: tasmop; c: tasmcond): string;
  203. { note: no checking is performed whether the given combination of }
  204. { conditions is valid }
  205. var
  206. tempstr: string;
  207. begin
  208. tempstr:=#9;
  209. case c.simple of
  210. false:
  211. begin
  212. cond2str := tempstr+gas_op2str[op];
  213. case c.dirhint of
  214. DH_None:;
  215. DH_Minus:
  216. cond2str:=cond2str+'-';
  217. DH_Plus:
  218. cond2str:=cond2str+'+';
  219. else
  220. internalerror(2003112901);
  221. end;
  222. cond2str:=cond2str+#9+tostr(c.bo)+','+tostr(c.bi);
  223. end;
  224. true:
  225. if (op >= A_B) and (op <= A_BCLRL) then
  226. case c.cond of
  227. { unconditional branch }
  228. C_NONE:
  229. cond2str := tempstr+gas_op2str[op];
  230. { bdnzt etc }
  231. else
  232. begin
  233. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  234. branchmode(op);
  235. case c.dirhint of
  236. DH_None:
  237. tempstr:=tempstr+#9;
  238. DH_Minus:
  239. tempstr:=tempstr+('-'+#9);
  240. DH_Plus:
  241. tempstr:=tempstr+('+'+#9);
  242. else
  243. internalerror(2003112901);
  244. end;
  245. case c.cond of
  246. C_LT..C_NU:
  247. cond2str := tempstr+gas_regname(newreg(R_SPECIALREGISTER,c.cr,R_SUBWHOLE));
  248. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  249. cond2str := tempstr+tostr(c.crbit);
  250. else
  251. cond2str := tempstr;
  252. end;
  253. end;
  254. end
  255. { we have a trap instruction }
  256. else
  257. begin
  258. internalerror(2002070601);
  259. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  260. { case tempstr := 'tw';}
  261. end;
  262. end;
  263. end;
  264. {****************************************************************************}
  265. { PowerPC Instruction Writer }
  266. {****************************************************************************}
  267. Procedure TPPCInstrWriter.WriteInstruction(hp : tai);
  268. var op: TAsmOp;
  269. s: string;
  270. i: byte;
  271. sep: string[3];
  272. begin
  273. op:=taicpu(hp).opcode;
  274. if is_calljmp(op) then
  275. begin
  276. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  277. case op of
  278. A_B,A_BA,A_BL,A_BLA:
  279. s:=#9+gas_op2str[op]+#9;
  280. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  281. s:=#9+gas_op2str[op]
  282. else
  283. begin
  284. s:=cond2str(op,taicpu(hp).condition);
  285. if (s[length(s)] <> #9) and
  286. (taicpu(hp).ops>0) then
  287. s := s + ',';
  288. end;
  289. end;
  290. if (taicpu(hp).ops>0) and (taicpu(hp).oper[0]^.typ<>top_none) then
  291. begin
  292. { first write the current contents of s, because the symbol }
  293. { may be 255 characters }
  294. owner.asmwrite(s);
  295. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  296. end;
  297. end
  298. else
  299. { process operands }
  300. begin
  301. s:=#9+gas_op2str[op];
  302. if taicpu(hp).ops<>0 then
  303. begin
  304. {
  305. if not is_calljmp(op) then
  306. sep:=','
  307. else
  308. }
  309. sep:=#9;
  310. for i:=0 to taicpu(hp).ops-1 do
  311. begin
  312. // debug code
  313. // writeln(s);
  314. // writeln(taicpu(hp).fileinfo.line);
  315. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  316. sep:=',';
  317. end;
  318. end;
  319. end;
  320. owner.AsmWriteLn(s);
  321. end;
  322. {****************************************************************************}
  323. { GNU PPC Assembler writer }
  324. {****************************************************************************}
  325. constructor TPPCVASM.create(smart: boolean);
  326. begin
  327. inherited create(smart);
  328. InstrWriter := TPPCInstrWriter.create(self);
  329. end;
  330. function TPPCVASM.MakeCmdLine: TCmdStr;
  331. begin
  332. result:=target_asm.asmcmd;
  333. if (cs_link_on_target in current_settings.globalswitches) then
  334. begin
  335. Replace(result,'$ASM',maybequoted(ScriptFixFileName(AsmFileName)));
  336. Replace(result,'$OBJ',maybequoted(ScriptFixFileName(ObjFileName)));
  337. end
  338. else
  339. begin
  340. Replace(result,'$ASM',maybequoted(Unix2AmigaPath(AsmFileName)));
  341. Replace(result,'$OBJ',maybequoted(Unix2AmigaPath(ObjFileName)));
  342. end;
  343. end;
  344. {*****************************************************************************
  345. Initialize
  346. *****************************************************************************}
  347. const
  348. as_powerpc_vasm_info : tasminfo =
  349. (
  350. id : as_powerpc_vasm;
  351. idtxt : 'VASM';
  352. asmbin : 'fpcvasm';
  353. asmcmd: '-quiet -Felf -o $OBJ $ASM';
  354. supported_targets : [system_powerpc_morphos];
  355. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  356. labelprefix : '.L';
  357. comment : '# ';
  358. );
  359. begin
  360. RegisterAssembler(as_powerpc_vasm_info,TPPCVASM);
  361. end.