agjasmin.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. {
  2. Copyright (c) 1998-2010 by the Free Pascal team
  3. This unit implements the Jasmin assembler writer
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { Unit for writing Jasmin assembler (JVM bytecode) output.
  18. }
  19. unit agjasmin;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. cclasses,
  24. globtype,globals,
  25. symconst,symbase,symdef,symsym,
  26. aasmbase,aasmtai,aasmdata,aasmcpu,
  27. assemble;
  28. type
  29. TJasminInstrWriter = class;
  30. {# This is a derived class which is used to write
  31. Jasmin-styled assembler.
  32. }
  33. { TJasminAssembler }
  34. TJasminAssembler=class(texternalassembler)
  35. protected
  36. jasminjar: tcmdstr;
  37. asmfiles: TCmdStrList;
  38. procedure WriteExtraHeader(obj: tobjectdef);
  39. procedure WriteInstruction(hp: tai);
  40. procedure NewAsmFileForObjectDef(obj: tobjectdef);
  41. function VisibilityToStr(vis: tvisibility): string;
  42. function MethodDefinition(pd: tprocdef): string;
  43. function FieldDefinition(sym: tabstractvarsym): string;
  44. procedure WriteProcDef(pd: tprocdef);
  45. procedure WriteFieldSym(sym: tabstractvarsym);
  46. procedure WriteSymtableVarSyms(st: TSymtable);
  47. procedure WriteSymtableProcdefs(st: TSymtable);
  48. procedure WriteSymtableObjectDefs(st: TSymtable);
  49. public
  50. constructor Create(smart: boolean); override;
  51. function MakeCmdLine: TCmdStr;override;
  52. procedure WriteTree(p:TAsmList);override;
  53. procedure WriteAsmList;override;
  54. destructor destroy; override;
  55. protected
  56. InstrWriter: TJasminInstrWriter;
  57. end;
  58. {# This is the base class for writing instructions.
  59. The WriteInstruction() method must be overridden
  60. to write a single instruction to the assembler
  61. file.
  62. }
  63. { TJasminInstrWriter }
  64. TJasminInstrWriter = class
  65. constructor create(_owner: TJasminAssembler);
  66. procedure WriteInstruction(hp : tai); virtual;
  67. protected
  68. owner: TJasminAssembler;
  69. end;
  70. implementation
  71. uses
  72. SysUtils,
  73. cutils,cfileutl,systems,script,
  74. fmodule,finput,verbose,
  75. symtype,symtable,jvmdef,
  76. itcpujas,cpubase,cgutils,
  77. widestr
  78. ;
  79. const
  80. line_length = 70;
  81. type
  82. t64bitarray = array[0..7] of byte;
  83. t32bitarray = array[0..3] of byte;
  84. {****************************************************************************}
  85. { Support routines }
  86. {****************************************************************************}
  87. function fixline(s:string):string;
  88. {
  89. return s with all leading and ending spaces and tabs removed
  90. }
  91. var
  92. i,j,k : integer;
  93. begin
  94. i:=length(s);
  95. while (i>0) and (s[i] in [#9,' ']) do
  96. dec(i);
  97. j:=1;
  98. while (j<i) and (s[j] in [#9,' ']) do
  99. inc(j);
  100. for k:=j to i do
  101. if s[k] in [#0..#31,#127..#255] then
  102. s[k]:='.';
  103. fixline:=Copy(s,j,i-j+1);
  104. end;
  105. {****************************************************************************}
  106. { Jasmin Assembler writer }
  107. {****************************************************************************}
  108. destructor TJasminAssembler.Destroy;
  109. begin
  110. InstrWriter.free;
  111. asmfiles.free;
  112. inherited destroy;
  113. end;
  114. procedure TJasminAssembler.WriteTree(p:TAsmList);
  115. var
  116. ch : char;
  117. hp : tai;
  118. hp1 : tailineinfo;
  119. constdef : taiconst_type;
  120. s,t : string;
  121. i,pos,l : longint;
  122. InlineLevel : longint;
  123. last_align : longint;
  124. co : comp;
  125. sin : single;
  126. d : double;
  127. do_line : boolean;
  128. sepChar : char;
  129. begin
  130. if not assigned(p) then
  131. exit;
  132. last_align := 2;
  133. InlineLevel:=0;
  134. { lineinfo is only needed for al_procedures (PFV) }
  135. do_line:=(cs_asm_source in current_settings.globalswitches) or
  136. ((cs_lineinfo in current_settings.moduleswitches)
  137. and (p=current_asmdata.asmlists[al_procedures]));
  138. hp:=tai(p.first);
  139. while assigned(hp) do
  140. begin
  141. prefetch(pointer(hp.next)^);
  142. if not(hp.typ in SkipLineInfo) then
  143. begin
  144. hp1 := hp as tailineinfo;
  145. current_filepos:=hp1.fileinfo;
  146. { no line info for inlined code }
  147. if do_line and (inlinelevel=0) then
  148. begin
  149. { load infile }
  150. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  151. begin
  152. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  153. if assigned(infile) then
  154. begin
  155. { open only if needed !! }
  156. if (cs_asm_source in current_settings.globalswitches) then
  157. infile.open;
  158. end;
  159. { avoid unnecessary reopens of the same file !! }
  160. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  161. { be sure to change line !! }
  162. lastfileinfo.line:=-1;
  163. end;
  164. { write source }
  165. if (cs_asm_source in current_settings.globalswitches) and
  166. assigned(infile) then
  167. begin
  168. if (infile<>lastinfile) then
  169. begin
  170. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  171. if assigned(lastinfile) then
  172. lastinfile.close;
  173. end;
  174. if (hp1.fileinfo.line<>lastfileinfo.line) and
  175. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  176. begin
  177. if (hp1.fileinfo.line<>0) and
  178. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  179. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  180. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  181. { set it to a negative value !
  182. to make that is has been read already !! PM }
  183. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  184. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  185. end;
  186. end;
  187. lastfileinfo:=hp1.fileinfo;
  188. lastinfile:=infile;
  189. end;
  190. end;
  191. case hp.typ of
  192. ait_comment :
  193. Begin
  194. AsmWrite(target_asm.comment);
  195. AsmWritePChar(tai_comment(hp).str);
  196. AsmLn;
  197. End;
  198. ait_regalloc :
  199. begin
  200. if (cs_asm_regalloc in current_settings.globalswitches) then
  201. begin
  202. AsmWrite(#9+target_asm.comment+'Register ');
  203. repeat
  204. AsmWrite(std_regname(Tai_regalloc(hp).reg));
  205. if (hp.next=nil) or
  206. (tai(hp.next).typ<>ait_regalloc) or
  207. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  208. break;
  209. hp:=tai(hp.next);
  210. AsmWrite(',');
  211. until false;
  212. AsmWrite(' ');
  213. AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  214. end;
  215. end;
  216. ait_tempalloc :
  217. begin
  218. if (cs_asm_tempalloc in current_settings.globalswitches) then
  219. begin
  220. {$ifdef EXTDEBUG}
  221. if assigned(tai_tempalloc(hp).problem) then
  222. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  223. tostr(tai_tempalloc(hp).tempsize)+' '+tai_tempalloc(hp).problem^)
  224. else
  225. {$endif EXTDEBUG}
  226. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  227. tostr(tai_tempalloc(hp).tempsize)+' '+tempallocstr[tai_tempalloc(hp).allocation]);
  228. end;
  229. end;
  230. ait_align :
  231. begin
  232. end;
  233. ait_section :
  234. begin
  235. end;
  236. ait_datablock :
  237. begin
  238. internalerror(2010122701);
  239. end;
  240. ait_const:
  241. begin
  242. AsmWriteln('constant');
  243. // internalerror(2010122702);
  244. end;
  245. ait_real_64bit :
  246. begin
  247. internalerror(2010122703);
  248. end;
  249. ait_real_32bit :
  250. begin
  251. internalerror(2010122703);
  252. end;
  253. ait_comp_64bit :
  254. begin
  255. internalerror(2010122704);
  256. end;
  257. ait_string :
  258. begin
  259. pos:=0;
  260. for i:=1 to tai_string(hp).len do
  261. begin
  262. if pos=0 then
  263. begin
  264. AsmWrite(#9'strconst: '#9'"');
  265. pos:=20;
  266. end;
  267. ch:=tai_string(hp).str[i-1];
  268. case ch of
  269. #0, {This can't be done by range, because a bug in FPC}
  270. #1..#31,
  271. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  272. '"' : s:='\"';
  273. '\' : s:='\\';
  274. else
  275. s:=ch;
  276. end;
  277. AsmWrite(s);
  278. inc(pos,length(s));
  279. if (pos>line_length) or (i=tai_string(hp).len) then
  280. begin
  281. AsmWriteLn('"');
  282. pos:=0;
  283. end;
  284. end;
  285. end;
  286. ait_label :
  287. begin
  288. if (tai_label(hp).labsym.is_used) then
  289. begin
  290. AsmWrite(tai_label(hp).labsym.name);
  291. AsmWriteLn(':');
  292. end;
  293. end;
  294. ait_symbol :
  295. begin
  296. if (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  297. begin
  298. end
  299. else
  300. begin
  301. AsmWrite('data symbol: ');
  302. AsmWriteln(tai_symbol(hp).sym.name);
  303. // internalerror(2010122706);
  304. end;
  305. end;
  306. ait_symbol_end :
  307. begin
  308. end;
  309. ait_instruction :
  310. begin
  311. WriteInstruction(hp);
  312. end;
  313. ait_force_line,
  314. ait_function_name : ;
  315. ait_cutobject :
  316. begin
  317. end;
  318. ait_marker :
  319. if tai_marker(hp).kind=mark_NoLineInfoStart then
  320. inc(InlineLevel)
  321. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  322. dec(InlineLevel);
  323. ait_directive :
  324. begin
  325. AsmWrite('.'+directivestr[tai_directive(hp).directive]+' ');
  326. if assigned(tai_directive(hp).name) then
  327. AsmWrite(tai_directive(hp).name^);
  328. AsmLn;
  329. end;
  330. ait_jvar:
  331. begin
  332. AsmWrite('.var ');
  333. AsmWrite(tostr(tai_jvar(hp).stackslot));
  334. AsmWrite(' is ');
  335. AsmWrite(tai_jvar(hp).desc^);
  336. AsmWrite(' from ');
  337. AsmWrite(tai_jvar(hp).startlab.name);
  338. AsmWrite(' to ');
  339. AsmWriteLn(tai_jvar(hp).stoplab.name);
  340. end;
  341. ait_jcatch:
  342. begin
  343. AsmWrite('.catch ');
  344. AsmWrite(tai_jcatch(hp).name^);
  345. AsmWrite(' from ');
  346. AsmWrite(tai_jcatch(hp).startlab.name);
  347. AsmWrite(' to ');
  348. AsmWrite(tai_jcatch(hp).stoplab.name);
  349. AsmWrite(' using ');
  350. AsmWriteLn(tai_jcatch(hp).handlerlab.name);
  351. end;
  352. else
  353. internalerror(2010122707);
  354. end;
  355. hp:=tai(hp.next);
  356. end;
  357. end;
  358. procedure TJasminAssembler.WriteExtraHeader(obj: tobjectdef);
  359. var
  360. superclass,
  361. intf: tobjectdef;
  362. n: string;
  363. i: longint;
  364. begin
  365. { JVM 1.5+ }
  366. AsmWriteLn('.bytecode 49.0');
  367. // include files are not support by Java, and the directory of the main
  368. // source file must not be specified
  369. if assigned(current_module.mainsource) then
  370. n:=ExtractFileName(current_module.mainsource^)
  371. else
  372. n:=InputFileName;
  373. AsmWriteLn('.source '+ExtractFileName(n));
  374. { class/interface name }
  375. if not assigned(obj) then
  376. begin
  377. { fake class type for unit -> name=unitname and
  378. superclass=java.lang.object }
  379. AsmWriteLn('.class '+current_module.realmodulename^);
  380. AsmWriteLn('.super java/lang/Object');
  381. end
  382. else
  383. begin
  384. case obj.objecttype of
  385. odt_javaclass:
  386. begin
  387. AsmWriteLn('.class '+obj.objextname^);
  388. superclass:=obj.childof;
  389. end;
  390. odt_interfacejava:
  391. begin
  392. AsmWriteLn('.interface abstract '+obj.objextname^);
  393. { interfaces must always specify Java.lang.object as
  394. superclass }
  395. superclass:=java_jlobject;
  396. end
  397. else
  398. internalerror(2011010906);
  399. end;
  400. { superclass }
  401. if assigned(superclass) then
  402. begin
  403. AsmWrite('.super ');
  404. if assigned(superclass.import_lib) then
  405. AsmWrite(superclass.import_lib^+'/');
  406. AsmWriteln(superclass.objextname^);
  407. end;
  408. { implemented interfaces }
  409. if assigned(obj.ImplementedInterfaces) then
  410. begin
  411. for i:=0 to obj.ImplementedInterfaces.count-1 do
  412. begin
  413. intf:=TImplementedInterface(obj.ImplementedInterfaces[i]).IntfDef;
  414. AsmWrite('.implements ');
  415. if assigned(intf.import_lib) then
  416. AsmWrite(intf.import_lib^+'/');
  417. AsmWriteln(intf.objextname^);
  418. end;
  419. end;
  420. end;
  421. AsmLn;
  422. end;
  423. procedure TJasminAssembler.WriteInstruction(hp: tai);
  424. begin
  425. InstrWriter.WriteInstruction(hp);
  426. end;
  427. function TJasminAssembler.MakeCmdLine: TCmdStr;
  428. const
  429. jasminjarname = 'jasmin.jar';
  430. var
  431. filenames: tcmdstr;
  432. jasminjarfound: boolean;
  433. begin
  434. if jasminjar='' then
  435. begin
  436. jasminjarfound:=false;
  437. if utilsdirectory<>'' then
  438. jasminjarfound:=FindFile(jasminjarname,utilsdirectory,false,jasminjar);
  439. if not jasminjarfound then
  440. jasminjarfound:=FindFileInExeLocations(jasminjarname,false,jasminjar);
  441. if (not jasminjarfound) and not(cs_asm_extern in current_settings.globalswitches) then
  442. begin
  443. Message1(exec_e_assembler_not_found,jasminjarname);
  444. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  445. end;
  446. if jasminjarfound then
  447. Message1(exec_t_using_assembler,jasminjar);
  448. end;
  449. result:=target_asm.asmcmd;
  450. filenames:=maybequoted(ScriptFixFileName(AsmFileName));
  451. while not asmfiles.empty do
  452. filenames:=filenames+' '+asmfiles.GetFirst;
  453. Replace(result,'$ASM',filenames);
  454. if (path<>'') then
  455. Replace(result,'$OBJDIR',maybequoted(ScriptFixFileName(path)))
  456. else
  457. Replace(result,'$OBJDIR','.');
  458. Replace(result,'$JASMINJAR',maybequoted(ScriptFixFileName(jasminjar)));
  459. end;
  460. procedure TJasminAssembler.NewAsmFileForObjectDef(obj: tobjectdef);
  461. var
  462. enclosingobj: tobjectdef;
  463. st: tsymtable;
  464. begin
  465. if AsmSize<>AsmStartSize then
  466. begin
  467. AsmClose;
  468. asmfiles.Concat(maybequoted(ScriptFixFileName(AsmFileName)));
  469. end
  470. else
  471. AsmClear;
  472. AsmFileName:=obj.objextname^;
  473. st:=obj.owner;
  474. while assigned(st) and
  475. (st.symtabletype=objectsymtable) do
  476. begin
  477. { nested classes are named as "OuterClass$InnerClass" }
  478. enclosingobj:=tobjectdef(st.defowner);
  479. AsmFileName:=enclosingobj.objextname^+'$'+AsmFileName;
  480. st:=enclosingobj.owner;
  481. end;
  482. AsmFileName:=Path+FixFileName(AsmFileName)+target_info.asmext;
  483. AsmCreate(cut_normal);
  484. end;
  485. function TJasminAssembler.VisibilityToStr(vis: tvisibility): string;
  486. begin
  487. case vis of
  488. vis_hidden,
  489. vis_strictprivate:
  490. result:='private ';
  491. vis_strictprotected:
  492. result:='protected ';
  493. vis_protected,
  494. vis_private:
  495. { pick default visibility = "package" visibility; required because
  496. other classes in the same unit can also access these symbols }
  497. result:='';
  498. vis_public:
  499. result:='public '
  500. else
  501. internalerror(2010122609);
  502. end;
  503. end;
  504. function TJasminAssembler.MethodDefinition(pd: tprocdef): string;
  505. begin
  506. result:=VisibilityToStr(pd.visibility);
  507. if (pd.procsym.owner.symtabletype in [globalsymtable,staticsymtable,localsymtable]) or
  508. (po_staticmethod in pd.procoptions) then
  509. result:=result+'static ';
  510. if is_javainterface(tdef(pd.owner.defowner)) then
  511. result:=result+'abstract ';
  512. result:=result+pd.jvmmangledbasename;
  513. end;
  514. function TJasminAssembler.FieldDefinition(sym: tabstractvarsym): string;
  515. var
  516. vissym: tabstractvarsym;
  517. begin
  518. vissym:=sym;
  519. { static field definition -> get original field definition for
  520. visibility }
  521. if (vissym.typ=staticvarsym) and
  522. (vissym.owner.symtabletype=objectsymtable) then
  523. begin
  524. vissym:=tabstractvarsym(search_struct_member(
  525. tobjectdef(vissym.owner.defowner),
  526. jvminternalstaticfieldname(vissym.name)));
  527. if not assigned(vissym) or
  528. (vissym.typ<>fieldvarsym) then
  529. internalerror(2011011501);
  530. end;
  531. case vissym.typ of
  532. staticvarsym:
  533. begin
  534. if vissym.owner.symtabletype=globalsymtable then
  535. result:='public '
  536. else
  537. { package visbility }
  538. result:='';
  539. end;
  540. fieldvarsym:
  541. result:=VisibilityToStr(tfieldvarsym(vissym).visibility);
  542. else
  543. internalerror(2011011204);
  544. end;
  545. if (vissym.owner.symtabletype in [staticsymtable,globalsymtable]) or
  546. (sp_static in vissym.symoptions) then
  547. result:=result+'static ';
  548. result:=result+sym.jvmmangledbasename;
  549. end;
  550. procedure TJasminAssembler.WriteProcDef(pd: tprocdef);
  551. begin
  552. if not assigned(pd.exprasmlist) and
  553. (not is_javainterface(pd.struct) or
  554. (pd.proctypeoption in [potype_unitinit,potype_unitfinalize])) then
  555. exit;
  556. AsmWrite('.method ');
  557. AsmWriteln(MethodDefinition(pd));
  558. WriteTree(pd.exprasmlist);
  559. AsmWriteln('.end method');
  560. AsmLn;
  561. end;
  562. procedure TJasminAssembler.WriteFieldSym(sym: tabstractvarsym);
  563. begin
  564. { internal static field definition alias -> skip }
  565. if sp_static in sym.symoptions then
  566. exit;
  567. AsmWrite('.field ');
  568. AsmWriteln(FieldDefinition(sym));
  569. end;
  570. procedure TJasminAssembler.WriteSymtableVarSyms(st: TSymtable);
  571. var
  572. sym : tsym;
  573. i : longint;
  574. begin
  575. if not assigned(st) then
  576. exit;
  577. for i:=0 to st.SymList.Count-1 do
  578. begin
  579. sym:=tsym(st.SymList[i]);
  580. case sym.typ of
  581. staticvarsym,
  582. fieldvarsym:
  583. begin
  584. WriteFieldSym(tabstractvarsym(sym));
  585. end;
  586. end;
  587. end;
  588. end;
  589. procedure TJasminAssembler.WriteSymtableProcdefs(st: TSymtable);
  590. var
  591. i : longint;
  592. def : tdef;
  593. begin
  594. if not assigned(st) then
  595. exit;
  596. for i:=0 to st.DefList.Count-1 do
  597. begin
  598. def:=tdef(st.DefList[i]);
  599. case def.typ of
  600. procdef :
  601. begin
  602. { methods are also in the static/globalsymtable of the unit
  603. -> make sure they are only written for the objectdefs that
  604. own them }
  605. if not(st.symtabletype in [staticsymtable,globalsymtable]) or
  606. (def.owner=st) then
  607. begin
  608. WriteProcDef(tprocdef(def));
  609. if assigned(tprocdef(def).localst) then
  610. WriteSymtableProcdefs(tprocdef(def).localst);
  611. end;
  612. end;
  613. end;
  614. end;
  615. end;
  616. procedure TJasminAssembler.WriteSymtableObjectDefs(st: TSymtable);
  617. var
  618. i : longint;
  619. def : tdef;
  620. obj : tobjectdef;
  621. nestedclasses: tfpobjectlist;
  622. begin
  623. if not assigned(st) then
  624. exit;
  625. nestedclasses:=tfpobjectlist.create(false);
  626. for i:=0 to st.DefList.Count-1 do
  627. begin
  628. def:=tdef(st.DefList[i]);
  629. case def.typ of
  630. objectdef:
  631. if not(oo_is_external in tobjectdef(def).objectoptions) then
  632. nestedclasses.add(def);
  633. end;
  634. end;
  635. for i:=0 to nestedclasses.count-1 do
  636. begin
  637. obj:=tobjectdef(nestedclasses[i]);
  638. NewAsmFileForObjectDef(obj);
  639. WriteExtraHeader(obj);
  640. WriteSymtableVarSyms(obj.symtable);
  641. AsmLn;
  642. WriteSymtableProcDefs(obj.symtable);
  643. WriteSymtableObjectDefs(obj.symtable);
  644. end;
  645. nestedclasses.free;
  646. end;
  647. constructor TJasminAssembler.Create(smart: boolean);
  648. begin
  649. inherited create(smart);
  650. InstrWriter:=TJasminInstrWriter.Create(self);
  651. asmfiles:=TCmdStrList.Create;
  652. end;
  653. procedure TJasminAssembler.WriteAsmList;
  654. var
  655. hal : tasmlisttype;
  656. i: longint;
  657. begin
  658. {$ifdef EXTDEBUG}
  659. if assigned(current_module.mainsource) then
  660. Comment(V_Debug,'Start writing Jasmin-styled assembler output for '+current_module.mainsource^);
  661. {$endif}
  662. AsmStartSize:=AsmSize;
  663. WriteExtraHeader(nil);
  664. (*
  665. for hal:=low(TasmlistType) to high(TasmlistType) do
  666. begin
  667. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  668. writetree(current_asmdata.asmlists[hal]);
  669. AsmWriteLn(target_asm.comment+'End asmlist '+AsmlistTypeStr[hal]);
  670. end;
  671. *)
  672. { print all global variables }
  673. WriteSymtableVarSyms(current_module.globalsymtable);
  674. WriteSymtableVarSyms(current_module.localsymtable);
  675. AsmLn;
  676. { print all global procedures/functions }
  677. WriteSymtableProcdefs(current_module.globalsymtable);
  678. WriteSymtableProcdefs(current_module.localsymtable);
  679. WriteSymtableObjectDefs(current_module.globalsymtable);
  680. WriteSymtableObjectDefs(current_module.localsymtable);
  681. AsmLn;
  682. {$ifdef EXTDEBUG}
  683. if assigned(current_module.mainsource) then
  684. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource^);
  685. {$endif EXTDEBUG}
  686. end;
  687. {****************************************************************************}
  688. { Jasmin Instruction Writer }
  689. {****************************************************************************}
  690. constructor TJasminInstrWriter.create(_owner: TJasminAssembler);
  691. begin
  692. inherited create;
  693. owner := _owner;
  694. end;
  695. function getreferencestring(var ref : treference) : string;
  696. begin
  697. if (ref.arrayreftype<>art_none) or
  698. (ref.index<>NR_NO) then
  699. internalerror(2010122809);
  700. if assigned(ref.symbol) then
  701. begin
  702. // global symbol or field -> full type and name
  703. // ref.base can be <> NR_NO in case an instance field is loaded.
  704. // This register is not part of this instruction, it will have
  705. // been placed on the stack by the previous one.
  706. if (ref.offset<>0) then
  707. internalerror(2010122811);
  708. result:=ref.symbol.name;
  709. end
  710. else
  711. begin
  712. // local symbol -> stack slot, stored in offset
  713. if ref.base<>NR_STACK_POINTER_REG then
  714. internalerror(2010122810);
  715. result:=tostr(ref.offset);
  716. end;
  717. end;
  718. function getopstr(const o:toper) : ansistring;
  719. var
  720. i,runstart,runlen: longint;
  721. num: string[4];
  722. begin
  723. case o.typ of
  724. top_reg:
  725. // should have been translated into a memory location by the
  726. // register allocator)
  727. if (cs_no_regalloc in current_settings.globalswitches) then
  728. getopstr:=std_regname(o.reg)
  729. else
  730. internalerror(2010122803);
  731. top_const:
  732. str(o.val,result);
  733. top_ref:
  734. getopstr:=getreferencestring(o.ref^);
  735. top_single:
  736. str(o.sval:0:20,result);
  737. top_double:
  738. begin
  739. str(o.dval:0:20,result);
  740. // force interpretation as double
  741. result:=result+'d';
  742. end;
  743. top_string:
  744. begin
  745. { escape control codes }
  746. runlen:=0;
  747. runstart:=0;
  748. for i:=1 to o.pcvallen do
  749. begin
  750. if o.pcval[i]<#32 then
  751. begin
  752. if runlen>0 then
  753. begin
  754. setlength(result,length(result)+runlen);
  755. move(result[length(result)-runlen],o.pcval[runstart],runlen);
  756. runlen:=0;
  757. end;
  758. result:=result+'\u'+hexstr(ord(o.pcval[i]),4);
  759. end
  760. else if o.pcval[i]<#127 then
  761. begin
  762. if runlen=0 then
  763. runstart:=i;
  764. inc(runlen);
  765. end
  766. else
  767. // since Jasmin expects an UTF-16 string, we can't safely
  768. // have high ASCII characters since they'll be
  769. // re-interpreted as utf-16 anyway
  770. internalerror(2010122808);
  771. end;
  772. if runlen>0 then
  773. begin
  774. setlength(result,length(result)+runlen);
  775. move(result[length(result)-runlen],o.pcval[runstart],runlen);
  776. end;
  777. end;
  778. top_wstring:
  779. begin
  780. { escape control codes }
  781. for i:=1 to getlengthwidestring(o.pwstrval) do
  782. begin
  783. if (o.pwstrval^.data[i]<32) or
  784. (o.pwstrval^.data[i]>127) then
  785. result:=result+'\u'+hexstr(o.pwstrval^.data[i],4)
  786. else
  787. result:=result+char(o.pwstrval^.data[i]);
  788. end;
  789. end
  790. else
  791. internalerror(2010122802);
  792. end;
  793. end;
  794. procedure TJasminInstrWriter.WriteInstruction(hp: tai);
  795. var
  796. s: ansistring;
  797. i: byte;
  798. sep: string[3];
  799. begin
  800. s:=#9+jas_op2str[taicpu(hp).opcode];
  801. if taicpu(hp).ops<>0 then
  802. begin
  803. sep:=#9;
  804. for i:=0 to taicpu(hp).ops-1 do
  805. begin
  806. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  807. sep:=' ';
  808. end;
  809. end;
  810. owner.AsmWriteLn(s);
  811. end;
  812. {****************************************************************************}
  813. { Jasmin Instruction Writer }
  814. {****************************************************************************}
  815. const
  816. as_jvm_jasmin_info : tasminfo =
  817. (
  818. id : as_jvm_jasmin;
  819. idtxt : 'Jasmin';
  820. asmbin : 'java';
  821. asmcmd : '-jar $JASMINJAR $ASM -d $OBJDIR';
  822. supported_targets : [system_jvm_java32];
  823. flags : [];
  824. labelprefix : 'L';
  825. comment : ' ; ';
  826. );
  827. begin
  828. RegisterAssembler(as_jvm_jasmin_info,TJasminAssembler);
  829. end.