agjasmin.pas 30 KB

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