agppcmpw.pas 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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,A_BLA:
  335. s:=#9+op2str[op]+#9;
  336. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  337. s:=#9+op2str[op];
  338. A_BL:
  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 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 now.
  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.22 2003-08-22 12:30:43 olle
  1234. + added xxx_regname stuff
  1235. * made the mpw asm writer work again
  1236. Revision 1.21 2003/08/18 11:47:15 olle
  1237. + added asm directive ALIGNING OFF to avoid unexpected aligning by the assembler
  1238. Revision 1.20 2002/10/01 05:17:27 olle
  1239. * minor fix
  1240. Revision 1.19 2003/04/06 21:01:40 olle
  1241. + line numbers are now emitted in the assembler code
  1242. * bug in export and import directive fixed
  1243. * made code more in sync with aggas.pas
  1244. Revision 1.18 2003/01/13 17:17:50 olle
  1245. * changed global var access, TOC now contain pointers to globals
  1246. * fixed handling of function pointers
  1247. Revision 1.17 2003/01/08 18:43:57 daniel
  1248. * Tregister changed into a record
  1249. Revision 1.16 2002/11/28 10:56:07 olle
  1250. * changed proc ref from .xxx[PR] (refering to its section)
  1251. to .xxx (refering to its label) to allow for multiple ref to a proc.
  1252. Revision 1.15 2002/11/17 16:31:59 carl
  1253. * memory optimization (3-4%) : cleanup of tai fields,
  1254. cleanup of tdef and tsym fields.
  1255. * make it work for m68k
  1256. Revision 1.14 2002/11/07 15:50:23 jonas
  1257. * fixed bctr(l) problems
  1258. Revision 1.13 2002/11/04 18:24:53 olle
  1259. * globals are located in TOC and relative r2, instead of absolute
  1260. * symbols which only differs in case are treated as a single symbol
  1261. + tai_const_symbol supported
  1262. * only refs_full accepted
  1263. Revision 1.12 2002/10/23 15:31:01 olle
  1264. * branch b does not jump to dotted symbol now
  1265. Revision 1.11 2002/10/19 23:52:40 olle
  1266. * import directive changed
  1267. Revision 1.10 2002/10/10 19:39:37 florian
  1268. * changes from Olle to get simple programs compiled and assembled
  1269. Revision 1.9 2002/10/07 21:19:53 florian
  1270. * more mpw fixes
  1271. Revision 1.8 2002/10/06 22:46:20 florian
  1272. * fixed function exporting
  1273. Revision 1.7 2002/10/02 22:14:15 florian
  1274. * improve function imports
  1275. Revision 1.6 2002/09/27 21:09:49 florian
  1276. + readed because previous version was broken
  1277. Revision 1.2 2002/08/31 12:43:31 florian
  1278. * ppc compilation fixed
  1279. Revision 1.1 2002/08/20 21:40:44 florian
  1280. + target macos for ppc added
  1281. + frame work for mpw assembler output
  1282. }