agppcmpw.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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 method 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_BA,A_BLA:
  306. s:=#9+op2str[op]+#9;
  307. A_B,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_B) or (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. end
  710. else
  711. begin
  712. AsmWriteLn(#9'export'#9'.'+s+'[PR]');
  713. AsmWriteLn(#9'export'#9+s+'[DS]');
  714. end;
  715. {Entry in transition vector: }
  716. AsmWriteLn(#9'csect'#9+s+'[DS]');
  717. AsmWriteLn(#9'dc.l'#9'.'+s);
  718. AsmWriteln(#9'dc.l'#9'TOC[tc0]');
  719. {Entry in TOC: }
  720. AsmWriteLn(#9'toc');
  721. AsmWriteLn(#9'tc'#9+s+'[TC],'+s+'[DS]');
  722. {Start the section of the body of the proc: }
  723. AsmWriteLn(#9'csect'#9'.'+s+'[PR]');
  724. AsmWrite('.');
  725. AsmWrite(s);
  726. AsmWriteLn(':');
  727. end
  728. else
  729. begin
  730. if tai_symbol(hp).is_global then
  731. if replaced then
  732. AsmWriteLn(#9'export'#9+s+' => '''+tai_symbol(hp).sym.name+'''')
  733. else
  734. AsmWriteLn(#9'export'#9+s);
  735. AsmWrite(s);
  736. AsmWriteLn(':');
  737. end;
  738. end;
  739. ait_symbol_end:
  740. ;
  741. ait_instruction:
  742. AsmWriteLn(GetInstruction(hp));
  743. {$ifdef GDB}
  744. ait_stabn,
  745. ait_stabs,
  746. ait_force_line,
  747. ait_stab_function_name : ;
  748. {$endif GDB}
  749. ait_cut : begin
  750. { only reset buffer if nothing has changed }
  751. if AsmSize=AsmStartSize then
  752. AsmClear
  753. else
  754. begin
  755. {
  756. if LasTSec<>sec_none then
  757. AsmWriteLn('_'+target_asm.secnames[LasTSec]+#9#9'ends');
  758. AsmLn;
  759. }
  760. AsmWriteLn(#9'end');
  761. AsmClose;
  762. DoAssemble;
  763. AsmCreate(tai_cut(hp).place);
  764. end;
  765. { avoid empty files }
  766. while assigned(hp.next) and (tai(hp.next).typ in [ait_cut,ait_section,ait_comment]) do
  767. begin
  768. if tai(hp.next).typ=ait_section then
  769. begin
  770. lasTSec:=tai_section(hp.next).sec;
  771. end;
  772. hp:=tai(hp.next);
  773. end;
  774. WriteAsmFileHeader;
  775. if lasTSec<>sec_none then
  776. AsmWriteLn(#9+target_asm.secnames[lasTSec]);
  777. { AsmWriteLn('_'+target_asm.secnames[lasTSec]+#9#9+
  778. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  779. target_asm.secnames[lasTSec]+'''');
  780. }
  781. AsmStartSize:=AsmSize;
  782. end;
  783. ait_marker :
  784. begin
  785. if tai_marker(hp).kind=InlineStart then
  786. inc(InlineLevel)
  787. else if tai_marker(hp).kind=InlineEnd then
  788. dec(InlineLevel);
  789. end;
  790. else
  791. internalerror(10000);
  792. end;
  793. hp:=tai(hp.next);
  794. end;
  795. end;
  796. var
  797. currentasmlist : TExternalAssembler;
  798. procedure writeexternal(p:tnamedindexitem;arg:pointer);
  799. var
  800. s:string;
  801. begin
  802. if tasmsymbol(p).defbind=AB_EXTERNAL then
  803. begin
  804. {currentasmlist.AsmWriteln(#9'import'#9+p.name);}
  805. s:= p.name;
  806. case tasmsymbol(p).typ of
  807. AT_FUNCTION:
  808. begin
  809. if ReplaceForbiddenChars(s) then
  810. begin
  811. currentasmlist.AsmWriteLn(#9'import'#9'.'+s+' <= ''.'+p.name+'[PR]''');
  812. currentasmlist.AsmWriteLn(#9'import'#9+s+' <= '''+p.name+'[DS]''');
  813. end
  814. else
  815. begin
  816. currentasmlist.AsmWriteLn(#9'import'#9'.'+s+'[PR]');
  817. currentasmlist.AsmWriteLn(#9'import'#9+s+'[DS]');
  818. end;
  819. currentasmlist.AsmWriteLn(#9'toc');
  820. currentasmlist.AsmWriteLn(#9'tc'#9+s+'[TC],'+s+'[DS]');
  821. end
  822. else
  823. begin
  824. if ReplaceForbiddenChars(s) then
  825. currentasmlist.AsmWriteLn(#9'import'#9+s+' <= '''+p.name+'''')
  826. else
  827. currentasmlist.AsmWriteLn(#9'import'#9+s);
  828. end;
  829. end;
  830. end;
  831. end;
  832. procedure TPPCMPWAssembler.WriteExternals;
  833. begin
  834. currentasmlist:=self;
  835. objectlibrary.symbolsearch.foreach_static({$ifdef fpcprocvar}@{$endif}writeexternal,nil);
  836. end;
  837. function TPPCMPWAssembler.DoAssemble : boolean;
  838. var f : file;
  839. begin
  840. DoAssemble:=Inherited DoAssemble;
  841. (*
  842. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  843. if (aktoutputformat = as_i386_masm) then
  844. begin
  845. if not(cs_asm_extern in aktglobalswitches) then
  846. begin
  847. if Not FileExists(objfile) and
  848. FileExists(ForceExtension(objfile,'.obj')) then
  849. begin
  850. Assign(F,ForceExtension(objfile,'.obj'));
  851. Rename(F,objfile);
  852. end;
  853. end
  854. else
  855. AsmRes.AddAsmCommand('mv',ForceExtension(objfile,'.obj')+' '+objfile,objfile);
  856. end;
  857. *)
  858. end;
  859. procedure TPPCMPWAssembler.WriteAsmFileHeader;
  860. begin
  861. (*
  862. AsmWriteLn(#9'.386p');
  863. { masm 6.11 does not seem to like LOCALS PM }
  864. if (aktoutputformat = as_i386_tasm) then
  865. begin
  866. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  867. end;
  868. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  869. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  870. AsmLn;
  871. *)
  872. AsmWriteLn(#9'STRING ASIS'); {Interpret strings just to be the content between the quotes.}
  873. AsmLn;
  874. end;
  875. procedure TPPCMPWAssembler.WriteAsmList;
  876. begin
  877. {$ifdef EXTDEBUG}
  878. if assigned(current_module.mainsource) then
  879. comment(v_info,'Start writing MPW-styled assembler output for '+current_module.mainsource^);
  880. {$endif}
  881. LasTSec:=sec_none;
  882. WriteAsmFileHeader;
  883. WriteExternals;
  884. { PowerPC MPW ASM doesn't support stabs, as we now.
  885. WriteTree(debuglist);}
  886. WriteTree(codesegment);
  887. WriteTree(datasegment);
  888. WriteTree(consts);
  889. WriteTree(rttilist);
  890. WriteTree(resourcestringlist);
  891. WriteTree(bsssegment);
  892. AsmWriteLn(#9'end');
  893. AsmLn;
  894. {$ifdef EXTDEBUG}
  895. if assigned(current_module.mainsource) then
  896. comment(v_info,'Done writing MPW-styled assembler output for '+current_module.mainsource^);
  897. {$endif EXTDEBUG}
  898. end;
  899. {*****************************************************************************
  900. Initialize
  901. *****************************************************************************}
  902. const
  903. as_powerpc_mpw_info : tasminfo =
  904. (
  905. id : as_powerpc_mpw;
  906. idtxt : 'MPW';
  907. asmbin : 'PPCAsm';
  908. asmcmd : '';
  909. supported_target : system_any; { what should I write here ?? }
  910. outputbinary: false;
  911. allowdirect : true;
  912. needar : true;
  913. labelprefix_only_inside_procedure : true;
  914. labelprefix : '@@';
  915. comment : '; ';
  916. secnames : ('',
  917. 'csect','csect','csect', {TODO: Perhaps use other section types.}
  918. '','','','','','',
  919. '','','')
  920. );
  921. initialization
  922. RegisterAssembler(as_powerpc_mpw_info,TPPCMPWAssembler);
  923. end.
  924. {
  925. $Log$
  926. Revision 1.10 2002-10-10 19:39:37 florian
  927. * changes from Olle to get simple programs compiled and assembled
  928. Revision 1.9 2002/10/07 21:19:53 florian
  929. * more mpw fixes
  930. Revision 1.8 2002/10/06 22:46:20 florian
  931. * fixed function exporting
  932. Revision 1.7 2002/10/02 22:14:15 florian
  933. * improve function imports
  934. Revision 1.6 2002/09/27 21:09:49 florian
  935. + readed because previous version was broken
  936. Revision 1.2 2002/08/31 12:43:31 florian
  937. * ppc compilation fixed
  938. Revision 1.1 2002/08/20 21:40:44 florian
  939. + target macos for ppc added
  940. + frame work for mpw assembler output
  941. }