agppcmpw.pas 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  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. aasmtai,
  26. globals,aasmbase,aasmcpu,assemble,
  27. cpubase;
  28. const
  29. mpw_reg2str : treg2strtable = ('',
  30. 'r0','r1','r2','r3','r4','r5','r6','r7','r8','r9','r10','r11','r12','r13','r14','r15','r16',
  31. 'r17','r18','r19','r20','r21','r22','r23','r24','r25','r26','r27','r28','r29','r30','r31',
  32. 'f0','f1','f2','f3','f4','f5','f6','f7', 'f8','f9','f10','f11','f12',
  33. 'f13','f14','f15','f16','f17', 'f18','f19','f20','f21','f22', 'f23','f24',
  34. 'f25','f26','f27','f28','f29','f30','f31',
  35. 'v0','v1','v2','v3','v4','v5','v6','v7','v8','v9','v10','v11','v12',
  36. 'v13','v14','v15','v16','v17','v18','v19','v20','v21','v22', 'v23','v24',
  37. 'v25','v26','v27','v28','v29','v30','v31',
  38. 'cR','cr0','cr1','cr2','cr3','cr4','cr5','cr6','cr7',
  39. 'xer','lr','ctr','fpscr'
  40. );
  41. type
  42. TPPCMPWAssembler = class(TExternalAssembler)
  43. procedure WriteTree(p:TAAsmoutput);override;
  44. procedure WriteAsmList;override;
  45. Function DoAssemble:boolean;override;
  46. procedure WriteExternals;
  47. {$ifdef GDB}
  48. procedure WriteFileLineInfo(var fileinfo : tfileposinfo);
  49. procedure WriteFileEndInfo;
  50. {$endif}
  51. procedure WriteAsmFileHeader;
  52. private
  53. procedure GenProcedureHeader(var hp:tai);
  54. end;
  55. implementation
  56. uses
  57. {$ifdef delphi}
  58. sysutils,
  59. {$endif}
  60. cutils,globtype,systems,cclasses,
  61. verbose,finput,fmodule,script,cpuinfo
  62. ;
  63. const
  64. line_length = 70;
  65. {Whether internal procedure references should be xxx[PR]: }
  66. use_PR = false;
  67. const_storage_class = '[RW]';
  68. {$ifdef GDB}
  69. var
  70. n_line : byte; { different types of source lines }
  71. linecount,
  72. includecount : longint;
  73. funcname : pchar;
  74. stabslastfileinfo : tfileposinfo;
  75. isInFunction: Boolean;
  76. firstLineInFunction: longint;
  77. {$endif}
  78. function ReplaceForbiddenChars(var s: string):Boolean;
  79. {Returns wheater a replacement has occured.}
  80. var
  81. i:Integer;
  82. {The dollar sign is not allowed in MPW PPCAsm}
  83. begin
  84. ReplaceForbiddenChars:=false;
  85. for i:=1 to Length(s) do
  86. if s[i]='$' then
  87. begin
  88. s[i]:='s';
  89. ReplaceForbiddenChars:=true;
  90. end;
  91. end;
  92. const
  93. {*** From here is copyed from agppcgas.pp, except where marked with CHANGED.
  94. Perhaps put in a third common file. ***}
  95. op2str : array[tasmop] of string[14] = ('<none>',
  96. 'add','add.','addo','addo.','addc','addc.','addco','addco.',
  97. 'adde','adde.','addeo','addeo.','addi','addic','addic.','addis',
  98. 'addme','addme.','addmeo','addmeo.','addze','addze.','addzeo',
  99. 'addzeo.','and','and.','andc','andc.','andi.','andis.','b',
  100. 'ba','bl','bla','bc','bca','bcl','bcla','bcctr','bcctrl','bclr',
  101. 'bclrl','cmp','cmpi','cmpl','cmpli','cntlzw','cntlzw.','crand',
  102. 'crandc','creqv','crnand','crnor','cror','crorc','crxor','dcba',
  103. 'dcbf','dcbi','dcbst','dcbt','divw','divw.','divwo','divwo.',
  104. 'divwu','divwu.','divwuo','divwuo.','eciwx','ecowx','eieio','eqv',
  105. 'eqv.','extsb','extsb.','extsh','extsh.','fabs','fabs.','fadd',
  106. 'fadd.','fadds','fadds.','fcmpo','fcmpu','fctiw','fctw.','fctwz',
  107. 'fctwz.','fdiv','fdiv.','fdivs','fdivs.','fmadd','fmadd.','fmadds',
  108. 'fmadds.','fmr','fmsub','fmsub.','fmsubs','fmsubs.','fmul','fmul.',
  109. 'fmuls','fmuls.','fnabs','fnabs.','fneg','fneg.','fnmadd',
  110. 'fnmadd.','fnmadds','fnmadds.','fnmsub','fnmsub.','fnmsubs',
  111. 'fnmsubs.','fres','fres.','frsp','frsp.','frsqrte','frsqrte.',
  112. 'fsel','fsel.','fsqrt','fsqrt.','fsqrts','fsqrts.','fsub','fsub.',
  113. 'fsubs','fsubs.','icbi','isync','lbz','lbzu','lbzux','lbzx',
  114. 'lfd','lfdu','lfdux','lfdx','lfs','lfsu','lfsux','lfsx','lha',
  115. 'lhau','lhaux','lhax','hbrx','lhz','lhzu','lhzux','lhzx','lmw',
  116. 'lswi','lswx','lwarx','lwbrx','lwz','lwzu','lwzux','lwzx','mcrf',
  117. 'mcrfs','mcrxr','lcrxe','mfcr','mffs','maffs.','mfmsr','mfspr','mfsr',
  118. 'mfsrin','mftb','mtfcrf','mtfd0','mtfsb1','mtfsf','mtfsf.',
  119. 'mtfsfi','mtfsfi.','mtmsr','mtspr','mtsr','mtsrin','mulhw',
  120. 'mulhw.','mulhwu','mulhwu.','mulli','mullw','mullw.','mullwo',
  121. 'mullwo.','nand','nand.','neg','neg.','nego','nego.','nor','nor.',
  122. 'or','or.','orc','orc.','ori','oris', 'rfi', 'rlwimi', 'rlwimi.',
  123. 'rlwinm', 'rlwinm.','rlwnm','sc','slw', 'slw.', 'sraw', 'sraw.',
  124. 'srawi', 'srawi.','srw', 'srw.', 'stb', 'stbu', 'stbux','stbx','stfd',
  125. 'stfdu', 'stfdux', 'stfdx', 'stfiwx', 'stfs', 'stfsu', 'stfsux', 'stfsx',
  126. 'sth', 'sthbrx', 'sthu', 'sthux', 'sthx', 'stmw', 'stswi', 'stswx', 'stw',
  127. 'stwbrx', 'stwx.', 'stwu', 'stwux', 'stwx', 'subf', 'subf.', 'subfo',
  128. 'subfo.', 'subfc', 'subc.', 'subfco', 'subfco.', 'subfe', 'subfe.',
  129. 'subfeo', 'subfeo.', 'subfic', 'subfme', 'subfme.', 'subfmeo', 'subfmeo.',
  130. 'subfze', 'subfze.', 'subfzeo', 'subfzeo.', 'sync', 'tlbia', 'tlbie',
  131. 'tlbsync', 'tw', 'twi', 'xor', 'xor.', 'xori', 'xoris',
  132. { some simplified mnemonics }
  133. 'subi', 'subis', 'subic', 'subic.', 'sub', 'sub.', 'subo', 'subo.',
  134. 'subc', 'subc.', 'subco', 'subco.', 'cmpwi', 'cmpw', 'cmplwi', 'cmplw',
  135. 'extlwi', 'extlwi.', 'extrwi', 'extrwi.', 'inslwi', 'inslwi.', 'insrwi',
  136. 'insrwi.', 'rotlwi', 'rotlwi.', 'rotlw', 'rotlw.', 'slwi', 'slwi.',
  137. 'srwi', 'srwi.', 'clrlwi', 'clrlwi.', 'clrrwi', 'clrrwi.', 'clrslwi',
  138. 'clrslwi.', 'blr', 'bctr', 'blrl', 'bctrl', 'crset', 'crclr', 'crmove',
  139. 'crnot', 'mt', 'mf','nop', 'li', 'lis', 'la', 'mr','mr.','not', 'mtcr', 'mtlr', 'mflr',
  140. 'mtctr', 'mfctr');
  141. function mpw_regname(r:Tnewregister):string;
  142. var s:Tsuperregister;
  143. begin
  144. s:=r shr 8;
  145. if s in [RS_R0..RS_R31] then
  146. mpw_regname:='r'+tostr(s-RS_R0)
  147. else
  148. begin
  149. {Generate a systematic name.}
  150. mpw_regname:='reg'+tostr(s)+'d';
  151. end;
  152. end;
  153. function getreferencestring(var ref : treference) : string;
  154. var
  155. s : string;
  156. i,b:boolean;
  157. begin
  158. with ref do
  159. begin
  160. inc(offset,offsetfixup);
  161. if (symaddr <> refs_full) then
  162. InternalError(2002110301)
  163. else if ((offset < -32768) or (offset > 32767)) then
  164. InternalError(19991);
  165. if assigned(symbol) then
  166. begin
  167. s:= symbol.name;
  168. ReplaceForbiddenChars(s);
  169. {if symbol.typ = AT_FUNCTION then
  170. ;}
  171. s:= s+'[TC]' {ref to TOC entry }
  172. end
  173. else
  174. s:= '';
  175. if offset<0 then
  176. s:=s+tostr(offset)
  177. else
  178. if (offset>0) then
  179. begin
  180. if assigned(symbol) then
  181. s:=s+'+'+tostr(offset)
  182. else
  183. s:=s+tostr(offset);
  184. end;
  185. b:=(base.enum=R_NO) or ((base.enum=R_INTREGISTER) and (base.number=NR_NO));
  186. i:=(index.enum=R_NO) or ((index.enum=R_INTREGISTER) and (index.number=NR_NO));
  187. if i and not b then
  188. begin
  189. if offset=0 then
  190. if not assigned(symbol) then
  191. s:=s+'0';
  192. if base.enum=R_INTREGISTER then
  193. s:=s+'('+mpw_regname(base.number)+')'
  194. else
  195. s:=s+'('+mpw_reg2str[base.enum]+')';
  196. end
  197. else if (not i) and (not b) and (offset=0) then
  198. if base.enum=R_INTREGISTER then
  199. s:=s+mpw_regname(base.number)+','+mpw_regname(index.number)
  200. else
  201. s:=s+mpw_reg2str[base.enum]+','+mpw_reg2str[index.enum]
  202. else if (not i) or (not b) then
  203. internalerror(19992);
  204. end;
  205. getreferencestring:=s;
  206. end;
  207. function getopstr_jmp(const o:toper) : string;
  208. var
  209. hs : string;
  210. begin
  211. case o.typ of
  212. top_reg :
  213. begin
  214. if o.reg.enum=R_INTREGISTER then
  215. getopstr_jmp:=mpw_regname(o.reg.number)
  216. else
  217. getopstr_jmp:=mpw_reg2str[o.reg.enum];
  218. end;
  219. { no top_ref jumping for powerpc }
  220. top_const :
  221. getopstr_jmp:=tostr(o.val);
  222. top_symbol :
  223. begin
  224. hs:=o.sym.name;
  225. ReplaceForbiddenChars(hs);
  226. if o.symofs>0 then
  227. hs:=hs+'+'+tostr(o.symofs)
  228. else
  229. if o.symofs<0 then
  230. hs:=hs+tostr(o.symofs);
  231. getopstr_jmp:=hs;
  232. end;
  233. top_none:
  234. getopstr_jmp:='';
  235. else
  236. internalerror(2002070603);
  237. end;
  238. end;
  239. function getopstr(const o:toper) : string;
  240. var
  241. hs : string;
  242. begin
  243. case o.typ of
  244. top_reg:
  245. begin
  246. if o.reg.enum=R_INTREGISTER then
  247. getopstr:=mpw_regname(o.reg.number)
  248. else
  249. getopstr:=mpw_reg2str[o.reg.enum];
  250. end;
  251. top_const:
  252. getopstr:=tostr(longint(o.val));
  253. top_ref:
  254. getopstr:=getreferencestring(o.ref^);
  255. top_symbol:
  256. begin
  257. hs:=o.sym.name;
  258. ReplaceForbiddenChars(hs);
  259. if o.symofs>0 then
  260. hs:=hs+'+'+tostr(o.symofs)
  261. else
  262. if o.symofs<0 then
  263. hs:=hs+tostr(o.symofs);
  264. getopstr:=hs;
  265. end;
  266. else
  267. internalerror(2002070604);
  268. end;
  269. end;
  270. function branchmode(o: tasmop): string[4];
  271. var tempstr: string[4];
  272. begin
  273. tempstr := '';
  274. case o of
  275. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  276. A_BCLR,A_BCLRL: tempstr := 'lr';
  277. end;
  278. case o of
  279. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  280. end;
  281. case o of
  282. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  283. end;
  284. branchmode := tempstr;
  285. end;
  286. function cond2str(op: tasmop; c: tasmcond): string;
  287. { note: no checking is performed whether the given combination of }
  288. { conditions is valid }
  289. var tempstr: string;
  290. begin
  291. tempstr:=#9;
  292. case c.simple of
  293. false: cond2str := tempstr+op2str[op]+#9+tostr(c.bo)+','+
  294. tostr(c.bi);
  295. true:
  296. if (op >= A_B) and (op <= A_BCLRL) then
  297. case c.cond of
  298. { unconditional branch }
  299. C_NONE:
  300. cond2str := tempstr+op2str[op];
  301. { bdnzt etc }
  302. else
  303. begin
  304. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  305. branchmode(op)+#9;
  306. case c.cond of
  307. C_LT..C_NU:
  308. cond2str := tempstr+mpw_reg2str[c.cr];
  309. C_T..C_DZF:
  310. cond2str := tempstr+tostr(c.crbit);
  311. end;
  312. end;
  313. end
  314. { we have a trap instruction }
  315. else
  316. begin
  317. internalerror(2002070601);
  318. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  319. { case tempstr := 'tw';}
  320. end;
  321. end;
  322. end;
  323. Function GetInstruction(hp : tai):string; {CHANGED from method to proc}
  324. var op: TAsmOp;
  325. s: string;
  326. i: byte;
  327. sep: string[3];
  328. begin
  329. op:=taicpu(hp).opcode;
  330. if is_calljmp(op) then
  331. begin
  332. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  333. case op of
  334. A_B,A_BA:
  335. s:=#9+op2str[op]+#9;
  336. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  337. s:=#9+op2str[op];
  338. A_BL,A_BLA:
  339. s:=#9+op2str[op]+#9'.';
  340. else
  341. s:=cond2str(op,taicpu(hp).condition)+',';
  342. end;
  343. if (taicpu(hp).oper[0].typ <> top_none) then
  344. s:=s+getopstr_jmp(taicpu(hp).oper[0]);
  345. if use_PR then
  346. if (op=A_BL) or (op=A_BLA) then
  347. s:=s+'[PR]';
  348. end
  349. else
  350. { process operands }
  351. begin
  352. case op of
  353. A_MFSPR:
  354. case taicpu(hp).oper[1].reg.enum of
  355. R_CR:
  356. begin
  357. op:=A_MFCR;
  358. taicpu(hp).ops:=1;
  359. end;
  360. R_LR:
  361. begin
  362. op:=A_MFLR;
  363. taicpu(hp).ops:=1;
  364. end;
  365. else
  366. internalerror(2002100701);
  367. end;
  368. A_MTSPR:
  369. case taicpu(hp).oper[1].reg.enum of
  370. R_CR:
  371. begin
  372. op:=A_MTCR;
  373. taicpu(hp).ops:=1;
  374. end;
  375. R_LR:
  376. begin
  377. op:=A_MTLR;
  378. taicpu(hp).ops:=1;
  379. end;
  380. else
  381. internalerror(2002100701);
  382. end;
  383. end;
  384. s:=#9+op2str[op];
  385. if taicpu(hp).ops<>0 then
  386. begin
  387. sep:=#9;
  388. for i:=0 to taicpu(hp).ops-1 do
  389. begin
  390. s:=s+sep+getopstr(taicpu(hp).oper[i]);
  391. sep:=',';
  392. end;
  393. end;
  394. end;
  395. GetInstruction:=s;
  396. end;
  397. {*** Until here is copyed from agppcgas.pp. ***}
  398. function single2str(d : single) : string;
  399. var
  400. hs : string;
  401. p : byte;
  402. begin
  403. str(d,hs);
  404. { nasm expects a lowercase e }
  405. p:=pos('E',hs);
  406. if p>0 then
  407. hs[p]:='e';
  408. p:=pos('+',hs);
  409. if p>0 then
  410. delete(hs,p,1);
  411. single2str:=lower(hs);
  412. end;
  413. function double2str(d : double) : string;
  414. var
  415. hs : string;
  416. p : byte;
  417. begin
  418. str(d,hs);
  419. { nasm expects a lowercase e }
  420. p:=pos('E',hs);
  421. if p>0 then
  422. hs[p]:='e';
  423. p:=pos('+',hs);
  424. if p>0 then
  425. delete(hs,p,1);
  426. double2str:=lower(hs);
  427. end;
  428. function fixline(s:string):string;
  429. {
  430. return s with all leading and ending spaces and tabs removed
  431. }
  432. var
  433. i,j,k : longint;
  434. begin
  435. i:=length(s);
  436. while (i>0) and (s[i] in [#9,' ']) do
  437. dec(i);
  438. j:=1;
  439. while (j<i) and (s[j] in [#9,' ']) do
  440. inc(j);
  441. for k:=j to i do
  442. if s[k] in [#0..#31,#127..#255] then
  443. s[k]:='.';
  444. fixline:=Copy(s,j,i-j+1);
  445. end;
  446. {****************************************************************************
  447. PowerPC MPW Assembler
  448. ****************************************************************************}
  449. procedure TPPCMPWAssembler.GenProcedureHeader(var hp:tai);
  450. {Returns the current hp where the caller should continue from}
  451. {For multiple entry procedures, only the last is exported as xxx[PR]
  452. (if use_PR is set) }
  453. procedure WriteExportHeader(hp:tai);
  454. var
  455. s: string;
  456. replaced: boolean;
  457. begin
  458. s:= tai_symbol(hp).sym.name;
  459. replaced:= ReplaceForbiddenChars(s);
  460. if replaced then
  461. begin
  462. if not use_PR then
  463. AsmWriteLn(#9'export'#9'.'+s+' => ''.'+tai_symbol(hp).sym.name+'''');
  464. AsmWriteLn(#9'export'#9+s+'[DS] => '''+tai_symbol(hp).sym.name+'[DS]''');
  465. end
  466. else
  467. begin
  468. if not use_PR then
  469. AsmWriteLn(#9'export'#9'.'+s);
  470. AsmWriteLn(#9'export'#9+s+'[DS]');
  471. end;
  472. {Entry in transition vector: }
  473. AsmWriteLn(#9'csect'#9+s+'[DS]');
  474. AsmWriteLn(#9'dc.l'#9'.'+s);
  475. AsmWriteln(#9'dc.l'#9'TOC[tc0]');
  476. {Entry in TOC: }
  477. AsmWriteLn(#9'toc');
  478. AsmWriteLn(#9'tc'#9+s+'[TC],'+s+'[DS]');
  479. end;
  480. function GetAdjacentTaiSymbol(var hp:tai):Boolean;
  481. begin
  482. GetAdjacentTaiSymbol:= false;
  483. while assigned(hp.next) do
  484. case tai(hp.next).typ of
  485. ait_symbol:
  486. begin
  487. hp:=tai(hp.next);
  488. GetAdjacentTaiSymbol:= true;
  489. Break;
  490. end;
  491. ait_stab_function_name:
  492. hp:=tai(hp.next);
  493. else
  494. begin
  495. //AsmWriteln(' ;#*#*# ' + tostr(Ord(tai(hp.next).typ)));
  496. Break;
  497. end;
  498. end;
  499. end;
  500. var
  501. first,last: tai;
  502. s: string;
  503. replaced: boolean;
  504. begin
  505. s:= tai_symbol(hp).sym.name;
  506. {Write all headers}
  507. first:= hp;
  508. repeat
  509. WriteExportHeader(hp);
  510. last:= hp;
  511. until not GetAdjacentTaiSymbol(hp);
  512. {Start the section of the body of the proc: }
  513. s:= tai_symbol(last).sym.name;
  514. replaced:= ReplaceForbiddenChars(s);
  515. if use_PR then
  516. if replaced then
  517. AsmWriteLn(#9'export'#9'.'+s+'[PR] => ''.'+tai_symbol(last).sym.name+'[PR]''')
  518. else
  519. AsmWriteLn(#9'export'#9'.'+s+'[PR]');
  520. AsmWriteLn(#9'csect'#9'.'+s+'[PR]'); //starts the section
  521. AsmWriteLn(#9'function'#9'.'+s+'[PR]'); //info for debugger
  522. {$ifdef GDB}
  523. if ((cs_debuginfo in aktmoduleswitches) or
  524. (cs_gdb_lineinfo in aktglobalswitches)) then
  525. begin
  526. //info for debuggers:
  527. firstLineInFunction:= stabslastfileinfo.line;
  528. AsmWriteLn(#9'beginf ' + tostr(firstLineInFunction));
  529. isInFunction:= true;
  530. end;
  531. {$endif}
  532. {Write all labels: }
  533. hp:= first;
  534. repeat
  535. s:= tai_symbol(hp).sym.name;
  536. ReplaceForbiddenChars(s);
  537. AsmWriteLn('.'+s+':');
  538. until not GetAdjacentTaiSymbol(hp);
  539. end;
  540. (*
  541. procedure TPPCMPWAssembler.GenProcedureHeader(hp:tai);
  542. var
  543. s: string;
  544. replaced: boolean;
  545. begin
  546. s:= tai_symbol(hp).sym.name;
  547. replaced:= ReplaceForbiddenChars(s);
  548. if replaced then
  549. begin
  550. AsmWriteLn(#9'export'#9'.'+s+'[PR] => ''.'+tai_symbol(hp).sym.name+'[PR]''');
  551. AsmWriteLn(#9'export'#9+s+'[DS] => '''+tai_symbol(hp).sym.name+'[DS]''');
  552. end
  553. else
  554. begin
  555. AsmWriteLn(#9'export'#9'.'+s+'[PR]');
  556. AsmWriteLn(#9'export'#9+s+'[DS]');
  557. end;
  558. {Entry in transition vector: }
  559. AsmWriteLn(#9'csect'#9+s+'[DS]');
  560. AsmWriteLn(#9'dc.l'#9'.'+s);
  561. AsmWriteln(#9'dc.l'#9'TOC[tc0]');
  562. {Entry in TOC: }
  563. AsmWriteLn(#9'toc');
  564. AsmWriteLn(#9'tc'#9+s+'[TC],'+s+'[DS]');
  565. {Start the section of the body of the proc: }
  566. AsmWriteLn(#9'csect'#9'.'+s+'[PR]');
  567. AsmWriteLn('.'+s+':');
  568. end;
  569. *)
  570. var
  571. LasTSec : TSection;
  572. lastfileinfo : tfileposinfo;
  573. infile,
  574. lastinfile : tinputfile;
  575. const
  576. ait_const2str:array[ait_const_32bit..ait_const_8bit] of string[8]=
  577. (#9'dc.l'#9,#9'dc.w'#9,#9'dc.b'#9);
  578. Function PadTabs(const p:string;addch:char):string;
  579. var
  580. s : string;
  581. i : longint;
  582. begin
  583. i:=length(p);
  584. if addch<>#0 then
  585. begin
  586. inc(i);
  587. s:=p+addch;
  588. end
  589. else
  590. s:=p;
  591. if i<8 then
  592. PadTabs:=s+#9#9
  593. else
  594. PadTabs:=s+#9;
  595. end;
  596. {$ifdef GDB}
  597. procedure TPPCMPWAssembler.WriteFileLineInfo(var fileinfo : tfileposinfo);
  598. var
  599. curr_n : byte;
  600. begin
  601. if not ((cs_debuginfo in aktmoduleswitches) or
  602. (cs_gdb_lineinfo in aktglobalswitches)) then
  603. exit;
  604. { file changed ? (must be before line info) }
  605. if (fileinfo.fileindex<>0) and
  606. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  607. begin
  608. infile:=current_module.sourcefiles.get_file(fileinfo.fileindex);
  609. if assigned(infile) then
  610. begin
  611. (*
  612. if includecount=0 then
  613. curr_n:=n_sourcefile
  614. else
  615. curr_n:=n_includefile;
  616. if (infile.path^<>'') then
  617. begin
  618. AsmWriteLn(#9'.stabs "'+lower(BsToSlash(FixPath(infile.path^,false)))+'",'+
  619. tostr(curr_n)+',0,0,'+target_asm.labelprefix+'text'+ToStr(IncludeCount));
  620. end;
  621. AsmWriteLn(#9'.stabs "'+lower(FixFileName(infile.name^))+'",'+
  622. tostr(curr_n)+',0,0,'+target_asm.labelprefix+'text'+ToStr(IncludeCount));
  623. *)
  624. AsmWriteLn(#9'file '''+lower(FixFileName(infile.name^))+'''');
  625. (*
  626. AsmWriteLn(target_asm.labelprefix+'text'+ToStr(IncludeCount)+':');
  627. *)
  628. inc(includecount);
  629. { force new line info }
  630. stabslastfileinfo.line:=-1;
  631. end;
  632. end;
  633. { line changed ? }
  634. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  635. begin
  636. (*
  637. if (n_line=n_textline) and assigned(funcname) and
  638. (target_info.use_function_relative_addresses) then
  639. begin
  640. AsmWriteLn(target_asm.labelprefix+'l'+tostr(linecount)+':');
  641. AsmWrite(#9'.stabn '+tostr(n_line)+',0,'+tostr(fileinfo.line)+','+
  642. target_asm.labelprefix+'l'+tostr(linecount)+' - ');
  643. AsmWritePChar(FuncName);
  644. AsmLn;
  645. inc(linecount);
  646. end
  647. else
  648. AsmWriteLn(#9'.stabd'#9+tostr(n_line)+',0,'+tostr(fileinfo.line));
  649. *)
  650. if isInFunction then
  651. AsmWriteln(#9'line '+ tostr(fileinfo.line - firstLineInFunction - 1));
  652. end;
  653. stabslastfileinfo:=fileinfo;
  654. end;
  655. procedure TPPCMPWAssembler.WriteFileEndInfo;
  656. begin
  657. if not ((cs_debuginfo in aktmoduleswitches) or
  658. (cs_gdb_lineinfo in aktglobalswitches)) then
  659. exit;
  660. AsmLn;
  661. (*
  662. AsmWriteLn(ait_section2str(sec_code));
  663. AsmWriteLn(#9'.stabs "",'+tostr(n_sourcefile)+',0,0,'+target_asm.labelprefix+'etext');
  664. AsmWriteLn(target_asm.labelprefix+'etext:');
  665. *)
  666. end;
  667. {$endif}
  668. procedure TPPCMPWAssembler.WriteTree(p:TAAsmoutput);
  669. var
  670. s,
  671. prefix,
  672. suffix : string;
  673. hp : tai;
  674. hp1 : tailineinfo;
  675. counter,
  676. lines,
  677. InlineLevel : longint;
  678. i,j,l : longint;
  679. consttyp : taitype;
  680. found,
  681. do_line,DoNotSplitLine,
  682. quoted : boolean;
  683. sep : char;
  684. replaced : boolean;
  685. begin
  686. if not assigned(p) then
  687. exit;
  688. InlineLevel:=0;
  689. { lineinfo is only needed for codesegment (PFV) }
  690. do_line:=((cs_asm_source in aktglobalswitches) or
  691. (cs_lineinfo in aktmoduleswitches))
  692. and (p=codesegment);
  693. DoNotSplitLine:=false;
  694. hp:=tai(p.first);
  695. while assigned(hp) do
  696. begin
  697. if not(hp.typ in SkipLineInfo) and
  698. not DoNotSplitLine then
  699. begin
  700. hp1 := hp as tailineinfo;
  701. {$ifdef GDB}
  702. { write debug info }
  703. if (cs_debuginfo in aktmoduleswitches) or
  704. (cs_gdb_lineinfo in aktglobalswitches) then
  705. WriteFileLineInfo(hp1.fileinfo);
  706. {$endif GDB}
  707. if do_line then
  708. begin
  709. { load infile }
  710. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  711. begin
  712. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  713. if assigned(infile) then
  714. begin
  715. { open only if needed !! }
  716. if (cs_asm_source in aktglobalswitches) then
  717. infile.open;
  718. end;
  719. { avoid unnecessary reopens of the same file !! }
  720. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  721. { be sure to change line !! }
  722. lastfileinfo.line:=-1;
  723. end;
  724. { write source }
  725. if (cs_asm_source in aktglobalswitches) and
  726. assigned(infile) then
  727. begin
  728. if (infile<>lastinfile) then
  729. begin
  730. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  731. if assigned(lastinfile) then
  732. lastinfile.close;
  733. end;
  734. if (hp1.fileinfo.line<>lastfileinfo.line) and
  735. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  736. begin
  737. if (hp1.fileinfo.line<>0) and
  738. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  739. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  740. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  741. { set it to a negative value !
  742. to make that is has been read already !! PM }
  743. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  744. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  745. end;
  746. end;
  747. lastfileinfo:=hp1.fileinfo;
  748. lastinfile:=infile;
  749. end;
  750. end;
  751. DoNotSplitLine:=false;
  752. case hp.typ of
  753. ait_comment:
  754. begin
  755. AsmWrite(target_asm.comment);
  756. AsmWritePChar(tai_comment(hp).str);
  757. AsmLn;
  758. end;
  759. ait_regalloc,
  760. ait_tempalloc:
  761. ;
  762. ait_section:
  763. begin
  764. {if LasTSec<>sec_none then
  765. AsmWriteLn('_'+target_asm.secnames[LasTSec]+#9#9'ENDS');}
  766. if tai_section(hp).sec<>sec_none then
  767. begin
  768. AsmLn;
  769. AsmWriteLn(#9+target_asm.secnames[tai_section(hp).sec]);
  770. {$ifdef GDB}
  771. lastfileinfo.line:=-1;
  772. {$endif GDB}
  773. end;
  774. LasTSec:=tai_section(hp).sec;
  775. end;
  776. ait_align:
  777. begin
  778. case tai_align(hp).aligntype of
  779. 1:AsmWriteLn(#9'align 0');
  780. 2:AsmWriteLn(#9'align 1');
  781. 4:AsmWriteLn(#9'align 2');
  782. otherwise internalerror(2002110302);
  783. end;
  784. end;
  785. ait_datablock:
  786. begin
  787. s:= tai_datablock(hp).sym.name;
  788. replaced:= ReplaceForbiddenChars(s);
  789. if tai_datablock(hp).is_global then
  790. if replaced then
  791. AsmWriteLn(#9'export'#9+s+'[RW] => '''+tai_datablock(hp).sym.name+'''')
  792. else
  793. AsmWriteLn(#9'export'#9+s+'[RW]');
  794. if not macos_direct_globals then
  795. begin
  796. AsmWriteLn(#9'toc');
  797. AsmWriteLn(#9'tc'#9+s+'[TC], '+s+'[RW]');
  798. AsmWriteLn(#9'csect'#9+s+'[RW]');
  799. AsmWriteLn(#9'ds.b '+tostr(tai_datablock(hp).size));
  800. end
  801. else
  802. begin
  803. AsmWriteLn(#9'csect'#9+s+'[TC]');
  804. AsmWriteLn(PadTabs(s+':',#0)+'ds.b '+tostr(tai_datablock(hp).size));
  805. {TODO: ? PadTabs(s,#0) }
  806. end;
  807. end;
  808. ait_const_32bit,
  809. ait_const_8bit,
  810. ait_const_16bit :
  811. begin
  812. AsmWrite(ait_const2str[hp.typ]+tostr(tai_const(hp).value));
  813. consttyp:=hp.typ;
  814. l:=0;
  815. repeat
  816. found:=(not (tai(hp.next)=nil)) and (tai(hp.next).typ=consttyp);
  817. if found then
  818. begin
  819. hp:=tai(hp.next);
  820. s:=','+tostr(tai_const(hp).value);
  821. AsmWrite(s);
  822. inc(l,length(s));
  823. end;
  824. until (not found) or (l>line_length);
  825. AsmLn;
  826. end;
  827. ait_const_symbol:
  828. begin
  829. (*
  830. AsmWriteLn(#9#9'dd'#9'offset '+tai_const_symbol(hp).sym.name);
  831. if tai_const_symbol(hp).offset>0 then
  832. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  833. else if tai_const_symbol(hp).offset<0 then
  834. AsmWrite(tostr(tai_const_symbol(hp).offset));
  835. AsmLn;
  836. *)
  837. s:= tai_const_symbol(hp).sym.name;
  838. ReplaceForbiddenChars(s);
  839. if tai_const_symbol(hp).sym.typ = AT_FUNCTION then
  840. begin
  841. if use_PR then
  842. AsmWriteLn(#9'dc.l'#9'.'+ s +'[PR]')
  843. else
  844. AsmWriteLn(#9'dc.l'#9 + s + '[DS]')
  845. end
  846. else
  847. begin
  848. if macos_direct_globals then
  849. AsmWriteLn(#9'dc.l'#9+s)
  850. else
  851. AsmWriteLn(#9'dc.l'#9+s+const_storage_class);
  852. end;
  853. (* TODO: the following might need to be included. Temporaily we
  854. generate an error
  855. if tai_const_symbol(hp).offset>0 then
  856. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  857. else if tai_const_symbol(hp).offset<0 then
  858. AsmWrite(tostr(tai_const_symbol(hp).offset));
  859. *)
  860. if tai_const_symbol(hp).offset <> 0 then
  861. InternalError(2002110101);
  862. AsmLn;
  863. end;
  864. ait_real_32bit:
  865. AsmWriteLn(#9'dc.l'#9'"'+single2str(tai_real_32bit(hp).value)+'"');
  866. ait_real_64bit:
  867. AsmWriteLn(#9'dc.d'#9'"'+double2str(tai_real_64bit(hp).value)+'"');
  868. ait_string:
  869. begin
  870. {NOTE When a single quote char is encountered, it is
  871. replaced with a numeric ascii value. It could also
  872. have been replaced with the escape seq of double quotes.}
  873. counter := 0;
  874. lines := tai_string(hp).len div line_length;
  875. { separate lines in different parts }
  876. if tai_string(hp).len > 0 then
  877. Begin
  878. for j := 0 to lines-1 do
  879. begin
  880. AsmWrite(#9'dc.b'#9);
  881. quoted:=false;
  882. for i:=counter to counter+line_length do
  883. begin
  884. { it is an ascii character. }
  885. if (ord(tai_string(hp).str[i])>31) and
  886. (ord(tai_string(hp).str[i])<128) and
  887. (tai_string(hp).str[i]<>'''') then
  888. begin
  889. if not(quoted) then
  890. begin
  891. if i>counter then
  892. AsmWrite(',');
  893. AsmWrite('''');
  894. end;
  895. AsmWrite(tai_string(hp).str[i]);
  896. quoted:=true;
  897. end { if > 31 and < 128 and ord('"') }
  898. else
  899. begin
  900. if quoted then
  901. AsmWrite('''');
  902. if i>counter then
  903. AsmWrite(',');
  904. quoted:=false;
  905. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  906. end;
  907. end; { end for i:=0 to... }
  908. if quoted then AsmWrite('''');
  909. AsmWrite(target_info.newline);
  910. counter := counter+line_length;
  911. end; { end for j:=0 ... }
  912. { do last line of lines }
  913. AsmWrite(#9'dc.b'#9);
  914. quoted:=false;
  915. for i:=counter to tai_string(hp).len-1 do
  916. begin
  917. { it is an ascii character. }
  918. if (ord(tai_string(hp).str[i])>31) and
  919. (ord(tai_string(hp).str[i])<128) and
  920. (tai_string(hp).str[i]<>'''') then
  921. begin
  922. if not(quoted) then
  923. begin
  924. if i>counter then
  925. AsmWrite(',');
  926. AsmWrite('''');
  927. end;
  928. AsmWrite(tai_string(hp).str[i]);
  929. quoted:=true;
  930. end { if > 31 and < 128 and " }
  931. else
  932. begin
  933. if quoted then
  934. AsmWrite('''');
  935. if i>counter then
  936. AsmWrite(',');
  937. quoted:=false;
  938. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  939. end;
  940. end; { end for i:=0 to... }
  941. if quoted then
  942. AsmWrite('''');
  943. end;
  944. AsmLn;
  945. end;
  946. ait_label:
  947. begin
  948. if tai_label(hp).l.is_used then
  949. begin
  950. s:= tai_label(hp).l.name;
  951. ReplaceForbiddenChars(s);
  952. if s[1] = '@' then
  953. AsmWriteLn(s+':')
  954. else
  955. begin
  956. if not macos_direct_globals then
  957. begin
  958. AsmWriteLn(#9'toc');
  959. AsmWriteLn(#9'tc'#9+s+'[TC], '+s+const_storage_class);
  960. AsmWriteLn(#9'csect'#9+s+const_storage_class);
  961. end
  962. else
  963. begin
  964. AsmWriteLn(#9'csect'#9+s+'[TC]');
  965. AsmWriteLn(PadTabs(s+':',#0));
  966. end;
  967. end;
  968. end;
  969. end;
  970. ait_direct:
  971. begin
  972. AsmWritePChar(tai_direct(hp).str);
  973. AsmLn;
  974. end;
  975. ait_symbol:
  976. begin
  977. if tai_symbol(hp).sym.typ=AT_FUNCTION then
  978. GenProcedureHeader(hp)
  979. else if tai_symbol(hp).sym.typ=AT_DATA then
  980. begin
  981. s:= tai_symbol(hp).sym.name;
  982. replaced:= ReplaceForbiddenChars(s);
  983. if tai_symbol(hp).is_global then
  984. if replaced then
  985. AsmWriteLn(#9'export'#9+s+'[RW] => '''+tai_symbol(hp).sym.name+'''')
  986. else
  987. AsmWriteLn(#9'export'#9+s+'[RW]');
  988. if not macos_direct_globals then
  989. begin
  990. AsmWriteLn(#9'toc');
  991. AsmWriteLn(#9'tc'#9+s+'[TC], '+s+ const_storage_class);
  992. AsmWriteLn(#9'csect'#9+s+ const_storage_class);
  993. end
  994. else
  995. begin
  996. AsmWriteLn(#9'csect'#9+s+'[TC]');
  997. AsmWriteLn(s+':');
  998. end;
  999. end
  1000. else
  1001. InternalError(2003071301);
  1002. end;
  1003. ait_symbol_end:
  1004. {$ifdef GDB}
  1005. if isInFunction then
  1006. if ((cs_debuginfo in aktmoduleswitches) or
  1007. (cs_gdb_lineinfo in aktglobalswitches)) then
  1008. begin
  1009. //info for debuggers:
  1010. AsmWriteLn(#9'endf ' + tostr(stabslastfileinfo.line));
  1011. isInFunction:= false;
  1012. end
  1013. {$endif GDB}
  1014. ;
  1015. ait_instruction:
  1016. AsmWriteLn(GetInstruction(hp));
  1017. {$ifdef GDB}
  1018. ait_stabn: ;
  1019. ait_stabs: ;
  1020. ait_force_line :
  1021. stabslastfileinfo.line:=0;
  1022. ait_stab_function_name: ;
  1023. {$endif GDB}
  1024. ait_cut :
  1025. begin
  1026. { only reset buffer if nothing has changed }
  1027. if AsmSize=AsmStartSize then
  1028. AsmClear
  1029. else
  1030. begin
  1031. {
  1032. if LasTSec<>sec_none then
  1033. AsmWriteLn('_'+target_asm.secnames[LasTSec]+#9#9'ends');
  1034. AsmLn;
  1035. }
  1036. AsmWriteLn(#9'end');
  1037. AsmClose;
  1038. DoAssemble;
  1039. AsmCreate(tai_cut(hp).place);
  1040. end;
  1041. { avoid empty files }
  1042. while assigned(hp.next) and (tai(hp.next).typ in [ait_cut,ait_section,ait_comment]) do
  1043. begin
  1044. if tai(hp.next).typ=ait_section then
  1045. begin
  1046. lasTSec:=tai_section(hp.next).sec;
  1047. end;
  1048. hp:=tai(hp.next);
  1049. end;
  1050. WriteAsmFileHeader;
  1051. if lasTSec<>sec_none then
  1052. AsmWriteLn(#9+target_asm.secnames[lasTSec]);
  1053. { AsmWriteLn('_'+target_asm.secnames[lasTSec]+#9#9+
  1054. 'SEGMENT'#9'PARA PUBLIC USE32 '''+
  1055. target_asm.secnames[lasTSec]+'''');
  1056. }
  1057. AsmStartSize:=AsmSize;
  1058. end;
  1059. ait_marker :
  1060. begin
  1061. if tai_marker(hp).kind=InlineStart then
  1062. inc(InlineLevel)
  1063. else if tai_marker(hp).kind=InlineEnd then
  1064. dec(InlineLevel);
  1065. end;
  1066. else
  1067. internalerror(2002110303);
  1068. end;
  1069. hp:=tai(hp.next);
  1070. end;
  1071. end;
  1072. var
  1073. currentasmlist : TExternalAssembler;
  1074. procedure writeexternal(p:tnamedindexitem;arg:pointer);
  1075. var
  1076. s:string;
  1077. begin
  1078. if tasmsymbol(p).defbind=AB_EXTERNAL then
  1079. begin
  1080. //Writeln('ZZZ ',p.name,' ',p.classname,' ',Ord(tasmsymbol(p).typ));
  1081. s:= p.name;
  1082. case tasmsymbol(p).typ of
  1083. AT_FUNCTION:
  1084. begin
  1085. if ReplaceForbiddenChars(s) then
  1086. begin
  1087. if not use_PR then
  1088. currentasmlist.AsmWriteLn(#9'import'#9'.'+s+' <= ''.'+p.name+'''')
  1089. else
  1090. currentasmlist.AsmWriteLn(#9'import'#9'.'+s+'[PR] <= ''.'+p.name+'[PR]''');
  1091. currentasmlist.AsmWriteLn(#9'import'#9+s+'[DS] <= '''+p.name+'[DS]''');
  1092. end
  1093. else
  1094. begin
  1095. if not use_PR then
  1096. currentasmlist.AsmWriteLn(#9'import'#9'.'+s)
  1097. else
  1098. currentasmlist.AsmWriteLn(#9'import'#9'.'+s+'[PR]');
  1099. currentasmlist.AsmWriteLn(#9'import'#9+s+'[DS]');
  1100. end;
  1101. currentasmlist.AsmWriteLn(#9'toc');
  1102. currentasmlist.AsmWriteLn(#9'tc'#9+s+'[TC],'+s+'[DS]');
  1103. end
  1104. else
  1105. begin
  1106. if ReplaceForbiddenChars(s) then
  1107. currentasmlist.AsmWriteLn(#9'import'#9+s+'[RW] <= '''+p.name+'''')
  1108. else
  1109. currentasmlist.AsmWriteLn(#9'import'#9+s+'[RW]');
  1110. currentasmlist.AsmWriteLn(#9'toc');
  1111. currentasmlist.AsmWriteLn(#9'tc'#9+s+'[TC],'+s+'[RW]');
  1112. end;
  1113. end;
  1114. end;
  1115. end;
  1116. procedure TPPCMPWAssembler.WriteExternals;
  1117. begin
  1118. currentasmlist:=self;
  1119. objectlibrary.symbolsearch.foreach_static({$ifdef fpcprocvar}@{$endif}writeexternal,nil);
  1120. end;
  1121. function TPPCMPWAssembler.DoAssemble : boolean;
  1122. var f : file;
  1123. begin
  1124. DoAssemble:=Inherited DoAssemble;
  1125. (*
  1126. { masm does not seem to recognize specific extensions and uses .obj allways PM }
  1127. if (aktoutputformat = as_i386_masm) then
  1128. begin
  1129. if not(cs_asm_extern in aktglobalswitches) then
  1130. begin
  1131. if Not FileExists(objfile) and
  1132. FileExists(ForceExtension(objfile,'.obj')) then
  1133. begin
  1134. Assign(F,ForceExtension(objfile,'.obj'));
  1135. Rename(F,objfile);
  1136. end;
  1137. end
  1138. else
  1139. AsmRes.AddAsmCommand('mv',ForceExtension(objfile,'.obj')+' '+objfile,objfile);
  1140. end;
  1141. *)
  1142. end;
  1143. procedure TPPCMPWAssembler.WriteAsmFileHeader;
  1144. begin
  1145. (*
  1146. AsmWriteLn(#9'.386p');
  1147. { masm 6.11 does not seem to like LOCALS PM }
  1148. if (aktoutputformat = as_i386_tasm) then
  1149. begin
  1150. AsmWriteLn(#9'LOCALS '+target_asm.labelprefix);
  1151. end;
  1152. AsmWriteLn('DGROUP'#9'GROUP'#9'_BSS,_DATA');
  1153. AsmWriteLn(#9'ASSUME'#9'CS:_CODE,ES:DGROUP,DS:DGROUP,SS:DGROUP');
  1154. AsmLn;
  1155. *)
  1156. AsmWriteLn(#9'string asis'); {Interpret strings just to be the content between the quotes.}
  1157. AsmWriteLn(#9'aligning off'); {We do our own aligning.}
  1158. AsmLn;
  1159. end;
  1160. procedure TPPCMPWAssembler.WriteAsmList;
  1161. {$ifdef GDB}
  1162. var
  1163. fileinfo : tfileposinfo;
  1164. {$endif GDB}
  1165. begin
  1166. {$ifdef EXTDEBUG}
  1167. if assigned(current_module.mainsource) then
  1168. comment(v_info,'Start writing MPW-styled assembler output for '+current_module.mainsource^);
  1169. {$endif}
  1170. LasTSec:=sec_none;
  1171. {$ifdef GDB}
  1172. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  1173. {$endif GDB}
  1174. {$ifdef GDB}
  1175. //n_line:=n_bssline;
  1176. funcname:=nil;
  1177. linecount:=1;
  1178. includecount:=0;
  1179. fileinfo.fileindex:=1;
  1180. fileinfo.line:=1;
  1181. isInFunction:= false;
  1182. firstLineInFunction:= 0;
  1183. { Write main file }
  1184. WriteFileLineInfo(fileinfo);
  1185. {$endif GDB}
  1186. WriteAsmFileHeader;
  1187. WriteExternals;
  1188. { PowerPC MPW ASM doesn't support stabs, as we know.
  1189. WriteTree(debuglist);}
  1190. WriteTree(codesegment);
  1191. WriteTree(datasegment);
  1192. WriteTree(consts);
  1193. WriteTree(rttilist);
  1194. WriteTree(resourcestringlist);
  1195. WriteTree(bsssegment);
  1196. {$ifdef GDB}
  1197. WriteFileEndInfo;
  1198. {$ENDIF}
  1199. AsmWriteLn(#9'end');
  1200. AsmLn;
  1201. {$ifdef EXTDEBUG}
  1202. if assigned(current_module.mainsource) then
  1203. comment(v_info,'Done writing MPW-styled assembler output for '+current_module.mainsource^);
  1204. {$endif EXTDEBUG}
  1205. end;
  1206. {*****************************************************************************
  1207. Initialize
  1208. *****************************************************************************}
  1209. const
  1210. as_powerpc_mpw_info : tasminfo =
  1211. (
  1212. id : as_powerpc_mpw;
  1213. idtxt : 'MPW';
  1214. asmbin : 'PPCAsm';
  1215. asmcmd : '';
  1216. supported_target : system_any; { what should I write here ?? }
  1217. outputbinary: false;
  1218. allowdirect : true;
  1219. needar : true;
  1220. labelprefix_only_inside_procedure : true;
  1221. labelprefix : '@';
  1222. comment : '; ';
  1223. secnames : ('',
  1224. 'csect','csect [TC]','csect [TC]', {TODO: Perhaps use other section types.}
  1225. '','','','','','',
  1226. '','','')
  1227. );
  1228. initialization
  1229. RegisterAssembler(as_powerpc_mpw_info,TPPCMPWAssembler);
  1230. end.
  1231. {
  1232. $Log$
  1233. Revision 1.23 2003-08-24 21:40:12 olle
  1234. * minor adjustment
  1235. Revision 1.21 2003/08/18 11:47:15 olle
  1236. + added asm directive ALIGNING OFF to avoid unexpected aligning by the assembler
  1237. Revision 1.20 2002/10/01 05:17:27 olle
  1238. * minor fix
  1239. Revision 1.19 2003/04/06 21:01:40 olle
  1240. + line numbers are now emitted in the assembler code
  1241. * bug in export and import directive fixed
  1242. * made code more in sync with aggas.pas
  1243. Revision 1.18 2003/01/13 17:17:50 olle
  1244. * changed global var access, TOC now contain pointers to globals
  1245. * fixed handling of function pointers
  1246. Revision 1.17 2003/01/08 18:43:57 daniel
  1247. * Tregister changed into a record
  1248. Revision 1.16 2002/11/28 10:56:07 olle
  1249. * changed proc ref from .xxx[PR] (refering to its section)
  1250. to .xxx (refering to its label) to allow for multiple ref to a proc.
  1251. Revision 1.15 2002/11/17 16:31:59 carl
  1252. * memory optimization (3-4%) : cleanup of tai fields,
  1253. cleanup of tdef and tsym fields.
  1254. * make it work for m68k
  1255. Revision 1.14 2002/11/07 15:50:23 jonas
  1256. * fixed bctr(l) problems
  1257. Revision 1.13 2002/11/04 18:24:53 olle
  1258. * globals are located in TOC and relative r2, instead of absolute
  1259. * symbols which only differs in case are treated as a single symbol
  1260. + tai_const_symbol supported
  1261. * only refs_full accepted
  1262. Revision 1.12 2002/10/23 15:31:01 olle
  1263. * branch b does not jump to dotted symbol now
  1264. Revision 1.11 2002/10/19 23:52:40 olle
  1265. * import directive changed
  1266. Revision 1.10 2002/10/10 19:39:37 florian
  1267. * changes from Olle to get simple programs compiled and assembled
  1268. Revision 1.9 2002/10/07 21:19:53 florian
  1269. * more mpw fixes
  1270. Revision 1.8 2002/10/06 22:46:20 florian
  1271. * fixed function exporting
  1272. Revision 1.7 2002/10/02 22:14:15 florian
  1273. * improve function imports
  1274. Revision 1.6 2002/09/27 21:09:49 florian
  1275. + readed because previous version was broken
  1276. Revision 1.2 2002/08/31 12:43:31 florian
  1277. * ppc compilation fixed
  1278. Revision 1.1 2002/08/20 21:40:44 florian
  1279. + target macos for ppc added
  1280. + frame work for mpw assembler output
  1281. }