agppcmpw.pas 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. case op of
  320. A_MFSPR:
  321. case taicpu(hp).oper[1].reg of
  322. R_CR:
  323. begin
  324. op:=A_MFCR;
  325. taicpu(hp).ops:=1;
  326. end;
  327. R_LR:
  328. begin
  329. op:=A_MFLR;
  330. taicpu(hp).ops:=1;
  331. end;
  332. else
  333. internalerror(2002100701);
  334. end;
  335. A_MTSPR:
  336. case taicpu(hp).oper[1].reg of
  337. R_CR:
  338. begin
  339. op:=A_MTCR;
  340. taicpu(hp).ops:=1;
  341. end;
  342. R_LR:
  343. begin
  344. op:=A_MTLR;
  345. taicpu(hp).ops:=1;
  346. end;
  347. else
  348. internalerror(2002100701);
  349. end;
  350. end;
  351. s:=#9+op2str[op];
  352. if taicpu(hp).ops<>0 then
  353. begin
  354. sep:=#9;
  355. for i:=0 to taicpu(hp).ops-1 do
  356. begin
  357. s:=s+sep+getopstr(taicpu(hp).oper[i]);
  358. sep:=',';
  359. end;
  360. end;
  361. end;
  362. GetInstruction:=s;
  363. end;
  364. {*** Until here is copyed from agppcgas.pp. ***}
  365. function single2str(d : single) : string;
  366. var
  367. hs : string;
  368. p : byte;
  369. begin
  370. str(d,hs);
  371. { nasm expects a lowercase e }
  372. p:=pos('E',hs);
  373. if p>0 then
  374. hs[p]:='e';
  375. p:=pos('+',hs);
  376. if p>0 then
  377. delete(hs,p,1);
  378. single2str:=lower(hs);
  379. end;
  380. function double2str(d : double) : string;
  381. var
  382. hs : string;
  383. p : byte;
  384. begin
  385. str(d,hs);
  386. { nasm expects a lowercase e }
  387. p:=pos('E',hs);
  388. if p>0 then
  389. hs[p]:='e';
  390. p:=pos('+',hs);
  391. if p>0 then
  392. delete(hs,p,1);
  393. double2str:=lower(hs);
  394. end;
  395. function fixline(s:string):string;
  396. {
  397. return s with all leading and ending spaces and tabs removed
  398. }
  399. var
  400. i,j,k : longint;
  401. begin
  402. i:=length(s);
  403. while (i>0) and (s[i] in [#9,' ']) do
  404. dec(i);
  405. j:=1;
  406. while (j<i) and (s[j] in [#9,' ']) do
  407. inc(j);
  408. for k:=j to i do
  409. if s[k] in [#0..#31,#127..#255] then
  410. s[k]:='.';
  411. fixline:=Copy(s,j,i-j+1);
  412. end;
  413. {****************************************************************************
  414. PowerPC MPW Assembler
  415. ****************************************************************************}
  416. var
  417. LasTSec : TSection;
  418. lastfileinfo : tfileposinfo;
  419. infile,
  420. lastinfile : tinputfile;
  421. const
  422. ait_const2str:array[ait_const_32bit..ait_const_8bit] of string[8]=
  423. (#9'dc.l'#9,#9'dc.w'#9,#9'dc.b'#9);
  424. Function PadTabs(const p:string;addch:char):string;
  425. var
  426. s : string;
  427. i : longint;
  428. begin
  429. i:=length(p);
  430. if addch<>#0 then
  431. begin
  432. inc(i);
  433. s:=p+addch;
  434. end
  435. else
  436. s:=p;
  437. if i<8 then
  438. PadTabs:=s+#9#9
  439. else
  440. PadTabs:=s+#9;
  441. end;
  442. procedure TPPCMPWAssembler.WriteTree(p:TAAsmoutput);
  443. const
  444. nolinetai =[ait_label,
  445. ait_regalloc,ait_tempalloc,
  446. {$ifdef GDB}
  447. ait_stabn,ait_stabs,ait_stab_function_name,
  448. {$endif GDB}
  449. ait_cut,ait_marker,ait_align,ait_section];
  450. {TODO: Perhaps replace internalerror(10000) with something else}
  451. var
  452. s,
  453. prefix,
  454. suffix : string;
  455. hp : tai;
  456. counter,
  457. lines,
  458. InlineLevel : longint;
  459. i,j,l : longint;
  460. consttyp : taitype;
  461. found,
  462. do_line,DoNotSplitLine,
  463. quoted : boolean;
  464. sep : char;
  465. replaced : boolean;
  466. begin
  467. if not assigned(p) then
  468. exit;
  469. { lineinfo is only needed for codesegment (PFV) }
  470. do_line:=((cs_asm_source in aktglobalswitches) or
  471. (cs_lineinfo in aktmoduleswitches))
  472. and (p=codesegment);
  473. InlineLevel:=0;
  474. DoNotSplitLine:=false;
  475. hp:=tai(p.first);
  476. while assigned(hp) do
  477. begin
  478. if do_line and not(hp.typ in nolinetai) and
  479. not DoNotSplitLine then
  480. begin
  481. { load infile }
  482. if lastfileinfo.fileindex<>hp.fileinfo.fileindex then
  483. begin
  484. infile:=current_module.sourcefiles.get_file(hp.fileinfo.fileindex);
  485. if assigned(infile) then
  486. begin
  487. { open only if needed !! }
  488. if (cs_asm_source in aktglobalswitches) then
  489. infile.open;
  490. end;
  491. { avoid unnecessary reopens of the same file !! }
  492. lastfileinfo.fileindex:=hp.fileinfo.fileindex;
  493. { be sure to change line !! }
  494. lastfileinfo.line:=-1;
  495. end;
  496. { write source }
  497. if (cs_asm_source in aktglobalswitches) and
  498. assigned(infile) then
  499. begin
  500. if (infile<>lastinfile) then
  501. begin
  502. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  503. if assigned(lastinfile) then
  504. lastinfile.close;
  505. end;
  506. if (hp.fileinfo.line<>lastfileinfo.line) and
  507. ((hp.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  508. begin
  509. if (hp.fileinfo.line<>0) and
  510. ((infile.linebuf^[hp.fileinfo.line]>=0) or (InlineLevel>0)) then
  511. AsmWriteLn(target_asm.comment+'['+tostr(hp.fileinfo.line)+'] '+
  512. fixline(infile.GetLineStr(hp.fileinfo.line)));
  513. { set it to a negative value !
  514. to make that is has been read already !! PM }
  515. if (infile.linebuf^[hp.fileinfo.line]>=0) then
  516. infile.linebuf^[hp.fileinfo.line]:=-infile.linebuf^[hp.fileinfo.line]-1;
  517. end;
  518. end;
  519. lastfileinfo:=hp.fileinfo;
  520. lastinfile:=infile;
  521. end;
  522. DoNotSplitLine:=false;
  523. case hp.typ of
  524. ait_comment:
  525. begin
  526. AsmWrite(target_asm.comment);
  527. AsmWritePChar(tai_comment(hp).str);
  528. AsmLn;
  529. end;
  530. ait_regalloc,
  531. ait_tempalloc:
  532. ;
  533. ait_section:
  534. begin
  535. {if LasTSec<>sec_none then
  536. AsmWriteLn('_'+target_asm.secnames[LasTSec]+#9#9'ENDS');}
  537. if tai_section(hp).sec<>sec_none then
  538. begin
  539. AsmLn;
  540. AsmWriteLn(#9+target_asm.secnames[tai_section(hp).sec]){+#9#9+
  541. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  542. target_asm.secnames[tai_section(hp).sec]+'''');}
  543. end;
  544. LasTSec:=tai_section(hp).sec;
  545. end;
  546. ait_align:
  547. begin
  548. case tai_align(hp).aligntype of
  549. 1:AsmWriteLn(#9'align 0');
  550. 2:AsmWriteLn(#9'align 1');
  551. 4:AsmWriteLn(#9'align 2');
  552. otherwise internalerror(10000);
  553. end;
  554. end;
  555. ait_datablock:
  556. begin
  557. s:= tai_datablock(hp).sym.name;
  558. replaced:= ReplaceForbiddenChars(s);
  559. if tai_datablock(hp).is_global then
  560. if replaced then
  561. AsmWriteLn(#9'export'#9+s+' => '''+tai_datablock(hp).sym.name+'''')
  562. else
  563. AsmWriteLn(#9'export'#9+s);
  564. AsmWriteLn(PadTabs(s,#0)+'DS.B '+tostr(tai_datablock(hp).size));
  565. {TODO: ? PadTabs(s,#0) }
  566. end;
  567. ait_const_32bit,
  568. ait_const_8bit,
  569. ait_const_16bit :
  570. begin
  571. AsmWrite(ait_const2str[hp.typ]+tostr(tai_const(hp).value));
  572. consttyp:=hp.typ;
  573. l:=0;
  574. repeat
  575. found:=(not (tai(hp.next)=nil)) and (tai(hp.next).typ=consttyp);
  576. if found then
  577. begin
  578. hp:=tai(hp.next);
  579. s:=','+tostr(tai_const(hp).value);
  580. AsmWrite(s);
  581. inc(l,length(s));
  582. end;
  583. until (not found) or (l>line_length);
  584. AsmLn;
  585. end;
  586. ait_const_symbol:
  587. begin
  588. AsmWriteLn(#9#9'dd'#9'offset '+tai_const_symbol(hp).sym.name);
  589. if tai_const_symbol(hp).offset>0 then
  590. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  591. else if tai_const_symbol(hp).offset<0 then
  592. AsmWrite(tostr(tai_const_symbol(hp).offset));
  593. AsmLn;
  594. end;
  595. ait_real_32bit:
  596. AsmWriteLn(#9'dc.l'#9'"'+single2str(tai_real_32bit(hp).value)+'"');
  597. ait_real_64bit:
  598. AsmWriteLn(#9'dc.d'#9'"'+double2str(tai_real_64bit(hp).value)+'"');
  599. ait_string:
  600. begin
  601. {NOTE When a single quote char is encountered, it is
  602. replaced with a numeric ascii value. It could also
  603. have been replaced with the escape seq of double quotes.}
  604. counter := 0;
  605. lines := tai_string(hp).len div line_length;
  606. { separate lines in different parts }
  607. if tai_string(hp).len > 0 then
  608. Begin
  609. for j := 0 to lines-1 do
  610. begin
  611. AsmWrite(#9'dc.b'#9);
  612. quoted:=false;
  613. for i:=counter to counter+line_length do
  614. begin
  615. { it is an ascii character. }
  616. if (ord(tai_string(hp).str[i])>31) and
  617. (ord(tai_string(hp).str[i])<128) and
  618. (tai_string(hp).str[i]<>'''') then
  619. begin
  620. if not(quoted) then
  621. begin
  622. if i>counter then
  623. AsmWrite(',');
  624. AsmWrite('''');
  625. end;
  626. AsmWrite(tai_string(hp).str[i]);
  627. quoted:=true;
  628. end { if > 31 and < 128 and ord('"') }
  629. else
  630. begin
  631. if quoted then
  632. AsmWrite('''');
  633. if i>counter then
  634. AsmWrite(',');
  635. quoted:=false;
  636. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  637. end;
  638. end; { end for i:=0 to... }
  639. if quoted then AsmWrite('''');
  640. AsmWrite(target_info.newline);
  641. counter := counter+line_length;
  642. end; { end for j:=0 ... }
  643. { do last line of lines }
  644. AsmWrite(#9'dc.b'#9);
  645. quoted:=false;
  646. for i:=counter to tai_string(hp).len-1 do
  647. begin
  648. { it is an ascii character. }
  649. if (ord(tai_string(hp).str[i])>31) and
  650. (ord(tai_string(hp).str[i])<128) and
  651. (tai_string(hp).str[i]<>'''') then
  652. begin
  653. if not(quoted) then
  654. begin
  655. if i>counter then
  656. AsmWrite(',');
  657. AsmWrite('''');
  658. end;
  659. AsmWrite(tai_string(hp).str[i]);
  660. quoted:=true;
  661. end { if > 31 and < 128 and " }
  662. else
  663. begin
  664. if quoted then
  665. AsmWrite('''');
  666. if i>counter then
  667. AsmWrite(',');
  668. quoted:=false;
  669. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  670. end;
  671. end; { end for i:=0 to... }
  672. if quoted then
  673. AsmWrite('''');
  674. end;
  675. AsmLn;
  676. end;
  677. ait_label:
  678. begin
  679. if tai_label(hp).l.is_used then
  680. begin
  681. s:= tai_label(hp).l.name;
  682. ReplaceForbiddenChars(s);
  683. AsmWrite(s);
  684. {if assigned(hp.next) and not(tai(hp.next).typ in
  685. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  686. ait_const_symbol,ait_const_rva,
  687. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  688. AsmWriteLn(':')
  689. else
  690. DoNotSplitLine:=true;}
  691. AsmWriteLn(':');
  692. end;
  693. end;
  694. ait_direct:
  695. begin
  696. AsmWritePChar(tai_direct(hp).str);
  697. AsmLn;
  698. end;
  699. ait_symbol:
  700. begin
  701. s:= tai_label(hp).l.name;
  702. replaced:= ReplaceForbiddenChars(s);
  703. if tai_label(hp).l.typ=AT_FUNCTION then
  704. begin
  705. if replaced then
  706. begin
  707. AsmWriteLn(#9'export'#9'.'+s+'[PR] => ''.'+tai_symbol(hp).sym.name+'[PR]''');
  708. AsmWriteLn(#9'export'#9+s+'[DS] => '''+tai_symbol(hp).sym.name+'[DS]''');
  709. AsmWriteLn(#9'csect'#9'.'+s+'[PR]');
  710. end
  711. else
  712. begin
  713. AsmWriteLn(#9'export'#9'.'+s+'[PR]');
  714. AsmWriteLn(#9'export'#9+s+'[DS]');
  715. AsmWriteLn(#9'csect'#9'.'+s+'[PR]');
  716. end;
  717. AsmWriteLn(#9'toc');
  718. AsmWriteLn(#9'tc'#9+s+'[TC],'+s+'[DS]');
  719. AsmWrite('.');
  720. AsmWrite(s);
  721. AsmWriteLn(':');
  722. end
  723. else
  724. begin
  725. if tai_symbol(hp).is_global then
  726. if replaced then
  727. AsmWriteLn(#9'export'#9+s+' => '''+tai_symbol(hp).sym.name+'''')
  728. else
  729. AsmWriteLn(#9'export'#9+s);
  730. AsmWrite(s);
  731. AsmWriteLn(':');
  732. end;
  733. end;
  734. ait_symbol_end:
  735. ;
  736. ait_instruction:
  737. AsmWriteLn(GetInstruction(hp));
  738. {$ifdef GDB}
  739. ait_stabn,
  740. ait_stabs,
  741. ait_force_line,
  742. ait_stab_function_name : ;
  743. {$endif GDB}
  744. ait_cut : begin
  745. { only reset buffer if nothing has changed }
  746. if AsmSize=AsmStartSize then
  747. AsmClear
  748. else
  749. begin
  750. {
  751. if LasTSec<>sec_none then
  752. AsmWriteLn('_'+target_asm.secnames[LasTSec]+#9#9'ends');
  753. AsmLn;
  754. }
  755. AsmWriteLn(#9'end');
  756. AsmClose;
  757. DoAssemble;
  758. AsmCreate(tai_cut(hp).place);
  759. end;
  760. { avoid empty files }
  761. while assigned(hp.next) and (tai(hp.next).typ in [ait_cut,ait_section,ait_comment]) do
  762. begin
  763. if tai(hp.next).typ=ait_section then
  764. begin
  765. lasTSec:=tai_section(hp.next).sec;
  766. end;
  767. hp:=tai(hp.next);
  768. end;
  769. WriteAsmFileHeader;
  770. if lasTSec<>sec_none then
  771. AsmWriteLn(#9+target_asm.secnames[lasTSec]);
  772. { AsmWriteLn('_'+target_asm.secnames[lasTSec]+#9#9+
  773. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  774. target_asm.secnames[lasTSec]+'''');
  775. }
  776. AsmStartSize:=AsmSize;
  777. end;
  778. ait_marker :
  779. begin
  780. if tai_marker(hp).kind=InlineStart then
  781. inc(InlineLevel)
  782. else if tai_marker(hp).kind=InlineEnd then
  783. dec(InlineLevel);
  784. end;
  785. else
  786. internalerror(10000);
  787. end;
  788. hp:=tai(hp.next);
  789. end;
  790. end;
  791. var
  792. currentasmlist : TExternalAssembler;
  793. procedure writeexternal(p:tnamedindexitem;arg:pointer);
  794. var
  795. s:string;
  796. begin
  797. if tasmsymbol(p).defbind=AB_EXTERNAL then
  798. begin
  799. {currentasmlist.AsmWriteln(#9'import'#9+p.name);}
  800. s:= p.name;
  801. case tasmsymbol(p).typ of
  802. AT_FUNCTION:
  803. begin
  804. if ReplaceForbiddenChars(s) then
  805. begin
  806. currentasmlist.AsmWriteLn(#9'import'#9'.'+s+' <= ''.'+p.name+'[PR]''');
  807. currentasmlist.AsmWriteLn(#9'import'#9+s+' <= '''+p.name+'[DS]''');
  808. end
  809. else
  810. begin
  811. currentasmlist.AsmWriteLn(#9'import'#9'.'+s+'[PR]');
  812. currentasmlist.AsmWriteLn(#9'import'#9+s+'[DS]');
  813. end;
  814. currentasmlist.AsmWriteLn(#9'toc');
  815. currentasmlist.AsmWriteLn(#9'tc'#9+s+'[TC],'+s+'[DS]');
  816. end
  817. else
  818. begin
  819. if ReplaceForbiddenChars(s) then
  820. currentasmlist.AsmWriteLn(#9'import'#9+s+' <= '''+p.name+'''')
  821. else
  822. currentasmlist.AsmWriteLn(#9'import'#9+s);
  823. end;
  824. end;
  825. end;
  826. end;
  827. procedure TPPCMPWAssembler.WriteExternals;
  828. begin
  829. currentasmlist:=self;
  830. objectlibrary.symbolsearch.foreach_static({$ifdef fpcprocvar}@{$endif}writeexternal,nil);
  831. end;
  832. function TPPCMPWAssembler.DoAssemble : boolean;
  833. var f : file;
  834. begin
  835. DoAssemble:=Inherited DoAssemble;
  836. (*
  837. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  838. if (aktoutputformat = as_i386_masm) then
  839. begin
  840. if not(cs_asm_extern in aktglobalswitches) then
  841. begin
  842. if Not FileExists(objfile) and
  843. FileExists(ForceExtension(objfile,'.obj')) then
  844. begin
  845. Assign(F,ForceExtension(objfile,'.obj'));
  846. Rename(F,objfile);
  847. end;
  848. end
  849. else
  850. AsmRes.AddAsmCommand('mv',ForceExtension(objfile,'.obj')+' '+objfile,objfile);
  851. end;
  852. *)
  853. end;
  854. procedure TPPCMPWAssembler.WriteAsmFileHeader;
  855. begin
  856. (*
  857. AsmWriteLn(#9'.386p');
  858. { masm 6.11 does not seem to like LOCALS PM }
  859. if (aktoutputformat = as_i386_tasm) then
  860. begin
  861. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  862. end;
  863. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  864. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  865. AsmLn;
  866. *)
  867. AsmWriteLn(#9'STRING ASIS'); {Interpret strings just to be the content between the quotes.}
  868. AsmLn;
  869. end;
  870. procedure TPPCMPWAssembler.WriteAsmList;
  871. begin
  872. {$ifdef EXTDEBUG}
  873. if assigned(current_module.mainsource) then
  874. comment(v_info,'Start writing MPW-styled assembler output for '+current_module.mainsource^);
  875. {$endif}
  876. LasTSec:=sec_none;
  877. WriteAsmFileHeader;
  878. WriteExternals;
  879. { PowerPC MPW ASM doesn't support stabs, as we now.
  880. WriteTree(debuglist);}
  881. WriteTree(codesegment);
  882. WriteTree(datasegment);
  883. WriteTree(consts);
  884. WriteTree(rttilist);
  885. WriteTree(resourcestringlist);
  886. WriteTree(bsssegment);
  887. AsmWriteLn(#9'end');
  888. AsmLn;
  889. {$ifdef EXTDEBUG}
  890. if assigned(current_module.mainsource) then
  891. comment(v_info,'Done writing MPW-styled assembler output for '+current_module.mainsource^);
  892. {$endif EXTDEBUG}
  893. end;
  894. {*****************************************************************************
  895. Initialize
  896. *****************************************************************************}
  897. const
  898. as_powerpc_mpw_info : tasminfo =
  899. (
  900. id : as_powerpc_mpw;
  901. idtxt : 'MPW';
  902. asmbin : 'PPCAsm';
  903. asmcmd : '';
  904. supported_target : system_any; { what should I write here ?? }
  905. outputbinary: false;
  906. allowdirect : true;
  907. needar : true;
  908. labelprefix_only_inside_procedure : true;
  909. labelprefix : '@@';
  910. comment : '; ';
  911. secnames : ('',
  912. 'csect','csect','csect', {TODO: Perhaps use other section types.}
  913. '','','','','','',
  914. '','','')
  915. );
  916. initialization
  917. RegisterAssembler(as_powerpc_mpw_info,TPPCMPWAssembler);
  918. end.
  919. {
  920. $Log$
  921. Revision 1.9 2002-10-07 21:19:53 florian
  922. * more mpw fixes
  923. Revision 1.8 2002/10/06 22:46:20 florian
  924. * fixed function exporting
  925. Revision 1.7 2002/10/02 22:14:15 florian
  926. * improve function imports
  927. Revision 1.6 2002/09/27 21:09:49 florian
  928. + readed because previous version was broken
  929. Revision 1.2 2002/08/31 12:43:31 florian
  930. * ppc compilation fixed
  931. Revision 1.1 2002/08/20 21:40:44 florian
  932. + target macos for ppc added
  933. + frame work for mpw assembler output
  934. }