agppcmpw.pas 37 KB

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