agppcmpw.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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. private
  48. procedure GenProcedureHeader(var hp:tai);
  49. end;
  50. implementation
  51. uses
  52. {$ifdef delphi}
  53. sysutils,
  54. {$endif}
  55. cutils,globtype,globals,systems,cclasses,
  56. verbose,finput,fmodule,script,cpuinfo
  57. ;
  58. const
  59. line_length = 70;
  60. use_PR = false; {Whether internal references should be xxx[PR] }
  61. function ReplaceForbiddenChars(var s: string):Boolean;
  62. {Returns wheater a replacement has occured.}
  63. var
  64. i:Integer;
  65. {The dollar sign is not allowed in MPW PPCAsm}
  66. begin
  67. ReplaceForbiddenChars:=false;
  68. for i:=1 to Length(s) do
  69. if s[i]='$' then
  70. begin
  71. s[i]:='s';
  72. ReplaceForbiddenChars:=true;
  73. end;
  74. end;
  75. const
  76. {*** From here is copyed from agppcgas.pp, except where marked with CHANGED.
  77. Perhaps put in a third common file. ***}
  78. op2str : array[tasmop] of string[14] = ('<none>',
  79. 'add','add.','addo','addo.','addc','addc.','addco','addco.',
  80. 'adde','adde.','addeo','addeo.','addi','addic','addic.','addis',
  81. 'addme','addme.','addmeo','addmeo.','addze','addze.','addzeo',
  82. 'addzeo.','and','and.','andc','andc.','andi.','andis.','b',
  83. 'ba','bl','bla','bc','bca','bcl','bcla','bcctr','bcctrl','bclr',
  84. 'bclrl','cmp','cmpi','cmpl','cmpli','cntlzw','cntlzw.','crand',
  85. 'crandc','creqv','crnand','crnor','cror','crorc','crxor','dcba',
  86. 'dcbf','dcbi','dcbst','dcbt','divw','divw.','divwo','divwo.',
  87. 'divwu','divwu.','divwuo','divwuo.','eciwx','ecowx','eieio','eqv',
  88. 'eqv.','extsb','extsb.','extsh','extsh.','fabs','fabs.','fadd',
  89. 'fadd.','fadds','fadds.','fcmpo','fcmpu','fctiw','fctw.','fctwz',
  90. 'fctwz.','fdiv','fdiv.','fdivs','fdivs.','fmadd','fmadd.','fmadds',
  91. 'fmadds.','fmr','fmsub','fmsub.','fmsubs','fmsubs.','fmul','fmul.',
  92. 'fmuls','fmuls.','fnabs','fnabs.','fneg','fneg.','fnmadd',
  93. 'fnmadd.','fnmadds','fnmadds.','fnmsub','fnmsub.','fnmsubs',
  94. 'fnmsubs.','fres','fres.','frsp','frsp.','frsqrte','frsqrte.',
  95. 'fsel','fsel.','fsqrt','fsqrt.','fsqrts','fsqrts.','fsub','fsub.',
  96. 'fsubs','fsubs.','icbi','isync','lbz','lbzu','lbzux','lbzx',
  97. 'lfd','lfdu','lfdux','lfdx','lfs','lfsu','lfsux','lfsx','lha',
  98. 'lhau','lhaux','lhax','hbrx','lhz','lhzu','lhzux','lhzx','lmw',
  99. 'lswi','lswx','lwarx','lwbrx','lwz','lwzu','lwzux','lwzx','mcrf',
  100. 'mcrfs','mcrxr','lcrxe','mfcr','mffs','maffs.','mfmsr','mfspr','mfsr',
  101. 'mfsrin','mftb','mtfcrf','mtfd0','mtfsb1','mtfsf','mtfsf.',
  102. 'mtfsfi','mtfsfi.','mtmsr','mtspr','mtsr','mtsrin','mulhw',
  103. 'mulhw.','mulhwu','mulhwu.','mulli','mullw','mullw.','mullwo',
  104. 'mullwo.','nand','nand.','neg','neg.','nego','nego.','nor','nor.',
  105. 'or','or.','orc','orc.','ori','oris', 'rfi', 'rlwimi', 'rlwimi.',
  106. 'rlwinm', 'rlwinm.','rlwnm','sc','slw', 'slw.', 'sraw', 'sraw.',
  107. 'srawi', 'srawi.','srw', 'srw.', 'stb', 'stbu', 'stbux','stbx','stfd',
  108. 'stfdu', 'stfdux', 'stfdx', 'stfiwx', 'stfs', 'stfsu', 'stfsux', 'stfsx',
  109. 'sth', 'sthbrx', 'sthu', 'sthux', 'sthx', 'stmw', 'stswi', 'stswx', 'stw',
  110. 'stwbrx', 'stwx.', 'stwu', 'stwux', 'stwx', 'subf', 'subf.', 'subfo',
  111. 'subfo.', 'subfc', 'subc.', 'subfco', 'subfco.', 'subfe', 'subfe.',
  112. 'subfeo', 'subfeo.', 'subfic', 'subfme', 'subfme.', 'subfmeo', 'subfmeo.',
  113. 'subfze', 'subfze.', 'subfzeo', 'subfzeo.', 'sync', 'tlbia', 'tlbie',
  114. 'tlbsync', 'tw', 'twi', 'xor', 'xor.', 'xori', 'xoris',
  115. { some simplified mnemonics }
  116. 'subi', 'subis', 'subic', 'subic.', 'sub', 'sub.', 'subo', 'subo.',
  117. 'subc', 'subc.', 'subco', 'subco.', 'cmpwi', 'cmpw', 'cmplwi', 'cmplw',
  118. 'extlwi', 'extlwi.', 'extrwi', 'extrwi.', 'inslwi', 'inslwi.', 'insrwi',
  119. 'insrwi.', 'rotlwi', 'rotlwi.', 'rotlw', 'rotlw.', 'slwi', 'slwi.',
  120. 'srwi', 'srwi.', 'clrlwi', 'clrlwi.', 'clrrwi', 'clrrwi.', 'clrslwi',
  121. 'clrslwi.', 'blr', 'bctr', 'blrl', 'bctrl', 'crset', 'crclr', 'crmove',
  122. 'crnot', 'mt', 'mf','nop', 'li', 'lis', 'la', 'mr','mr.','not', 'mtcr', 'mtlr', 'mflr',
  123. 'mtctr', 'mfctr');
  124. function getreferencestring(var ref : treference) : string;
  125. var
  126. s : string;
  127. begin
  128. with ref do
  129. begin
  130. inc(offset,offsetfixup);
  131. if (symaddr <> refs_full) then
  132. InternalError(2002110301)
  133. else if ((offset < -32768) or (offset > 32767)) then
  134. InternalError(19991);
  135. if assigned(symbol) then
  136. begin
  137. s:= symbol.name;
  138. ReplaceForbiddenChars(s);
  139. {if symbol.typ = AT_FUNCTION then
  140. ;}
  141. s:= s+'[TC]' {ref to TOC entry }
  142. end
  143. else
  144. s:= '';
  145. if offset<0 then
  146. s:=s+tostr(offset)
  147. else
  148. if (offset>0) then
  149. begin
  150. if assigned(symbol) then
  151. s:=s+'+'+tostr(offset)
  152. else
  153. s:=s+tostr(offset);
  154. end;
  155. if (index.enum=R_NO) and (base.enum<>R_NO) then
  156. begin
  157. if offset=0 then
  158. if not assigned(symbol) then
  159. s:=s+'0';
  160. s:=s+'('+mpw_reg2str[base.enum]+')'
  161. end
  162. else if (index.enum<>R_NO) and (base.enum<>R_NO) and (offset=0) then
  163. s:=s+mpw_reg2str[base.enum]+','+mpw_reg2str[index.enum]
  164. else if ((index.enum<>R_NO) or (base.enum<>R_NO)) then
  165. internalerror(19992);
  166. end;
  167. getreferencestring:=s;
  168. end;
  169. function getopstr_jmp(const o:toper) : string;
  170. var
  171. hs : string;
  172. begin
  173. case o.typ of
  174. top_reg :
  175. getopstr_jmp:=mpw_reg2str[o.reg.enum];
  176. { no top_ref jumping for powerpc }
  177. top_const :
  178. getopstr_jmp:=tostr(o.val);
  179. top_symbol :
  180. begin
  181. hs:=o.sym.name;
  182. ReplaceForbiddenChars(hs);
  183. if o.symofs>0 then
  184. hs:=hs+'+'+tostr(o.symofs)
  185. else
  186. if o.symofs<0 then
  187. hs:=hs+tostr(o.symofs);
  188. getopstr_jmp:=hs;
  189. end;
  190. top_none:
  191. getopstr_jmp:='';
  192. else
  193. {$ifndef testing}
  194. internalerror(2002070603);
  195. {$else testing}
  196. begin
  197. writeln('internalerror 10001');
  198. halt(1);
  199. end;
  200. {$endif testing}
  201. end;
  202. end;
  203. function getopstr(const o:toper) : string;
  204. var
  205. hs : string;
  206. begin
  207. case o.typ of
  208. top_reg:
  209. getopstr:=mpw_reg2str[o.reg.enum];
  210. { no top_ref jumping for powerpc }
  211. top_const:
  212. getopstr:=tostr(longint(o.val));
  213. top_ref:
  214. getopstr:=getreferencestring(o.ref^);
  215. top_symbol:
  216. begin
  217. hs:=o.sym.name;
  218. ReplaceForbiddenChars(hs);
  219. if o.symofs>0 then
  220. hs:=hs+'+'+tostr(o.symofs)
  221. else
  222. if o.symofs<0 then
  223. hs:=hs+tostr(o.symofs);
  224. getopstr:=hs;
  225. end;
  226. else
  227. {$ifndef testing}
  228. internalerror(2002070604);
  229. {$else testing}
  230. begin
  231. writeln('internalerror 10001');
  232. halt(1);
  233. end;
  234. {$endif testing}
  235. end;
  236. end;
  237. function branchmode(o: tasmop): string[4];
  238. var tempstr: string[4];
  239. begin
  240. tempstr := '';
  241. case o of
  242. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  243. A_BCLR,A_BCLRL: tempstr := 'lr';
  244. end;
  245. case o of
  246. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  247. end;
  248. case o of
  249. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  250. end;
  251. branchmode := tempstr;
  252. end;
  253. function cond2str(op: tasmop; c: tasmcond): string;
  254. { note: no checking is performed whether the given combination of }
  255. { conditions is valid }
  256. var tempstr: string;
  257. begin
  258. tempstr:=#9;
  259. case c.simple of
  260. false: cond2str := tempstr+op2str[op]+#9+tostr(c.bo)+','+
  261. tostr(c.bi);
  262. true:
  263. if (op >= A_B) and (op <= A_BCLRL) then
  264. case c.cond of
  265. { unconditional branch }
  266. C_NONE:
  267. cond2str := tempstr+op2str[op];
  268. { bdnzt etc }
  269. else
  270. begin
  271. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  272. branchmode(op)+#9;
  273. case c.cond of
  274. C_LT..C_NU:
  275. cond2str := tempstr+mpw_reg2str[c.cr];
  276. C_T..C_DZF:
  277. cond2str := tempstr+tostr(c.crbit);
  278. end;
  279. end;
  280. end
  281. { we have a trap instruction }
  282. else
  283. begin
  284. internalerror(2002070601);
  285. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  286. { case tempstr := 'tw';}
  287. end;
  288. end;
  289. end;
  290. Function GetInstruction(hp : tai):string; {CHANGED from method to proc}
  291. var op: TAsmOp;
  292. s: string;
  293. i: byte;
  294. sep: string[3];
  295. begin
  296. op:=taicpu(hp).opcode;
  297. if is_calljmp(op) then
  298. begin
  299. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  300. case op of
  301. A_B,A_BA,A_BLA:
  302. s:=#9+op2str[op]+#9;
  303. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  304. s:=#9+op2str[op];
  305. A_BL:
  306. s:=#9+op2str[op]+#9'.';
  307. else
  308. s:=cond2str(op,taicpu(hp).condition)+',';
  309. end;
  310. if (taicpu(hp).oper[0].typ <> top_none) then
  311. s:=s+getopstr_jmp(taicpu(hp).oper[0]);
  312. if use_PR then
  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.enum 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.enum 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. procedure TPPCMPWAssembler.GenProcedureHeader(var hp:tai);
  417. {Returns the current hp where the caller should continue from}
  418. {For multiple entry procedures, only the last is exported as xxx[PR]
  419. (if use_PR is set) }
  420. procedure WriteExportHeader(hp:tai);
  421. var
  422. s: string;
  423. replaced: boolean;
  424. begin
  425. s:= tai_symbol(hp).sym.name;
  426. replaced:= ReplaceForbiddenChars(s);
  427. if replaced then
  428. begin
  429. if not use_PR then
  430. AsmWriteLn(#9'export'#9'.'+s+' => ''.'+tai_symbol(hp).sym.name+'''');
  431. AsmWriteLn(#9'export'#9+s+'[DS] => '''+tai_symbol(hp).sym.name+'[DS]''');
  432. end
  433. else
  434. begin
  435. if not use_PR then
  436. AsmWriteLn(#9'export'#9'.'+s);
  437. AsmWriteLn(#9'export'#9+s+'[DS]');
  438. end;
  439. {Entry in transition vector: }
  440. AsmWriteLn(#9'csect'#9+s+'[DS]');
  441. AsmWriteLn(#9'dc.l'#9'.'+s);
  442. AsmWriteln(#9'dc.l'#9'TOC[tc0]');
  443. {Entry in TOC: }
  444. AsmWriteLn(#9'toc');
  445. AsmWriteLn(#9'tc'#9+s+'[TC],'+s+'[DS]');
  446. end;
  447. function GetAdjacentTaiSymbol(var hp:tai):Boolean;
  448. begin
  449. GetAdjacentTaiSymbol:= false;
  450. if assigned(hp.next) and (tai(hp.next).typ=ait_symbol) then
  451. begin
  452. hp:=tai(hp.next);
  453. GetAdjacentTaiSymbol:= true;
  454. end;
  455. end;
  456. var
  457. first,last: tai;
  458. s: string;
  459. replaced: boolean;
  460. begin
  461. s:= tai_symbol(hp).sym.name;
  462. {Write all headers}
  463. first:= hp;
  464. repeat
  465. WriteExportHeader(hp);
  466. last:= hp;
  467. until not GetAdjacentTaiSymbol(hp);
  468. {Start the section of the body of the proc: }
  469. s:= tai_symbol(last).sym.name;
  470. replaced:= ReplaceForbiddenChars(s);
  471. if use_PR then
  472. if replaced then
  473. AsmWriteLn(#9'export'#9'.'+s+'[PR] => ''.'+tai_symbol(last).sym.name+'[PR]''')
  474. else
  475. AsmWriteLn(#9'export'#9'.'+s+'[PR]');
  476. AsmWriteLn(#9'csect'#9'.'+s+'[PR]'); //starts the section
  477. AsmWriteLn(#9'function'#9'.'+s+'[PR]'); //info for debugger
  478. {Write all labels: }
  479. hp:= first;
  480. repeat
  481. s:= tai_symbol(hp).sym.name;
  482. ReplaceForbiddenChars(s);
  483. AsmWriteLn('.'+s+':');
  484. until not GetAdjacentTaiSymbol(hp);
  485. end;
  486. (*
  487. procedure TPPCMPWAssembler.GenProcedureHeader(hp:tai);
  488. var
  489. s: string;
  490. replaced: boolean;
  491. begin
  492. s:= tai_symbol(hp).sym.name;
  493. replaced:= ReplaceForbiddenChars(s);
  494. if replaced then
  495. begin
  496. AsmWriteLn(#9'export'#9'.'+s+'[PR] => ''.'+tai_symbol(hp).sym.name+'[PR]''');
  497. AsmWriteLn(#9'export'#9+s+'[DS] => '''+tai_symbol(hp).sym.name+'[DS]''');
  498. end
  499. else
  500. begin
  501. AsmWriteLn(#9'export'#9'.'+s+'[PR]');
  502. AsmWriteLn(#9'export'#9+s+'[DS]');
  503. end;
  504. {Entry in transition vector: }
  505. AsmWriteLn(#9'csect'#9+s+'[DS]');
  506. AsmWriteLn(#9'dc.l'#9'.'+s);
  507. AsmWriteln(#9'dc.l'#9'TOC[tc0]');
  508. {Entry in TOC: }
  509. AsmWriteLn(#9'toc');
  510. AsmWriteLn(#9'tc'#9+s+'[TC],'+s+'[DS]');
  511. {Start the section of the body of the proc: }
  512. AsmWriteLn(#9'csect'#9'.'+s+'[PR]');
  513. AsmWriteLn('.'+s+':');
  514. end;
  515. *)
  516. var
  517. LasTSec : TSection;
  518. lastfileinfo : tfileposinfo;
  519. infile,
  520. lastinfile : tinputfile;
  521. const
  522. ait_const2str:array[ait_const_32bit..ait_const_8bit] of string[8]=
  523. (#9'dc.l'#9,#9'dc.w'#9,#9'dc.b'#9);
  524. Function PadTabs(const p:string;addch:char):string;
  525. var
  526. s : string;
  527. i : longint;
  528. begin
  529. i:=length(p);
  530. if addch<>#0 then
  531. begin
  532. inc(i);
  533. s:=p+addch;
  534. end
  535. else
  536. s:=p;
  537. if i<8 then
  538. PadTabs:=s+#9#9
  539. else
  540. PadTabs:=s+#9;
  541. end;
  542. procedure TPPCMPWAssembler.WriteTree(p:TAAsmoutput);
  543. var
  544. s,
  545. prefix,
  546. suffix : string;
  547. hp : tai;
  548. hp1 : tailineinfo;
  549. counter,
  550. lines,
  551. InlineLevel : longint;
  552. i,j,l : longint;
  553. consttyp : taitype;
  554. found,
  555. do_line,DoNotSplitLine,
  556. quoted : boolean;
  557. sep : char;
  558. replaced : boolean;
  559. begin
  560. if not assigned(p) then
  561. exit;
  562. { lineinfo is only needed for codesegment (PFV) }
  563. do_line:=((cs_asm_source in aktglobalswitches) or
  564. (cs_lineinfo in aktmoduleswitches))
  565. and (p=codesegment);
  566. InlineLevel:=0;
  567. DoNotSplitLine:=false;
  568. hp:=tai(p.first);
  569. while assigned(hp) do
  570. begin
  571. if do_line and not(hp.typ in SkipLineInfo) and
  572. not DoNotSplitLine then
  573. begin
  574. hp1 := hp as tailineinfo;
  575. { load infile }
  576. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  577. begin
  578. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  579. if assigned(infile) then
  580. begin
  581. { open only if needed !! }
  582. if (cs_asm_source in aktglobalswitches) then
  583. infile.open;
  584. end;
  585. { avoid unnecessary reopens of the same file !! }
  586. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  587. { be sure to change line !! }
  588. lastfileinfo.line:=-1;
  589. end;
  590. { write source }
  591. if (cs_asm_source in aktglobalswitches) and
  592. assigned(infile) then
  593. begin
  594. if (infile<>lastinfile) then
  595. begin
  596. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  597. if assigned(lastinfile) then
  598. lastinfile.close;
  599. end;
  600. if (hp1.fileinfo.line<>lastfileinfo.line) and
  601. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  602. begin
  603. if (hp1.fileinfo.line<>0) and
  604. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  605. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  606. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  607. { set it to a negative value !
  608. to make that is has been read already !! PM }
  609. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  610. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  611. end;
  612. end;
  613. lastfileinfo:=hp1.fileinfo;
  614. lastinfile:=infile;
  615. end;
  616. DoNotSplitLine:=false;
  617. case hp.typ of
  618. ait_comment:
  619. begin
  620. AsmWrite(target_asm.comment);
  621. AsmWritePChar(tai_comment(hp).str);
  622. AsmLn;
  623. end;
  624. ait_regalloc,
  625. ait_tempalloc:
  626. ;
  627. ait_section:
  628. begin
  629. {if LasTSec<>sec_none then
  630. AsmWriteLn('_'+target_asm.secnames[LasTSec]+#9#9'ENDS');}
  631. if tai_section(hp).sec<>sec_none then
  632. begin
  633. AsmLn;
  634. AsmWriteLn(#9+target_asm.secnames[tai_section(hp).sec]){+#9#9+
  635. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  636. target_asm.secnames[tai_section(hp).sec]+'''');}
  637. end;
  638. LasTSec:=tai_section(hp).sec;
  639. end;
  640. ait_align:
  641. begin
  642. case tai_align(hp).aligntype of
  643. 1:AsmWriteLn(#9'align 0');
  644. 2:AsmWriteLn(#9'align 1');
  645. 4:AsmWriteLn(#9'align 2');
  646. otherwise internalerror(2002110302);
  647. end;
  648. end;
  649. ait_datablock:
  650. begin
  651. s:= tai_datablock(hp).sym.name;
  652. replaced:= ReplaceForbiddenChars(s);
  653. if tai_datablock(hp).is_global then
  654. if replaced then
  655. AsmWriteLn(#9'export'#9+s+' => '''+tai_datablock(hp).sym.name+'''')
  656. else
  657. AsmWriteLn(#9'export'#9+s);
  658. AsmWriteLn(#9'csect'#9+s+'[TC]');
  659. AsmWriteLn(PadTabs(s+':',#0)+'ds.b '+tostr(tai_datablock(hp).size));
  660. {TODO: ? PadTabs(s,#0) }
  661. end;
  662. ait_const_32bit,
  663. ait_const_8bit,
  664. ait_const_16bit :
  665. begin
  666. AsmWrite(ait_const2str[hp.typ]+tostr(tai_const(hp).value));
  667. consttyp:=hp.typ;
  668. l:=0;
  669. repeat
  670. found:=(not (tai(hp.next)=nil)) and (tai(hp.next).typ=consttyp);
  671. if found then
  672. begin
  673. hp:=tai(hp.next);
  674. s:=','+tostr(tai_const(hp).value);
  675. AsmWrite(s);
  676. inc(l,length(s));
  677. end;
  678. until (not found) or (l>line_length);
  679. AsmLn;
  680. end;
  681. ait_const_symbol:
  682. begin
  683. (*
  684. AsmWriteLn(#9#9'dd'#9'offset '+tai_const_symbol(hp).sym.name);
  685. if tai_const_symbol(hp).offset>0 then
  686. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  687. else if tai_const_symbol(hp).offset<0 then
  688. AsmWrite(tostr(tai_const_symbol(hp).offset));
  689. AsmLn;
  690. *)
  691. s:= tai_const_symbol(hp).sym.name;
  692. ReplaceForbiddenChars(s);
  693. if tai_const_symbol(hp).sym.typ = AT_FUNCTION then
  694. begin
  695. if use_PR then
  696. AsmWriteLn(#9'dc.l'#9'.'+ s +'[PR]')
  697. else
  698. AsmWriteLn(#9'dc.l'#9'.'+ s)
  699. end
  700. else
  701. AsmWriteLn(#9'dc.l'#9+ s);
  702. (* TODO: the following might need to be included. Temporaily we
  703. generate an error
  704. if tai_const_symbol(hp).offset>0 then
  705. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  706. else if tai_const_symbol(hp).offset<0 then
  707. AsmWrite(tostr(tai_const_symbol(hp).offset));
  708. *)
  709. if tai_const_symbol(hp).offset <> 0 then
  710. InternalError(2002110101);
  711. AsmLn;
  712. end;
  713. ait_real_32bit:
  714. AsmWriteLn(#9'dc.l'#9'"'+single2str(tai_real_32bit(hp).value)+'"');
  715. ait_real_64bit:
  716. AsmWriteLn(#9'dc.d'#9'"'+double2str(tai_real_64bit(hp).value)+'"');
  717. ait_string:
  718. begin
  719. {NOTE When a single quote char is encountered, it is
  720. replaced with a numeric ascii value. It could also
  721. have been replaced with the escape seq of double quotes.}
  722. counter := 0;
  723. lines := tai_string(hp).len div line_length;
  724. { separate lines in different parts }
  725. if tai_string(hp).len > 0 then
  726. Begin
  727. for j := 0 to lines-1 do
  728. begin
  729. AsmWrite(#9'dc.b'#9);
  730. quoted:=false;
  731. for i:=counter to counter+line_length do
  732. begin
  733. { it is an ascii character. }
  734. if (ord(tai_string(hp).str[i])>31) and
  735. (ord(tai_string(hp).str[i])<128) and
  736. (tai_string(hp).str[i]<>'''') then
  737. begin
  738. if not(quoted) then
  739. begin
  740. if i>counter then
  741. AsmWrite(',');
  742. AsmWrite('''');
  743. end;
  744. AsmWrite(tai_string(hp).str[i]);
  745. quoted:=true;
  746. end { if > 31 and < 128 and ord('"') }
  747. else
  748. begin
  749. if quoted then
  750. AsmWrite('''');
  751. if i>counter then
  752. AsmWrite(',');
  753. quoted:=false;
  754. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  755. end;
  756. end; { end for i:=0 to... }
  757. if quoted then AsmWrite('''');
  758. AsmWrite(target_info.newline);
  759. counter := counter+line_length;
  760. end; { end for j:=0 ... }
  761. { do last line of lines }
  762. AsmWrite(#9'dc.b'#9);
  763. quoted:=false;
  764. for i:=counter to tai_string(hp).len-1 do
  765. begin
  766. { it is an ascii character. }
  767. if (ord(tai_string(hp).str[i])>31) and
  768. (ord(tai_string(hp).str[i])<128) and
  769. (tai_string(hp).str[i]<>'''') then
  770. begin
  771. if not(quoted) then
  772. begin
  773. if i>counter then
  774. AsmWrite(',');
  775. AsmWrite('''');
  776. end;
  777. AsmWrite(tai_string(hp).str[i]);
  778. quoted:=true;
  779. end { if > 31 and < 128 and " }
  780. else
  781. begin
  782. if quoted then
  783. AsmWrite('''');
  784. if i>counter then
  785. AsmWrite(',');
  786. quoted:=false;
  787. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  788. end;
  789. end; { end for i:=0 to... }
  790. if quoted then
  791. AsmWrite('''');
  792. end;
  793. AsmLn;
  794. end;
  795. ait_label:
  796. begin
  797. if tai_label(hp).l.is_used then
  798. begin
  799. s:= tai_label(hp).l.name;
  800. ReplaceForbiddenChars(s);
  801. if s[1] <> '@' then
  802. AsmWriteLn(#9'csect'#9+s+'[TC]');
  803. AsmWrite(s);
  804. {if assigned(hp.next) and not(tai(hp.next).typ in
  805. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  806. ait_const_symbol,ait_const_rva,
  807. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  808. AsmWriteLn(':')
  809. else
  810. DoNotSplitLine:=true;}
  811. AsmWriteLn(':');
  812. end;
  813. end;
  814. ait_direct:
  815. begin
  816. AsmWritePChar(tai_direct(hp).str);
  817. AsmLn;
  818. end;
  819. ait_symbol:
  820. begin
  821. if tai_symbol(hp).sym.typ=AT_FUNCTION then
  822. GenProcedureHeader(hp)
  823. else
  824. begin
  825. s:= tai_symbol(hp).sym.name;
  826. replaced:= ReplaceForbiddenChars(s);
  827. if tai_symbol(hp).is_global then
  828. if replaced then
  829. AsmWriteLn(#9'export'#9+s+' => '''+tai_symbol(hp).sym.name+'''')
  830. else
  831. AsmWriteLn(#9'export'#9+s);
  832. AsmWriteLn(#9'csect'#9+s+'[TC]');
  833. AsmWrite(s);
  834. AsmWriteLn(':');
  835. end;
  836. end;
  837. ait_symbol_end:
  838. ;
  839. ait_instruction:
  840. AsmWriteLn(GetInstruction(hp));
  841. {$ifdef GDB}
  842. ait_stabn,
  843. ait_stabs,
  844. ait_force_line,
  845. ait_stab_function_name : ;
  846. {$endif GDB}
  847. ait_cut : begin
  848. { only reset buffer if nothing has changed }
  849. if AsmSize=AsmStartSize then
  850. AsmClear
  851. else
  852. begin
  853. {
  854. if LasTSec<>sec_none then
  855. AsmWriteLn('_'+target_asm.secnames[LasTSec]+#9#9'ends');
  856. AsmLn;
  857. }
  858. AsmWriteLn(#9'end');
  859. AsmClose;
  860. DoAssemble;
  861. AsmCreate(tai_cut(hp).place);
  862. end;
  863. { avoid empty files }
  864. while assigned(hp.next) and (tai(hp.next).typ in [ait_cut,ait_section,ait_comment]) do
  865. begin
  866. if tai(hp.next).typ=ait_section then
  867. begin
  868. lasTSec:=tai_section(hp.next).sec;
  869. end;
  870. hp:=tai(hp.next);
  871. end;
  872. WriteAsmFileHeader;
  873. if lasTSec<>sec_none then
  874. AsmWriteLn(#9+target_asm.secnames[lasTSec]);
  875. { AsmWriteLn('_'+target_asm.secnames[lasTSec]+#9#9+
  876. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  877. target_asm.secnames[lasTSec]+'''');
  878. }
  879. AsmStartSize:=AsmSize;
  880. end;
  881. ait_marker :
  882. begin
  883. if tai_marker(hp).kind=InlineStart then
  884. inc(InlineLevel)
  885. else if tai_marker(hp).kind=InlineEnd then
  886. dec(InlineLevel);
  887. end;
  888. else
  889. internalerror(2002110303);
  890. end;
  891. hp:=tai(hp.next);
  892. end;
  893. end;
  894. var
  895. currentasmlist : TExternalAssembler;
  896. procedure writeexternal(p:tnamedindexitem;arg:pointer);
  897. var
  898. s:string;
  899. begin
  900. if tasmsymbol(p).defbind=AB_EXTERNAL then
  901. begin
  902. //Writeln('ZZZ ',p.name,' ',p.classname,' ',Ord(tasmsymbol(p).typ));
  903. s:= p.name;
  904. case tasmsymbol(p).typ of
  905. AT_FUNCTION:
  906. begin
  907. if ReplaceForbiddenChars(s) then
  908. begin
  909. if not use_PR then
  910. currentasmlist.AsmWriteLn(#9'import'#9'.'+s+' <= ''.'+p.name+'''')
  911. else
  912. currentasmlist.AsmWriteLn(#9'import'#9'.'+s+'[PR] <= ''.'+p.name+'[PR]''');
  913. currentasmlist.AsmWriteLn(#9'import'#9+s+'[DS] <= '''+p.name+'[DS]''');
  914. end
  915. else
  916. begin
  917. if not use_PR then
  918. currentasmlist.AsmWriteLn(#9'import'#9'.'+s)
  919. else
  920. currentasmlist.AsmWriteLn(#9'import'#9'.'+s+'[PR]');
  921. currentasmlist.AsmWriteLn(#9'import'#9+s+'[DS]');
  922. end;
  923. currentasmlist.AsmWriteLn(#9'toc');
  924. currentasmlist.AsmWriteLn(#9'tc'#9+s+'[TC],'+s+'[DS]');
  925. end
  926. else
  927. begin
  928. if ReplaceForbiddenChars(s) then
  929. currentasmlist.AsmWriteLn(#9'import'#9+s+' <= '''+p.name+'''')
  930. else
  931. currentasmlist.AsmWriteLn(#9'import'#9+s);
  932. end;
  933. end;
  934. end;
  935. end;
  936. procedure TPPCMPWAssembler.WriteExternals;
  937. begin
  938. currentasmlist:=self;
  939. objectlibrary.symbolsearch.foreach_static({$ifdef fpcprocvar}@{$endif}writeexternal,nil);
  940. end;
  941. function TPPCMPWAssembler.DoAssemble : boolean;
  942. var f : file;
  943. begin
  944. DoAssemble:=Inherited DoAssemble;
  945. (*
  946. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  947. if (aktoutputformat = as_i386_masm) then
  948. begin
  949. if not(cs_asm_extern in aktglobalswitches) then
  950. begin
  951. if Not FileExists(objfile) and
  952. FileExists(ForceExtension(objfile,'.obj')) then
  953. begin
  954. Assign(F,ForceExtension(objfile,'.obj'));
  955. Rename(F,objfile);
  956. end;
  957. end
  958. else
  959. AsmRes.AddAsmCommand('mv',ForceExtension(objfile,'.obj')+' '+objfile,objfile);
  960. end;
  961. *)
  962. end;
  963. procedure TPPCMPWAssembler.WriteAsmFileHeader;
  964. begin
  965. (*
  966. AsmWriteLn(#9'.386p');
  967. { masm 6.11 does not seem to like LOCALS PM }
  968. if (aktoutputformat = as_i386_tasm) then
  969. begin
  970. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  971. end;
  972. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  973. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  974. AsmLn;
  975. *)
  976. AsmWriteLn(#9'STRING ASIS'); {Interpret strings just to be the content between the quotes.}
  977. AsmLn;
  978. end;
  979. procedure TPPCMPWAssembler.WriteAsmList;
  980. begin
  981. {$ifdef EXTDEBUG}
  982. if assigned(current_module.mainsource) then
  983. comment(v_info,'Start writing MPW-styled assembler output for '+current_module.mainsource^);
  984. {$endif}
  985. LasTSec:=sec_none;
  986. WriteAsmFileHeader;
  987. WriteExternals;
  988. { PowerPC MPW ASM doesn't support stabs, as we now.
  989. WriteTree(debuglist);}
  990. WriteTree(codesegment);
  991. WriteTree(datasegment);
  992. WriteTree(consts);
  993. WriteTree(rttilist);
  994. WriteTree(resourcestringlist);
  995. WriteTree(bsssegment);
  996. AsmWriteLn(#9'end');
  997. AsmLn;
  998. {$ifdef EXTDEBUG}
  999. if assigned(current_module.mainsource) then
  1000. comment(v_info,'Done writing MPW-styled assembler output for '+current_module.mainsource^);
  1001. {$endif EXTDEBUG}
  1002. end;
  1003. {*****************************************************************************
  1004. Initialize
  1005. *****************************************************************************}
  1006. const
  1007. as_powerpc_mpw_info : tasminfo =
  1008. (
  1009. id : as_powerpc_mpw;
  1010. idtxt : 'MPW';
  1011. asmbin : 'PPCAsm';
  1012. asmcmd : '';
  1013. supported_target : system_any; { what should I write here ?? }
  1014. outputbinary: false;
  1015. allowdirect : true;
  1016. needar : true;
  1017. labelprefix_only_inside_procedure : true;
  1018. labelprefix : '@';
  1019. comment : '; ';
  1020. secnames : ('',
  1021. 'csect','csect [TC]','csect [TC]', {TODO: Perhaps use other section types.}
  1022. '','','','','','',
  1023. '','','')
  1024. );
  1025. initialization
  1026. RegisterAssembler(as_powerpc_mpw_info,TPPCMPWAssembler);
  1027. end.
  1028. {
  1029. $Log$
  1030. Revision 1.17 2003-01-08 18:43:57 daniel
  1031. * Tregister changed into a record
  1032. Revision 1.16 2002/11/28 10:56:07 olle
  1033. * changed proc ref from .xxx[PR] (refering to its section)
  1034. to .xxx (refering to its label) to allow for multiple ref to a proc.
  1035. Revision 1.15 2002/11/17 16:31:59 carl
  1036. * memory optimization (3-4%) : cleanup of tai fields,
  1037. cleanup of tdef and tsym fields.
  1038. * make it work for m68k
  1039. Revision 1.14 2002/11/07 15:50:23 jonas
  1040. * fixed bctr(l) problems
  1041. Revision 1.13 2002/11/04 18:24:53 olle
  1042. * globals are located in TOC and relative r2, instead of absolute
  1043. * symbols which only differs in case are treated as a single symbol
  1044. + tai_const_symbol supported
  1045. * only refs_full accepted
  1046. Revision 1.12 2002/10/23 15:31:01 olle
  1047. * branch b does not jump to dotted symbol now
  1048. Revision 1.11 2002/10/19 23:52:40 olle
  1049. * import directive changed
  1050. Revision 1.10 2002/10/10 19:39:37 florian
  1051. * changes from Olle to get simple programs compiled and assembled
  1052. Revision 1.9 2002/10/07 21:19:53 florian
  1053. * more mpw fixes
  1054. Revision 1.8 2002/10/06 22:46:20 florian
  1055. * fixed function exporting
  1056. Revision 1.7 2002/10/02 22:14:15 florian
  1057. * improve function imports
  1058. Revision 1.6 2002/09/27 21:09:49 florian
  1059. + readed because previous version was broken
  1060. Revision 1.2 2002/08/31 12:43:31 florian
  1061. * ppc compilation fixed
  1062. Revision 1.1 2002/08/20 21:40:44 florian
  1063. + target macos for ppc added
  1064. + frame work for mpw assembler output
  1065. }