agppcmpw.pas 41 KB

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