agppcmpw.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. function getreferencestring(var ref : treference) : string;
  122. var
  123. s : string;
  124. begin
  125. with ref do
  126. begin
  127. inc(offset,offsetfixup);
  128. if (symaddr <> refs_full) then
  129. InternalError(2002110301)
  130. else if ((offset < -32768) or (offset > 32767)) then
  131. InternalError(19991);
  132. if assigned(symbol) then
  133. begin
  134. s:= symbol.name;
  135. ReplaceForbiddenChars(s);
  136. {if symbol.typ = AT_FUNCTION then
  137. ;}
  138. s:= s+'[TC]' {ref to TOC entry }
  139. end
  140. else
  141. s:= '';
  142. if offset<0 then
  143. s:=s+tostr(offset)
  144. else
  145. if (offset>0) then
  146. begin
  147. if assigned(symbol) then
  148. s:=s+'+'+tostr(offset)
  149. else
  150. s:=s+tostr(offset);
  151. end;
  152. if (index=R_NO) and (base<>R_NO) then
  153. begin
  154. if offset=0 then
  155. if not assigned(symbol) then
  156. s:=s+'0';
  157. s:=s+'('+mpw_reg2str[base]+')'
  158. end
  159. else if (index<>R_NO) and (base<>R_NO) and (offset=0) then
  160. s:=s+mpw_reg2str[base]+','+mpw_reg2str[index]
  161. else if ((index<>R_NO) or (base<>R_NO)) then
  162. internalerror(19992);
  163. end;
  164. getreferencestring:=s;
  165. end;
  166. function getopstr_jmp(const o:toper) : string;
  167. var
  168. hs : string;
  169. begin
  170. case o.typ of
  171. top_reg :
  172. getopstr_jmp:=mpw_reg2str[o.reg];
  173. { no top_ref jumping for powerpc }
  174. top_const :
  175. getopstr_jmp:=tostr(o.val);
  176. top_symbol :
  177. begin
  178. hs:=o.sym.name;
  179. ReplaceForbiddenChars(hs);
  180. if o.symofs>0 then
  181. hs:=hs+'+'+tostr(o.symofs)
  182. else
  183. if o.symofs<0 then
  184. hs:=hs+tostr(o.symofs);
  185. getopstr_jmp:=hs;
  186. end;
  187. top_none:
  188. getopstr_jmp:='';
  189. else
  190. {$ifndef testing}
  191. internalerror(2002070603);
  192. {$else testing}
  193. begin
  194. writeln('internalerror 10001');
  195. halt(1);
  196. end;
  197. {$endif testing}
  198. end;
  199. end;
  200. function getopstr(const o:toper) : string;
  201. var
  202. hs : string;
  203. begin
  204. case o.typ of
  205. top_reg:
  206. getopstr:=mpw_reg2str[o.reg];
  207. { no top_ref jumping for powerpc }
  208. top_const:
  209. getopstr:=tostr(longint(o.val));
  210. top_ref:
  211. getopstr:=getreferencestring(o.ref^);
  212. top_symbol:
  213. begin
  214. hs:=o.sym.name;
  215. ReplaceForbiddenChars(hs);
  216. if o.symofs>0 then
  217. hs:=hs+'+'+tostr(o.symofs)
  218. else
  219. if o.symofs<0 then
  220. hs:=hs+tostr(o.symofs);
  221. getopstr:=hs;
  222. end;
  223. else
  224. {$ifndef testing}
  225. internalerror(2002070604);
  226. {$else testing}
  227. begin
  228. writeln('internalerror 10001');
  229. halt(1);
  230. end;
  231. {$endif testing}
  232. end;
  233. end;
  234. function branchmode(o: tasmop): string[4];
  235. var tempstr: string[4];
  236. begin
  237. tempstr := '';
  238. case o of
  239. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  240. A_BCLR,A_BCLRL: tempstr := 'lr';
  241. end;
  242. case o of
  243. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  244. end;
  245. case o of
  246. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  247. end;
  248. branchmode := tempstr;
  249. end;
  250. function cond2str(op: tasmop; c: tasmcond): string;
  251. { note: no checking is performed whether the given combination of }
  252. { conditions is valid }
  253. var tempstr: string;
  254. begin
  255. tempstr:=#9;
  256. case c.simple of
  257. false: cond2str := tempstr+op2str[op]+#9+tostr(c.bo)+','+
  258. tostr(c.bi);
  259. true:
  260. if (op >= A_B) and (op <= A_BCLRL) then
  261. case c.cond of
  262. { unconditional branch }
  263. C_NONE:
  264. cond2str := tempstr+op2str[op];
  265. { bdnzt etc }
  266. else
  267. begin
  268. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  269. branchmode(op)+#9;
  270. case c.cond of
  271. C_LT..C_NU:
  272. cond2str := tempstr+mpw_reg2str[c.cr];
  273. C_T..C_DZF:
  274. cond2str := tempstr+tostr(c.crbit);
  275. end;
  276. end;
  277. end
  278. { we have a trap instruction }
  279. else
  280. begin
  281. internalerror(2002070601);
  282. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  283. { case tempstr := 'tw';}
  284. end;
  285. end;
  286. end;
  287. Function GetInstruction(hp : tai):string; {CHANGED from method to proc}
  288. var op: TAsmOp;
  289. s: string;
  290. i: byte;
  291. sep: string[3];
  292. begin
  293. op:=taicpu(hp).opcode;
  294. if is_calljmp(op) then
  295. begin
  296. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  297. case op of
  298. A_B,A_BA,A_BLA:
  299. s:=#9+op2str[op]+#9;
  300. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  301. s:=#9+op2str[op];
  302. A_BL:
  303. s:=#9+op2str[op]+#9'.';
  304. else
  305. s:=cond2str(op,taicpu(hp).condition)+',';
  306. end;
  307. if (taicpu(hp).oper[0].typ <> top_none) then
  308. s:=s+getopstr_jmp(taicpu(hp).oper[0]);
  309. if op=A_BL then
  310. s:=s+'[PR]';
  311. end
  312. else
  313. { process operands }
  314. begin
  315. case op of
  316. A_MFSPR:
  317. case taicpu(hp).oper[1].reg of
  318. R_CR:
  319. begin
  320. op:=A_MFCR;
  321. taicpu(hp).ops:=1;
  322. end;
  323. R_LR:
  324. begin
  325. op:=A_MFLR;
  326. taicpu(hp).ops:=1;
  327. end;
  328. else
  329. internalerror(2002100701);
  330. end;
  331. A_MTSPR:
  332. case taicpu(hp).oper[1].reg of
  333. R_CR:
  334. begin
  335. op:=A_MTCR;
  336. taicpu(hp).ops:=1;
  337. end;
  338. R_LR:
  339. begin
  340. op:=A_MTLR;
  341. taicpu(hp).ops:=1;
  342. end;
  343. else
  344. internalerror(2002100701);
  345. end;
  346. end;
  347. s:=#9+op2str[op];
  348. if taicpu(hp).ops<>0 then
  349. begin
  350. sep:=#9;
  351. for i:=0 to taicpu(hp).ops-1 do
  352. begin
  353. s:=s+sep+getopstr(taicpu(hp).oper[i]);
  354. sep:=',';
  355. end;
  356. end;
  357. end;
  358. GetInstruction:=s;
  359. end;
  360. {*** Until here is copyed from agppcgas.pp. ***}
  361. function single2str(d : single) : string;
  362. var
  363. hs : string;
  364. p : byte;
  365. begin
  366. str(d,hs);
  367. { nasm expects a lowercase e }
  368. p:=pos('E',hs);
  369. if p>0 then
  370. hs[p]:='e';
  371. p:=pos('+',hs);
  372. if p>0 then
  373. delete(hs,p,1);
  374. single2str:=lower(hs);
  375. end;
  376. function double2str(d : double) : string;
  377. var
  378. hs : string;
  379. p : byte;
  380. begin
  381. str(d,hs);
  382. { nasm expects a lowercase e }
  383. p:=pos('E',hs);
  384. if p>0 then
  385. hs[p]:='e';
  386. p:=pos('+',hs);
  387. if p>0 then
  388. delete(hs,p,1);
  389. double2str:=lower(hs);
  390. end;
  391. function fixline(s:string):string;
  392. {
  393. return s with all leading and ending spaces and tabs removed
  394. }
  395. var
  396. i,j,k : longint;
  397. begin
  398. i:=length(s);
  399. while (i>0) and (s[i] in [#9,' ']) do
  400. dec(i);
  401. j:=1;
  402. while (j<i) and (s[j] in [#9,' ']) do
  403. inc(j);
  404. for k:=j to i do
  405. if s[k] in [#0..#31,#127..#255] then
  406. s[k]:='.';
  407. fixline:=Copy(s,j,i-j+1);
  408. end;
  409. {****************************************************************************
  410. PowerPC MPW Assembler
  411. ****************************************************************************}
  412. var
  413. LasTSec : TSection;
  414. lastfileinfo : tfileposinfo;
  415. infile,
  416. lastinfile : tinputfile;
  417. const
  418. ait_const2str:array[ait_const_32bit..ait_const_8bit] of string[8]=
  419. (#9'dc.l'#9,#9'dc.w'#9,#9'dc.b'#9);
  420. Function PadTabs(const p:string;addch:char):string;
  421. var
  422. s : string;
  423. i : longint;
  424. begin
  425. i:=length(p);
  426. if addch<>#0 then
  427. begin
  428. inc(i);
  429. s:=p+addch;
  430. end
  431. else
  432. s:=p;
  433. if i<8 then
  434. PadTabs:=s+#9#9
  435. else
  436. PadTabs:=s+#9;
  437. end;
  438. procedure TPPCMPWAssembler.WriteTree(p:TAAsmoutput);
  439. var
  440. s,
  441. prefix,
  442. suffix : string;
  443. hp : tai;
  444. hp1 : tailineinfo;
  445. counter,
  446. lines,
  447. InlineLevel : longint;
  448. i,j,l : longint;
  449. consttyp : taitype;
  450. found,
  451. do_line,DoNotSplitLine,
  452. quoted : boolean;
  453. sep : char;
  454. replaced : boolean;
  455. begin
  456. if not assigned(p) then
  457. exit;
  458. { lineinfo is only needed for codesegment (PFV) }
  459. do_line:=((cs_asm_source in aktglobalswitches) or
  460. (cs_lineinfo in aktmoduleswitches))
  461. and (p=codesegment);
  462. InlineLevel:=0;
  463. DoNotSplitLine:=false;
  464. hp:=tai(p.first);
  465. while assigned(hp) do
  466. begin
  467. if do_line and not(hp.typ in SkipLineInfo) and
  468. not DoNotSplitLine then
  469. begin
  470. hp1 := hp as tailineinfo;
  471. { load infile }
  472. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  473. begin
  474. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  475. if assigned(infile) then
  476. begin
  477. { open only if needed !! }
  478. if (cs_asm_source in aktglobalswitches) then
  479. infile.open;
  480. end;
  481. { avoid unnecessary reopens of the same file !! }
  482. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  483. { be sure to change line !! }
  484. lastfileinfo.line:=-1;
  485. end;
  486. { write source }
  487. if (cs_asm_source in aktglobalswitches) and
  488. assigned(infile) then
  489. begin
  490. if (infile<>lastinfile) then
  491. begin
  492. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  493. if assigned(lastinfile) then
  494. lastinfile.close;
  495. end;
  496. if (hp1.fileinfo.line<>lastfileinfo.line) and
  497. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  498. begin
  499. if (hp1.fileinfo.line<>0) and
  500. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  501. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  502. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  503. { set it to a negative value !
  504. to make that is has been read already !! PM }
  505. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  506. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  507. end;
  508. end;
  509. lastfileinfo:=hp1.fileinfo;
  510. lastinfile:=infile;
  511. end;
  512. DoNotSplitLine:=false;
  513. case hp.typ of
  514. ait_comment:
  515. begin
  516. AsmWrite(target_asm.comment);
  517. AsmWritePChar(tai_comment(hp).str);
  518. AsmLn;
  519. end;
  520. ait_regalloc,
  521. ait_tempalloc:
  522. ;
  523. ait_section:
  524. begin
  525. {if LasTSec<>sec_none then
  526. AsmWriteLn('_'+target_asm.secnames[LasTSec]+#9#9'ENDS');}
  527. if tai_section(hp).sec<>sec_none then
  528. begin
  529. AsmLn;
  530. AsmWriteLn(#9+target_asm.secnames[tai_section(hp).sec]){+#9#9+
  531. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  532. target_asm.secnames[tai_section(hp).sec]+'''');}
  533. end;
  534. LasTSec:=tai_section(hp).sec;
  535. end;
  536. ait_align:
  537. begin
  538. case tai_align(hp).aligntype of
  539. 1:AsmWriteLn(#9'align 0');
  540. 2:AsmWriteLn(#9'align 1');
  541. 4:AsmWriteLn(#9'align 2');
  542. otherwise internalerror(2002110302);
  543. end;
  544. end;
  545. ait_datablock:
  546. begin
  547. s:= tai_datablock(hp).sym.name;
  548. replaced:= ReplaceForbiddenChars(s);
  549. if tai_datablock(hp).is_global then
  550. if replaced then
  551. AsmWriteLn(#9'export'#9+s+' => '''+tai_datablock(hp).sym.name+'''')
  552. else
  553. AsmWriteLn(#9'export'#9+s);
  554. AsmWriteLn(#9'csect'#9+s+'[TC]');
  555. AsmWriteLn(PadTabs(s+':',#0)+'ds.b '+tostr(tai_datablock(hp).size));
  556. {TODO: ? PadTabs(s,#0) }
  557. end;
  558. ait_const_32bit,
  559. ait_const_8bit,
  560. ait_const_16bit :
  561. begin
  562. AsmWrite(ait_const2str[hp.typ]+tostr(tai_const(hp).value));
  563. consttyp:=hp.typ;
  564. l:=0;
  565. repeat
  566. found:=(not (tai(hp.next)=nil)) and (tai(hp.next).typ=consttyp);
  567. if found then
  568. begin
  569. hp:=tai(hp.next);
  570. s:=','+tostr(tai_const(hp).value);
  571. AsmWrite(s);
  572. inc(l,length(s));
  573. end;
  574. until (not found) or (l>line_length);
  575. AsmLn;
  576. end;
  577. ait_const_symbol:
  578. begin
  579. (*
  580. AsmWriteLn(#9#9'dd'#9'offset '+tai_const_symbol(hp).sym.name);
  581. if tai_const_symbol(hp).offset>0 then
  582. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  583. else if tai_const_symbol(hp).offset<0 then
  584. AsmWrite(tostr(tai_const_symbol(hp).offset));
  585. AsmLn;
  586. *)
  587. s:= tai_const_symbol(hp).sym.name;
  588. ReplaceForbiddenChars(s);
  589. if tai_const_symbol(hp).sym.typ = AT_FUNCTION then
  590. AsmWriteLn(#9'dc.l'#9'.'+ s +'[PR]')
  591. else
  592. AsmWriteLn(#9'dc.l'#9+ s);
  593. (* TODO: the following might need to be included. Temporaily we
  594. generate an error
  595. if tai_const_symbol(hp).offset>0 then
  596. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  597. else if tai_const_symbol(hp).offset<0 then
  598. AsmWrite(tostr(tai_const_symbol(hp).offset));
  599. *)
  600. if tai_const_symbol(hp).offset <> 0 then
  601. InternalError(2002110101);
  602. AsmLn;
  603. end;
  604. ait_real_32bit:
  605. AsmWriteLn(#9'dc.l'#9'"'+single2str(tai_real_32bit(hp).value)+'"');
  606. ait_real_64bit:
  607. AsmWriteLn(#9'dc.d'#9'"'+double2str(tai_real_64bit(hp).value)+'"');
  608. ait_string:
  609. begin
  610. {NOTE When a single quote char is encountered, it is
  611. replaced with a numeric ascii value. It could also
  612. have been replaced with the escape seq of double quotes.}
  613. counter := 0;
  614. lines := tai_string(hp).len div line_length;
  615. { separate lines in different parts }
  616. if tai_string(hp).len > 0 then
  617. Begin
  618. for j := 0 to lines-1 do
  619. begin
  620. AsmWrite(#9'dc.b'#9);
  621. quoted:=false;
  622. for i:=counter to counter+line_length do
  623. begin
  624. { it is an ascii character. }
  625. if (ord(tai_string(hp).str[i])>31) and
  626. (ord(tai_string(hp).str[i])<128) and
  627. (tai_string(hp).str[i]<>'''') then
  628. begin
  629. if not(quoted) then
  630. begin
  631. if i>counter then
  632. AsmWrite(',');
  633. AsmWrite('''');
  634. end;
  635. AsmWrite(tai_string(hp).str[i]);
  636. quoted:=true;
  637. end { if > 31 and < 128 and ord('"') }
  638. else
  639. begin
  640. if quoted then
  641. AsmWrite('''');
  642. if i>counter then
  643. AsmWrite(',');
  644. quoted:=false;
  645. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  646. end;
  647. end; { end for i:=0 to... }
  648. if quoted then AsmWrite('''');
  649. AsmWrite(target_info.newline);
  650. counter := counter+line_length;
  651. end; { end for j:=0 ... }
  652. { do last line of lines }
  653. AsmWrite(#9'dc.b'#9);
  654. quoted:=false;
  655. for i:=counter to tai_string(hp).len-1 do
  656. begin
  657. { it is an ascii character. }
  658. if (ord(tai_string(hp).str[i])>31) and
  659. (ord(tai_string(hp).str[i])<128) and
  660. (tai_string(hp).str[i]<>'''') then
  661. begin
  662. if not(quoted) then
  663. begin
  664. if i>counter then
  665. AsmWrite(',');
  666. AsmWrite('''');
  667. end;
  668. AsmWrite(tai_string(hp).str[i]);
  669. quoted:=true;
  670. end { if > 31 and < 128 and " }
  671. else
  672. begin
  673. if quoted then
  674. AsmWrite('''');
  675. if i>counter then
  676. AsmWrite(',');
  677. quoted:=false;
  678. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  679. end;
  680. end; { end for i:=0 to... }
  681. if quoted then
  682. AsmWrite('''');
  683. end;
  684. AsmLn;
  685. end;
  686. ait_label:
  687. begin
  688. if tai_label(hp).l.is_used then
  689. begin
  690. s:= tai_label(hp).l.name;
  691. ReplaceForbiddenChars(s);
  692. if s[1] <> '@' then
  693. AsmWriteLn(#9'csect'#9+s+'[TC]');
  694. AsmWrite(s);
  695. {if assigned(hp.next) and not(tai(hp.next).typ in
  696. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  697. ait_const_symbol,ait_const_rva,
  698. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  699. AsmWriteLn(':')
  700. else
  701. DoNotSplitLine:=true;}
  702. AsmWriteLn(':');
  703. end;
  704. end;
  705. ait_direct:
  706. begin
  707. AsmWritePChar(tai_direct(hp).str);
  708. AsmLn;
  709. end;
  710. ait_symbol:
  711. begin
  712. {Two adjacent symbols, which only differ in case, is to be treated as
  713. a single symbol, due to the case insensitivity of PPCAsm.}
  714. if (not (tai(hp.next)=nil)) and (tai(hp.next).typ=ait_stab_function_name) then
  715. if (not (tai(hp.next.next)=nil)) and (tai(hp.next.next).typ=ait_symbol) then
  716. if CompareText(tai_label(hp).l.name,tai_label(hp.next.next).l.name) = 0 then
  717. hp:=tai(hp.next.next);
  718. s:= tai_label(hp).l.name;
  719. replaced:= ReplaceForbiddenChars(s);
  720. if tai_label(hp).l.typ=AT_FUNCTION then
  721. begin
  722. if replaced then
  723. begin
  724. AsmWriteLn(#9'export'#9'.'+s+'[PR] => ''.'+tai_symbol(hp).sym.name+'[PR]''');
  725. AsmWriteLn(#9'export'#9+s+'[DS] => '''+tai_symbol(hp).sym.name+'[DS]''');
  726. end
  727. else
  728. begin
  729. AsmWriteLn(#9'export'#9'.'+s+'[PR]');
  730. AsmWriteLn(#9'export'#9+s+'[DS]');
  731. end;
  732. {Entry in transition vector: }
  733. AsmWriteLn(#9'csect'#9+s+'[DS]');
  734. AsmWriteLn(#9'dc.l'#9'.'+s);
  735. AsmWriteln(#9'dc.l'#9'TOC[tc0]');
  736. {Entry in TOC: }
  737. AsmWriteLn(#9'toc');
  738. AsmWriteLn(#9'tc'#9+s+'[TC],'+s+'[DS]');
  739. {Start the section of the body of the proc: }
  740. AsmWriteLn(#9'csect'#9'.'+s+'[PR]');
  741. AsmWrite('.');
  742. AsmWrite(s);
  743. AsmWriteLn(':');
  744. end
  745. else
  746. begin
  747. if tai_symbol(hp).is_global then
  748. if replaced then
  749. AsmWriteLn(#9'export'#9+s+' => '''+tai_symbol(hp).sym.name+'''')
  750. else
  751. AsmWriteLn(#9'export'#9+s);
  752. AsmWriteLn(#9'csect'#9+s+'[TC]');
  753. AsmWrite(s);
  754. AsmWriteLn(':');
  755. end;
  756. end;
  757. ait_symbol_end:
  758. ;
  759. ait_instruction:
  760. AsmWriteLn(GetInstruction(hp));
  761. {$ifdef GDB}
  762. ait_stabn,
  763. ait_stabs,
  764. ait_force_line,
  765. ait_stab_function_name : ;
  766. {$endif GDB}
  767. ait_cut : begin
  768. { only reset buffer if nothing has changed }
  769. if AsmSize=AsmStartSize then
  770. AsmClear
  771. else
  772. begin
  773. {
  774. if LasTSec<>sec_none then
  775. AsmWriteLn('_'+target_asm.secnames[LasTSec]+#9#9'ends');
  776. AsmLn;
  777. }
  778. AsmWriteLn(#9'end');
  779. AsmClose;
  780. DoAssemble;
  781. AsmCreate(tai_cut(hp).place);
  782. end;
  783. { avoid empty files }
  784. while assigned(hp.next) and (tai(hp.next).typ in [ait_cut,ait_section,ait_comment]) do
  785. begin
  786. if tai(hp.next).typ=ait_section then
  787. begin
  788. lasTSec:=tai_section(hp.next).sec;
  789. end;
  790. hp:=tai(hp.next);
  791. end;
  792. WriteAsmFileHeader;
  793. if lasTSec<>sec_none then
  794. AsmWriteLn(#9+target_asm.secnames[lasTSec]);
  795. { AsmWriteLn('_'+target_asm.secnames[lasTSec]+#9#9+
  796. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  797. target_asm.secnames[lasTSec]+'''');
  798. }
  799. AsmStartSize:=AsmSize;
  800. end;
  801. ait_marker :
  802. begin
  803. if tai_marker(hp).kind=InlineStart then
  804. inc(InlineLevel)
  805. else if tai_marker(hp).kind=InlineEnd then
  806. dec(InlineLevel);
  807. end;
  808. else
  809. internalerror(2002110303);
  810. end;
  811. hp:=tai(hp.next);
  812. end;
  813. end;
  814. var
  815. currentasmlist : TExternalAssembler;
  816. procedure writeexternal(p:tnamedindexitem;arg:pointer);
  817. var
  818. s:string;
  819. begin
  820. if tasmsymbol(p).defbind=AB_EXTERNAL then
  821. begin
  822. {currentasmlist.AsmWriteln(#9'import'#9+p.name);}
  823. s:= p.name;
  824. case tasmsymbol(p).typ of
  825. AT_FUNCTION:
  826. begin
  827. if ReplaceForbiddenChars(s) then
  828. begin
  829. currentasmlist.AsmWriteLn(#9'import'#9'.'+s+'[PR] <= ''.'+p.name+'[PR]''');
  830. currentasmlist.AsmWriteLn(#9'import'#9+s+'[DS] <= '''+p.name+'[DS]''');
  831. end
  832. else
  833. begin
  834. currentasmlist.AsmWriteLn(#9'import'#9'.'+s+'[PR]');
  835. currentasmlist.AsmWriteLn(#9'import'#9+s+'[DS]');
  836. end;
  837. currentasmlist.AsmWriteLn(#9'toc');
  838. currentasmlist.AsmWriteLn(#9'tc'#9+s+'[TC],'+s+'[DS]');
  839. end
  840. else
  841. begin
  842. if ReplaceForbiddenChars(s) then
  843. currentasmlist.AsmWriteLn(#9'import'#9+s+' <= '''+p.name+'''')
  844. else
  845. currentasmlist.AsmWriteLn(#9'import'#9+s);
  846. end;
  847. end;
  848. end;
  849. end;
  850. procedure TPPCMPWAssembler.WriteExternals;
  851. begin
  852. currentasmlist:=self;
  853. objectlibrary.symbolsearch.foreach_static({$ifdef fpcprocvar}@{$endif}writeexternal,nil);
  854. end;
  855. function TPPCMPWAssembler.DoAssemble : boolean;
  856. var f : file;
  857. begin
  858. DoAssemble:=Inherited DoAssemble;
  859. (*
  860. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  861. if (aktoutputformat = as_i386_masm) then
  862. begin
  863. if not(cs_asm_extern in aktglobalswitches) then
  864. begin
  865. if Not FileExists(objfile) and
  866. FileExists(ForceExtension(objfile,'.obj')) then
  867. begin
  868. Assign(F,ForceExtension(objfile,'.obj'));
  869. Rename(F,objfile);
  870. end;
  871. end
  872. else
  873. AsmRes.AddAsmCommand('mv',ForceExtension(objfile,'.obj')+' '+objfile,objfile);
  874. end;
  875. *)
  876. end;
  877. procedure TPPCMPWAssembler.WriteAsmFileHeader;
  878. begin
  879. (*
  880. AsmWriteLn(#9'.386p');
  881. { masm 6.11 does not seem to like LOCALS PM }
  882. if (aktoutputformat = as_i386_tasm) then
  883. begin
  884. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  885. end;
  886. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  887. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  888. AsmLn;
  889. *)
  890. AsmWriteLn(#9'STRING ASIS'); {Interpret strings just to be the content between the quotes.}
  891. AsmLn;
  892. end;
  893. procedure TPPCMPWAssembler.WriteAsmList;
  894. begin
  895. {$ifdef EXTDEBUG}
  896. if assigned(current_module.mainsource) then
  897. comment(v_info,'Start writing MPW-styled assembler output for '+current_module.mainsource^);
  898. {$endif}
  899. LasTSec:=sec_none;
  900. WriteAsmFileHeader;
  901. WriteExternals;
  902. { PowerPC MPW ASM doesn't support stabs, as we now.
  903. WriteTree(debuglist);}
  904. WriteTree(codesegment);
  905. WriteTree(datasegment);
  906. WriteTree(consts);
  907. WriteTree(rttilist);
  908. WriteTree(resourcestringlist);
  909. WriteTree(bsssegment);
  910. AsmWriteLn(#9'end');
  911. AsmLn;
  912. {$ifdef EXTDEBUG}
  913. if assigned(current_module.mainsource) then
  914. comment(v_info,'Done writing MPW-styled assembler output for '+current_module.mainsource^);
  915. {$endif EXTDEBUG}
  916. end;
  917. {*****************************************************************************
  918. Initialize
  919. *****************************************************************************}
  920. const
  921. as_powerpc_mpw_info : tasminfo =
  922. (
  923. id : as_powerpc_mpw;
  924. idtxt : 'MPW';
  925. asmbin : 'PPCAsm';
  926. asmcmd : '';
  927. supported_target : system_any; { what should I write here ?? }
  928. outputbinary: false;
  929. allowdirect : true;
  930. needar : true;
  931. labelprefix_only_inside_procedure : true;
  932. labelprefix : '@';
  933. comment : '; ';
  934. secnames : ('',
  935. 'csect','csect [TC]','csect [TC]', {TODO: Perhaps use other section types.}
  936. '','','','','','',
  937. '','','')
  938. );
  939. initialization
  940. RegisterAssembler(as_powerpc_mpw_info,TPPCMPWAssembler);
  941. end.
  942. {
  943. $Log$
  944. Revision 1.15 2002-11-17 16:31:59 carl
  945. * memory optimization (3-4%) : cleanup of tai fields,
  946. cleanup of tdef and tsym fields.
  947. * make it work for m68k
  948. Revision 1.14 2002/11/07 15:50:23 jonas
  949. * fixed bctr(l) problems
  950. Revision 1.13 2002/11/04 18:24:53 olle
  951. * globals are located in TOC and relative r2, instead of absolute
  952. * symbols which only differs in case are treated as a single symbol
  953. + tai_const_symbol supported
  954. * only refs_full accepted
  955. Revision 1.12 2002/10/23 15:31:01 olle
  956. * branch b does not jump to dotted symbol now
  957. Revision 1.11 2002/10/19 23:52:40 olle
  958. * import directive changed
  959. Revision 1.10 2002/10/10 19:39:37 florian
  960. * changes from Olle to get simple programs compiled and assembled
  961. Revision 1.9 2002/10/07 21:19:53 florian
  962. * more mpw fixes
  963. Revision 1.8 2002/10/06 22:46:20 florian
  964. * fixed function exporting
  965. Revision 1.7 2002/10/02 22:14:15 florian
  966. * improve function imports
  967. Revision 1.6 2002/09/27 21:09:49 florian
  968. + readed because previous version was broken
  969. Revision 1.2 2002/08/31 12:43:31 florian
  970. * ppc compilation fixed
  971. Revision 1.1 2002/08/20 21:40:44 florian
  972. + target macos for ppc added
  973. + frame work for mpw assembler output
  974. }