agppcmpw.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. {
  2. $Id$
  3. Copyright (c) 2002 by Florian Klaempfl
  4. This unit implements an asmoutput class for PowerPC with MPW syntax
  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. {
  19. This unit implements an asmoutput class for PowerPC with MPW syntax
  20. }
  21. unit agppcmpw;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. aasmbase,aasmtai,aasmcpu,assemble,
  26. cpubase;
  27. const
  28. mpw_reg2str : treg2strtable = ('',
  29. 'r0','r1','r2','r3','r4','r5','r6','r7','r8','r9','r10','r11','r12','r13','r14','r15','r16',
  30. 'r17','r18','r19','r20','r21','r22','r23','r24','r25','r26','r27','r28','r29','r30','r31',
  31. 'f0','f1','f2','f3','f4','f5','f6','f7', 'f8','f9','f10','f11','f12',
  32. 'f13','f14','f15','f16','f17', 'f18','f19','f20','f21','f22', 'f23','f24',
  33. 'f25','f26','f27','f28','f29','f30','f31',
  34. 'v0','v1','v2','v3','v4','v5','v6','v7','v8','v9','v10','v11','v12',
  35. 'v13','v14','v15','v16','v17','v18','v19','v20','v21','v22', 'v23','v24',
  36. 'v25','v26','v27','v28','v29','v30','v31',
  37. 'cR','cr0','cr1','cr2','cr3','cr4','cr5','cr6','cr7',
  38. 'xer','lr','ctr','fpscr'
  39. );
  40. type
  41. TPPCMPWAssembler = class(TExternalAssembler)
  42. procedure WriteTree(p:TAAsmoutput);override;
  43. procedure WriteAsmList;override;
  44. Function DoAssemble:boolean;override;
  45. procedure WriteExternals;
  46. procedure WriteAsmFileHeader;
  47. end;
  48. implementation
  49. uses
  50. {$ifdef delphi}
  51. sysutils,
  52. {$endif}
  53. cutils,globtype,globals,systems,cclasses,
  54. verbose,finput,fmodule,script,cpuinfo
  55. ;
  56. const
  57. line_length = 70;
  58. function ReplaceForbiddenChars(var s: string):Boolean;
  59. {Returns wheater a replacement has occured.}
  60. var
  61. i:Integer;
  62. {The dollar sign is not allowed in MPW PPCAsm}
  63. begin
  64. ReplaceForbiddenChars:=false;
  65. for i:=1 to Length(s) do
  66. if s[i]='$' then
  67. begin
  68. s[i]:='s';
  69. ReplaceForbiddenChars:=true;
  70. end;
  71. end;
  72. const
  73. {*** From here is copyed from agppcgas.pp, except where marked with CHANGED.
  74. Perhaps put in a third common file. ***}
  75. op2str : array[tasmop] of string[14] = ('<none>',
  76. 'add','add.','addo','addo.','addc','addc.','addco','addco.',
  77. 'adde','adde.','addeo','addeo.','addi','addic','addic.','addis',
  78. 'addme','addme.','addmeo','addmeo.','addze','addze.','addzeo',
  79. 'addzeo.','and','and.','andc','andc.','andi.','andis.','b',
  80. 'ba','bl','bla','bc','bca','bcl','bcla','bcctr','bcctrl','bclr',
  81. 'bclrl','cmp','cmpi','cmpl','cmpli','cntlzw','cntlzw.','crand',
  82. 'crandc','creqv','crnand','crnor','cror','crorc','crxor','dcba',
  83. 'dcbf','dcbi','dcbst','dcbt','divw','divw.','divwo','divwo.',
  84. 'divwu','divwu.','divwuo','divwuo.','eciwx','ecowx','eieio','eqv',
  85. 'eqv.','extsb','extsb.','extsh','extsh.','fabs','fabs.','fadd',
  86. 'fadd.','fadds','fadds.','fcmpo','fcmpu','fctiw','fctw.','fctwz',
  87. 'fctwz.','fdiv','fdiv.','fdivs','fdivs.','fmadd','fmadd.','fmadds',
  88. 'fmadds.','fmr','fmsub','fmsub.','fmsubs','fmsubs.','fmul','fmul.',
  89. 'fmuls','fmuls.','fnabs','fnabs.','fneg','fneg.','fnmadd',
  90. 'fnmadd.','fnmadds','fnmadds.','fnmsub','fnmsub.','fnmsubs',
  91. 'fnmsubs.','fres','fres.','frsp','frsp.','frsqrte','frsqrte.',
  92. 'fsel','fsel.','fsqrt','fsqrt.','fsqrts','fsqrts.','fsub','fsub.',
  93. 'fsubs','fsubs.','icbi','isync','lbz','lbzu','lbzux','lbzx',
  94. 'lfd','lfdu','lfdux','lfdx','lfs','lfsu','lfsux','lfsx','lha',
  95. 'lhau','lhaux','lhax','hbrx','lhz','lhzu','lhzux','lhzx','lmw',
  96. 'lswi','lswx','lwarx','lwbrx','lwz','lwzu','lwzux','lwzx','mcrf',
  97. 'mcrfs','mcrxr','lcrxe','mfcr','mffs','maffs.','mfmsr','mfspr','mfsr',
  98. 'mfsrin','mftb','mtfcrf','mtfd0','mtfsb1','mtfsf','mtfsf.',
  99. 'mtfsfi','mtfsfi.','mtmsr','mtspr','mtsr','mtsrin','mulhw',
  100. 'mulhw.','mulhwu','mulhwu.','mulli','mullw','mullw.','mullwo',
  101. 'mullwo.','nand','nand.','neg','neg.','nego','nego.','nor','nor.',
  102. 'or','or.','orc','orc.','ori','oris', 'rfi', 'rlwimi', 'rlwimi.',
  103. 'rlwinm', 'rlwinm.','rlwnm','sc','slw', 'slw.', 'sraw', 'sraw.',
  104. 'srawi', 'srawi.','srw', 'srw.', 'stb', 'stbu', 'stbux','stbx','stfd',
  105. 'stfdu', 'stfdux', 'stfdx', 'stfiwx', 'stfs', 'stfsu', 'stfsux', 'stfsx',
  106. 'sth', 'sthbrx', 'sthu', 'sthux', 'sthx', 'stmw', 'stswi', 'stswx', 'stw',
  107. 'stwbrx', 'stwx.', 'stwu', 'stwux', 'stwx', 'subf', 'subf.', 'subfo',
  108. 'subfo.', 'subfc', 'subc.', 'subfco', 'subfco.', 'subfe', 'subfe.',
  109. 'subfeo', 'subfeo.', 'subfic', 'subfme', 'subfme.', 'subfmeo', 'subfmeo.',
  110. 'subfze', 'subfze.', 'subfzeo', 'subfzeo.', 'sync', 'tlbia', 'tlbie',
  111. 'tlbsync', 'tw', 'twi', 'xor', 'xor.', 'xori', 'xoris',
  112. { some simplified mnemonics }
  113. 'subi', 'subis', 'subic', 'subic.', 'sub', 'sub.', 'subo', 'subo.',
  114. 'subc', 'subc.', 'subco', 'subco.', 'cmpwi', 'cmpw', 'cmplwi', 'cmplw',
  115. 'extlwi', 'extlwi.', 'extrwi', 'extrwi.', 'inslwi', 'inslwi.', 'insrwi',
  116. 'insrwi.', 'rotlwi', 'rotlwi.', 'rotlw', 'rotlw.', 'slwi', 'slwi.',
  117. 'srwi', 'srwi.', 'clrlwi', 'clrlwi.', 'clrrwi', 'clrrwi.', 'clrslwi',
  118. 'clrslwi.', 'blr', 'bctr', 'blrl', 'bctrl', 'crset', 'crclr', 'crmove',
  119. 'crnot', 'mt', 'mf','nop', 'li', 'lis', 'la', 'mr','mr.','not', 'mtcr', 'mtlr', 'mflr',
  120. 'mtctr', 'mfctr');
  121. symaddr2str: array[trefsymaddr] of string[3] = ('','@ha','@l');
  122. function getreferencestring(var ref : treference) : string;
  123. var
  124. s : string;
  125. begin
  126. with ref do
  127. begin
  128. inc(offset,offsetfixup);
  129. if ((offset < -32768) or (offset > 32767)) and
  130. (symaddr = refs_full) then
  131. internalerror(19991);
  132. if (symaddr = refs_full) then
  133. s := ''
  134. else if not assigned(symbol) then
  135. s := '('
  136. else
  137. begin
  138. {s:='('+symbol.name; CHANGED from this to: }
  139. s:= symbol.name;
  140. ReplaceForbiddenChars(s);
  141. s:='('+s;
  142. end;
  143. if offset<0 then
  144. s:=s+tostr(offset)
  145. else
  146. if (offset>0) then
  147. begin
  148. if assigned(symbol) then
  149. s:=s+'+'+tostr(offset)
  150. else
  151. s:=s+tostr(offset);
  152. end;
  153. if (symaddr <> refs_full) then
  154. s := s+')'+symaddr2str[symaddr];
  155. if (index=R_NO) and (base<>R_NO) then
  156. begin
  157. if offset=0 then
  158. begin
  159. if assigned(symbol) then
  160. s:=s+'+0'
  161. else
  162. s:=s+'0';
  163. end;
  164. s:=s+'('+mpw_reg2str[base]+')'
  165. end
  166. else if (index<>R_NO) and (base<>R_NO) and (offset=0) then
  167. s:=s+mpw_reg2str[base]+','+mpw_reg2str[index]
  168. else if ((index<>R_NO) or (base<>R_NO)) then
  169. internalerror(19992);
  170. end;
  171. getreferencestring:=s;
  172. end;
  173. function getopstr_jmp(const o:toper) : string;
  174. var
  175. hs : string;
  176. begin
  177. case o.typ of
  178. top_reg :
  179. getopstr_jmp:=mpw_reg2str[o.reg];
  180. { no top_ref jumping for powerpc }
  181. top_const :
  182. getopstr_jmp:=tostr(o.val);
  183. top_symbol :
  184. begin
  185. hs:=o.sym.name;
  186. ReplaceForbiddenChars(hs);
  187. if o.symofs>0 then
  188. hs:=hs+'+'+tostr(o.symofs)
  189. else
  190. if o.symofs<0 then
  191. hs:=hs+tostr(o.symofs);
  192. getopstr_jmp:=hs;
  193. end;
  194. top_none:
  195. getopstr_jmp:='';
  196. else
  197. {$ifndef testing}
  198. internalerror(2002070603);
  199. {$else testing}
  200. begin
  201. writeln('internalerror 10001');
  202. halt(1);
  203. end;
  204. {$endif testing}
  205. end;
  206. end;
  207. function getopstr(const o:toper) : string;
  208. var
  209. hs : string;
  210. begin
  211. case o.typ of
  212. top_reg:
  213. getopstr:=mpw_reg2str[o.reg];
  214. { no top_ref jumping for powerpc }
  215. top_const:
  216. getopstr:=tostr(longint(o.val));
  217. top_ref:
  218. getopstr:=getreferencestring(o.ref^);
  219. top_symbol:
  220. begin
  221. hs:=o.sym.name;
  222. ReplaceForbiddenChars(hs);
  223. if o.symofs>0 then
  224. hs:=hs+'+'+tostr(o.symofs)
  225. else
  226. if o.symofs<0 then
  227. hs:=hs+tostr(o.symofs);
  228. getopstr:=hs;
  229. end;
  230. else
  231. {$ifndef testing}
  232. internalerror(2002070604);
  233. {$else testing}
  234. begin
  235. writeln('internalerror 10001');
  236. halt(1);
  237. end;
  238. {$endif testing}
  239. end;
  240. end;
  241. function branchmode(o: tasmop): string[4];
  242. var tempstr: string[4];
  243. begin
  244. tempstr := '';
  245. case o of
  246. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  247. A_BCLR,A_BCLRL: tempstr := 'lr';
  248. end;
  249. case o of
  250. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  251. end;
  252. case o of
  253. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  254. end;
  255. branchmode := tempstr;
  256. end;
  257. function cond2str(op: tasmop; c: tasmcond): string;
  258. { note: no checking is performed whether the given combination of }
  259. { conditions is valid }
  260. var tempstr: string;
  261. begin
  262. tempstr:=#9;
  263. case c.simple of
  264. false: cond2str := tempstr+op2str[op]+#9+tostr(c.bo)+','+
  265. tostr(c.bi);
  266. true:
  267. if (op >= A_B) and (op <= A_BCLRL) then
  268. case c.cond of
  269. { unconditional branch }
  270. C_NONE:
  271. cond2str := tempstr+op2str[op];
  272. { bdnzt etc }
  273. else
  274. begin
  275. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  276. branchmode(op)+#9;
  277. case c.cond of
  278. C_LT..C_NU:
  279. cond2str := tempstr+mpw_reg2str[c.cr];
  280. C_T..C_DZF:
  281. cond2str := tempstr+tostr(c.crbit);
  282. end;
  283. end;
  284. end
  285. { we have a trap instruction }
  286. else
  287. begin
  288. internalerror(2002070601);
  289. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  290. { case tempstr := 'tw';}
  291. end;
  292. end;
  293. end;
  294. Function GetInstruction(hp : tai):string; {CHANGED from methot to proc}
  295. var op: TAsmOp;
  296. s: string;
  297. i: byte;
  298. sep: string[3];
  299. begin
  300. op:=taicpu(hp).opcode;
  301. if is_calljmp(op) then
  302. begin
  303. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  304. case op of
  305. A_B,A_BA,A_BLA:
  306. s:=#9+op2str[op]+#9;
  307. A_BL:
  308. s:=#9+op2str[op]+#9'.';
  309. else
  310. s:=cond2str(op,taicpu(hp).condition)+',';
  311. end;
  312. s:=s+getopstr_jmp(taicpu(hp).oper[0]);
  313. if op=A_BL then
  314. s:=s+'[PR]';
  315. end
  316. else
  317. { process operands }
  318. begin
  319. s:=#9+op2str[op];
  320. if taicpu(hp).ops<>0 then
  321. begin
  322. sep:=#9;
  323. for i:=0 to taicpu(hp).ops-1 do
  324. begin
  325. s:=s+sep+getopstr(taicpu(hp).oper[i]);
  326. sep:=',';
  327. end;
  328. end;
  329. end;
  330. GetInstruction:=s;
  331. end;
  332. {*** Until here is copyed from agppcgas.pp. ***}
  333. function single2str(d : single) : string;
  334. var
  335. hs : string;
  336. p : byte;
  337. begin
  338. str(d,hs);
  339. { nasm expects a lowercase e }
  340. p:=pos('E',hs);
  341. if p>0 then
  342. hs[p]:='e';
  343. p:=pos('+',hs);
  344. if p>0 then
  345. delete(hs,p,1);
  346. single2str:=lower(hs);
  347. end;
  348. function double2str(d : double) : string;
  349. var
  350. hs : string;
  351. p : byte;
  352. begin
  353. str(d,hs);
  354. { nasm expects a lowercase e }
  355. p:=pos('E',hs);
  356. if p>0 then
  357. hs[p]:='e';
  358. p:=pos('+',hs);
  359. if p>0 then
  360. delete(hs,p,1);
  361. double2str:=lower(hs);
  362. end;
  363. function fixline(s:string):string;
  364. {
  365. return s with all leading and ending spaces and tabs removed
  366. }
  367. var
  368. i,j,k : longint;
  369. begin
  370. i:=length(s);
  371. while (i>0) and (s[i] in [#9,' ']) do
  372. dec(i);
  373. j:=1;
  374. while (j<i) and (s[j] in [#9,' ']) do
  375. inc(j);
  376. for k:=j to i do
  377. if s[k] in [#0..#31,#127..#255] then
  378. s[k]:='.';
  379. fixline:=Copy(s,j,i-j+1);
  380. end;
  381. {****************************************************************************
  382. PowerPC MPW Assembler
  383. ****************************************************************************}
  384. var
  385. LasTSec : TSection;
  386. lastfileinfo : tfileposinfo;
  387. infile,
  388. lastinfile : tinputfile;
  389. const
  390. ait_const2str:array[ait_const_32bit..ait_const_8bit] of string[8]=
  391. (#9'DC.L'#9,#9'DC.W'#9,#9'DC.B'#9);
  392. Function PadTabs(const p:string;addch:char):string;
  393. var
  394. s : string;
  395. i : longint;
  396. begin
  397. i:=length(p);
  398. if addch<>#0 then
  399. begin
  400. inc(i);
  401. s:=p+addch;
  402. end
  403. else
  404. s:=p;
  405. if i<8 then
  406. PadTabs:=s+#9#9
  407. else
  408. PadTabs:=s+#9;
  409. end;
  410. procedure TPPCMPWAssembler.WriteTree(p:TAAsmoutput);
  411. const
  412. nolinetai =[ait_label,
  413. ait_regalloc,ait_tempalloc,
  414. {$ifdef GDB}
  415. ait_stabn,ait_stabs,ait_stab_function_name,
  416. {$endif GDB}
  417. ait_cut,ait_marker,ait_align,ait_section];
  418. {TODO: Perhaps replace internalerror(10000) with something else}
  419. var
  420. s,
  421. prefix,
  422. suffix : string;
  423. hp : tai;
  424. counter,
  425. lines,
  426. InlineLevel : longint;
  427. i,j,l : longint;
  428. consttyp : taitype;
  429. found,
  430. do_line,DoNotSplitLine,
  431. quoted : boolean;
  432. sep : char;
  433. replaced : boolean;
  434. begin
  435. if not assigned(p) then
  436. exit;
  437. { lineinfo is only needed for codesegment (PFV) }
  438. do_line:=((cs_asm_source in aktglobalswitches) or
  439. (cs_lineinfo in aktmoduleswitches))
  440. and (p=codesegment);
  441. InlineLevel:=0;
  442. DoNotSplitLine:=false;
  443. hp:=tai(p.first);
  444. while assigned(hp) do
  445. begin
  446. if do_line and not(hp.typ in nolinetai) and
  447. not DoNotSplitLine then
  448. begin
  449. { load infile }
  450. if lastfileinfo.fileindex<>hp.fileinfo.fileindex then
  451. begin
  452. infile:=current_module.sourcefiles.get_file(hp.fileinfo.fileindex);
  453. if assigned(infile) then
  454. begin
  455. { open only if needed !! }
  456. if (cs_asm_source in aktglobalswitches) then
  457. infile.open;
  458. end;
  459. { avoid unnecessary reopens of the same file !! }
  460. lastfileinfo.fileindex:=hp.fileinfo.fileindex;
  461. { be sure to change line !! }
  462. lastfileinfo.line:=-1;
  463. end;
  464. { write source }
  465. if (cs_asm_source in aktglobalswitches) and
  466. assigned(infile) then
  467. begin
  468. if (infile<>lastinfile) then
  469. begin
  470. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  471. if assigned(lastinfile) then
  472. lastinfile.close;
  473. end;
  474. if (hp.fileinfo.line<>lastfileinfo.line) and
  475. ((hp.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  476. begin
  477. if (hp.fileinfo.line<>0) and
  478. ((infile.linebuf^[hp.fileinfo.line]>=0) or (InlineLevel>0)) then
  479. AsmWriteLn(target_asm.comment+'['+tostr(hp.fileinfo.line)+'] '+
  480. fixline(infile.GetLineStr(hp.fileinfo.line)));
  481. { set it to a negative value !
  482. to make that is has been read already !! PM }
  483. if (infile.linebuf^[hp.fileinfo.line]>=0) then
  484. infile.linebuf^[hp.fileinfo.line]:=-infile.linebuf^[hp.fileinfo.line]-1;
  485. end;
  486. end;
  487. lastfileinfo:=hp.fileinfo;
  488. lastinfile:=infile;
  489. end;
  490. DoNotSplitLine:=false;
  491. case hp.typ of
  492. ait_comment:
  493. begin
  494. AsmWrite(target_asm.comment);
  495. AsmWritePChar(tai_comment(hp).str);
  496. AsmLn;
  497. end;
  498. ait_regalloc,
  499. ait_tempalloc:
  500. ;
  501. ait_section:
  502. begin
  503. {if LasTSec<>sec_none then
  504. AsmWriteLn('_'+target_asm.secnames[LasTSec]+#9#9'ENDS');}
  505. if tai_section(hp).sec<>sec_none then
  506. begin
  507. AsmLn;
  508. AsmWriteLn(#9+target_asm.secnames[tai_section(hp).sec]){+#9#9+
  509. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  510. target_asm.secnames[tai_section(hp).sec]+'''');}
  511. end;
  512. LasTSec:=tai_section(hp).sec;
  513. end;
  514. ait_align:
  515. begin
  516. case tai_align(hp).aligntype of
  517. 1:AsmWriteLn(#9'ALIGN 0');
  518. 2:AsmWriteLn(#9'ALIGN 1');
  519. 4:AsmWriteLn(#9'ALIGN 2');
  520. otherwise internalerror(10000);
  521. end;
  522. end;
  523. ait_datablock:
  524. begin
  525. s:= tai_datablock(hp).sym.name;
  526. replaced:= ReplaceForbiddenChars(s);
  527. if tai_datablock(hp).is_global then
  528. if replaced then
  529. AsmWriteLn(#9'EXPORT'#9+s+' => '''+tai_datablock(hp).sym.name+'''')
  530. else
  531. AsmWriteLn(#9'EXPORT'#9+s);
  532. AsmWriteLn(PadTabs(s,#0)+'DS.B '+tostr(tai_datablock(hp).size));
  533. {TODO: ? PadTabs(s,#0) }
  534. end;
  535. ait_const_32bit,
  536. ait_const_8bit,
  537. ait_const_16bit :
  538. begin
  539. AsmWrite(ait_const2str[hp.typ]+tostr(tai_const(hp).value));
  540. consttyp:=hp.typ;
  541. l:=0;
  542. repeat
  543. found:=(not (tai(hp.next)=nil)) and (tai(hp.next).typ=consttyp);
  544. if found then
  545. begin
  546. hp:=tai(hp.next);
  547. s:=','+tostr(tai_const(hp).value);
  548. AsmWrite(s);
  549. inc(l,length(s));
  550. end;
  551. until (not found) or (l>line_length);
  552. AsmLn;
  553. end;
  554. ait_const_symbol : begin
  555. AsmWriteLn(#9#9'DD'#9'offset '+tai_const_symbol(hp).sym.name);
  556. if tai_const_symbol(hp).offset>0 then
  557. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  558. else if tai_const_symbol(hp).offset<0 then
  559. AsmWrite(tostr(tai_const_symbol(hp).offset));
  560. AsmLn;
  561. end;
  562. ait_real_32bit : AsmWriteLn(#9'DC.L'#9'"'+single2str(tai_real_32bit(hp).value)+'"');
  563. ait_real_64bit : AsmWriteLn(#9'DC.D'#9'"'+double2str(tai_real_64bit(hp).value)+'"');
  564. ait_string : begin
  565. {NOTE When a single quote char is encountered, it is
  566. replaced with a numeric ascii value. It could also
  567. have been replaced with the escape seq of double quotes.}
  568. counter := 0;
  569. lines := tai_string(hp).len div line_length;
  570. { separate lines in different parts }
  571. if tai_string(hp).len > 0 then
  572. Begin
  573. for j := 0 to lines-1 do
  574. begin
  575. AsmWrite(#9'DC.B'#9);
  576. quoted:=false;
  577. for i:=counter to counter+line_length do
  578. begin
  579. { it is an ascii character. }
  580. if (ord(tai_string(hp).str[i])>31) and
  581. (ord(tai_string(hp).str[i])<128) and
  582. (tai_string(hp).str[i]<>'''') then
  583. begin
  584. if not(quoted) then
  585. begin
  586. if i>counter then
  587. AsmWrite(',');
  588. AsmWrite('''');
  589. end;
  590. AsmWrite(tai_string(hp).str[i]);
  591. quoted:=true;
  592. end { if > 31 and < 128 and ord('"') }
  593. else
  594. begin
  595. if quoted then
  596. AsmWrite('''');
  597. if i>counter then
  598. AsmWrite(',');
  599. quoted:=false;
  600. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  601. end;
  602. end; { end for i:=0 to... }
  603. if quoted then AsmWrite('''');
  604. AsmWrite(target_info.newline);
  605. counter := counter+line_length;
  606. end; { end for j:=0 ... }
  607. { do last line of lines }
  608. AsmWrite(#9'DC.B'#9);
  609. quoted:=false;
  610. for i:=counter to tai_string(hp).len-1 do
  611. begin
  612. { it is an ascii character. }
  613. if (ord(tai_string(hp).str[i])>31) and
  614. (ord(tai_string(hp).str[i])<128) and
  615. (tai_string(hp).str[i]<>'''') then
  616. begin
  617. if not(quoted) then
  618. begin
  619. if i>counter then
  620. AsmWrite(',');
  621. AsmWrite('''');
  622. end;
  623. AsmWrite(tai_string(hp).str[i]);
  624. quoted:=true;
  625. end { if > 31 and < 128 and " }
  626. else
  627. begin
  628. if quoted then
  629. AsmWrite('''');
  630. if i>counter then
  631. AsmWrite(',');
  632. quoted:=false;
  633. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  634. end;
  635. end; { end for i:=0 to... }
  636. if quoted then
  637. AsmWrite('''');
  638. end;
  639. AsmLn;
  640. end;
  641. ait_label : begin
  642. if tai_label(hp).l.is_used then
  643. begin
  644. s:= tai_label(hp).l.name;
  645. ReplaceForbiddenChars(s);
  646. AsmWrite(s);
  647. {if assigned(hp.next) and not(tai(hp.next).typ in
  648. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  649. ait_const_symbol,ait_const_rva,
  650. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  651. AsmWriteLn(':')
  652. else
  653. DoNotSplitLine:=true;}
  654. AsmWriteLn(':');
  655. end;
  656. end;
  657. ait_direct : begin
  658. AsmWritePChar(tai_direct(hp).str);
  659. AsmLn;
  660. end;
  661. ait_symbol : begin
  662. s:= tai_label(hp).l.name;
  663. replaced:= ReplaceForbiddenChars(s);
  664. if tai_symbol(hp).is_global then
  665. if replaced then
  666. AsmWriteLn(#9'EXPORT'#9+s+' => '''+tai_symbol(hp).sym.name+'''')
  667. else
  668. AsmWriteLn(#9'EXPORT'#9+s);
  669. AsmWrite(s);
  670. {if assigned(hp.next) and not(tai(hp.next).typ in
  671. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  672. ait_const_symbol,ait_const_rva,
  673. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then}
  674. AsmWriteLn(':')
  675. end;
  676. ait_symbol_end : begin
  677. end;
  678. ait_instruction : begin
  679. AsmWriteLn(GetInstruction(hp));
  680. end;
  681. {$ifdef GDB}
  682. ait_stabn,
  683. ait_stabs,
  684. ait_force_line,
  685. ait_stab_function_name : ;
  686. {$endif GDB}
  687. ait_cut : begin
  688. { only reset buffer if nothing has changed }
  689. if AsmSize=AsmStartSize then
  690. AsmClear
  691. else
  692. begin
  693. {
  694. if LasTSec<>sec_none then
  695. AsmWriteLn('_'+target_asm.secnames[LasTSec]+#9#9'ENDS');
  696. AsmLn;
  697. }
  698. AsmWriteLn(#9'END');
  699. AsmClose;
  700. DoAssemble;
  701. AsmCreate(tai_cut(hp).place);
  702. end;
  703. { avoid empty files }
  704. while assigned(hp.next) and (tai(hp.next).typ in [ait_cut,ait_section,ait_comment]) do
  705. begin
  706. if tai(hp.next).typ=ait_section then
  707. begin
  708. lasTSec:=tai_section(hp.next).sec;
  709. end;
  710. hp:=tai(hp.next);
  711. end;
  712. WriteAsmFileHeader;
  713. if lasTSec<>sec_none then
  714. AsmWriteLn(#9+target_asm.secnames[lasTSec]);
  715. { AsmWriteLn('_'+target_asm.secnames[lasTSec]+#9#9+
  716. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  717. target_asm.secnames[lasTSec]+'''');
  718. }
  719. AsmStartSize:=AsmSize;
  720. end;
  721. ait_marker :
  722. begin
  723. if tai_marker(hp).kind=InlineStart then
  724. inc(InlineLevel)
  725. else if tai_marker(hp).kind=InlineEnd then
  726. dec(InlineLevel);
  727. end;
  728. else
  729. internalerror(10000);
  730. end;
  731. hp:=tai(hp.next);
  732. end;
  733. end;
  734. var
  735. currentasmlist : TExternalAssembler;
  736. procedure writeexternal(p:tnamedindexitem;arg:pointer);
  737. var
  738. s:string;
  739. begin
  740. if tasmsymbol(p).defbind=AB_EXTERNAL then
  741. begin
  742. {currentasmlist.AsmWriteln(#9'IMPORT'#9+p.name);}
  743. s:= p.name;
  744. case tasmsymbol(p).typ of
  745. AT_FUNCTION:
  746. begin
  747. if ReplaceForbiddenChars(s) then
  748. begin
  749. currentasmlist.AsmWriteLn(#9'IMPORT'#9'.'+s+' <= ''.'+p.name+'[PR]''');
  750. currentasmlist.AsmWriteLn(#9'IMPORT'#9+s+' <= '''+p.name+'[DS]''');
  751. end
  752. else
  753. begin
  754. currentasmlist.AsmWriteLn(#9'IMPORT'#9'.'+s+'[PR]');
  755. currentasmlist.AsmWriteLn(#9'IMPORT'#9+s+'[DS]');
  756. end;
  757. currentasmlist.AsmWriteLn(#9'TOC');
  758. currentasmlist.AsmWriteLn(#9'TC'#9+s+'[TC],'+s+'[DS]');
  759. end
  760. else
  761. begin
  762. if ReplaceForbiddenChars(s) then
  763. currentasmlist.AsmWriteLn(#9'IMPORT'#9+s+' <= '''+p.name+'''')
  764. else
  765. currentasmlist.AsmWriteLn(#9'IMPORT'#9+s);
  766. end;
  767. end;
  768. end;
  769. end;
  770. procedure TPPCMPWAssembler.WriteExternals;
  771. begin
  772. currentasmlist:=self;
  773. objectlibrary.symbolsearch.foreach_static({$ifdef fpcprocvar}@{$endif}writeexternal,nil);
  774. end;
  775. function TPPCMPWAssembler.DoAssemble : boolean;
  776. var f : file;
  777. begin
  778. DoAssemble:=Inherited DoAssemble;
  779. (*
  780. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  781. if (aktoutputformat = as_i386_masm) then
  782. begin
  783. if not(cs_asm_extern in aktglobalswitches) then
  784. begin
  785. if Not FileExists(objfile) and
  786. FileExists(ForceExtension(objfile,'.obj')) then
  787. begin
  788. Assign(F,ForceExtension(objfile,'.obj'));
  789. Rename(F,objfile);
  790. end;
  791. end
  792. else
  793. AsmRes.AddAsmCommand('mv',ForceExtension(objfile,'.obj')+' '+objfile,objfile);
  794. end;
  795. *)
  796. end;
  797. procedure TPPCMPWAssembler.WriteAsmFileHeader;
  798. begin
  799. (*
  800. AsmWriteLn(#9'.386p');
  801. { masm 6.11 does not seem to like LOCALS PM }
  802. if (aktoutputformat = as_i386_tasm) then
  803. begin
  804. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  805. end;
  806. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  807. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  808. AsmLn;
  809. *)
  810. AsmWriteLn(#9'STRING ASIS'); {Interpret strings just to be the content between the quotes.}
  811. AsmLn;
  812. end;
  813. procedure TPPCMPWAssembler.WriteAsmList;
  814. begin
  815. {$ifdef EXTDEBUG}
  816. if assigned(current_module.mainsource) then
  817. comment(v_info,'Start writing MPW-styled assembler output for '+current_module.mainsource^);
  818. {$endif}
  819. LasTSec:=sec_none;
  820. WriteAsmFileHeader;
  821. WriteExternals;
  822. { PowerPC MPW ASM doesn't support stabs, as we now.
  823. WriteTree(debuglist);}
  824. WriteTree(codesegment);
  825. WriteTree(datasegment);
  826. WriteTree(consts);
  827. WriteTree(rttilist);
  828. WriteTree(resourcestringlist);
  829. WriteTree(bsssegment);
  830. AsmWriteLn(#9'END');
  831. AsmLn;
  832. {$ifdef EXTDEBUG}
  833. if assigned(current_module.mainsource) then
  834. comment(v_info,'Done writing MPW-styled assembler output for '+current_module.mainsource^);
  835. {$endif EXTDEBUG}
  836. end;
  837. {*****************************************************************************
  838. Initialize
  839. *****************************************************************************}
  840. const
  841. as_powerpc_mpw_info : tasminfo =
  842. (
  843. id : as_powerpc_mpw;
  844. idtxt : 'MPW';
  845. asmbin : 'PPCAsm';
  846. asmcmd : '';
  847. supported_target : system_any; { what should I write here ?? }
  848. outputbinary: false;
  849. allowdirect : true;
  850. needar : true;
  851. labelprefix_only_inside_procedure : true;
  852. labelprefix : '@@';
  853. comment : '; ';
  854. secnames : ('',
  855. 'CSECT','CSECT','CSECT', {TODO: Perhaps use other section types.}
  856. '','','','','','',
  857. '','','')
  858. );
  859. initialization
  860. RegisterAssembler(as_powerpc_mpw_info,TPPCMPWAssembler);
  861. end.
  862. {
  863. $Log$
  864. Revision 1.7 2002-10-02 22:14:15 florian
  865. * improve function imports
  866. Revision 1.6 2002/09/27 21:09:49 florian
  867. + readed because previous version was broken
  868. Revision 1.2 2002/08/31 12:43:31 florian
  869. * ppc compilation fixed
  870. Revision 1.1 2002/08/20 21:40:44 florian
  871. + target macos for ppc added
  872. + frame work for mpw assembler output
  873. }