agjasmin.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. else
  331. internalerror(2010122707);
  332. end;
  333. hp:=tai(hp.next);
  334. end;
  335. end;
  336. procedure TJasminAssembler.WriteExtraHeader(obj: tobjectdef);
  337. var
  338. superclass,
  339. intf: tobjectdef;
  340. n: string;
  341. i: longint;
  342. begin
  343. { JVM 1.5+ }
  344. AsmWriteLn('.bytecode 49.0');
  345. // include files are not support by Java, and the directory of the main
  346. // source file must not be specified
  347. if assigned(current_module.mainsource) then
  348. n:=ExtractFileName(current_module.mainsource^)
  349. else
  350. n:=InputFileName;
  351. AsmWriteLn('.source '+ExtractFileName(n));
  352. { class/interface name }
  353. if not assigned(obj) then
  354. begin
  355. { fake class type for unit -> name=unitname and
  356. superclass=java.lang.object }
  357. AsmWriteLn('.class '+current_module.realmodulename^);
  358. AsmWriteLn('.super java/lang/Object');
  359. end
  360. else
  361. begin
  362. case obj.objecttype of
  363. odt_javaclass:
  364. begin
  365. AsmWriteLn('.class '+obj.objextname^);
  366. superclass:=obj.childof;
  367. end;
  368. odt_interfacejava:
  369. begin
  370. AsmWriteLn('.interface abstract '+obj.objextname^);
  371. { interfaces must always specify Java.lang.object as
  372. superclass }
  373. superclass:=java_jlobject;
  374. end
  375. else
  376. internalerror(2011010906);
  377. end;
  378. { superclass }
  379. if assigned(superclass) then
  380. begin
  381. AsmWrite('.super ');
  382. if assigned(superclass.import_lib) then
  383. AsmWrite(superclass.import_lib^+'/');
  384. AsmWriteln(superclass.objextname^);
  385. end;
  386. { implemented interfaces }
  387. if assigned(obj.ImplementedInterfaces) then
  388. begin
  389. for i:=0 to obj.ImplementedInterfaces.count-1 do
  390. begin
  391. intf:=TImplementedInterface(obj.ImplementedInterfaces[i]).IntfDef;
  392. AsmWrite('.implements ');
  393. if assigned(intf.import_lib) then
  394. AsmWrite(intf.import_lib^+'/');
  395. AsmWriteln(intf.objextname^);
  396. end;
  397. end;
  398. end;
  399. AsmLn;
  400. end;
  401. procedure TJasminAssembler.WriteInstruction(hp: tai);
  402. begin
  403. InstrWriter.WriteInstruction(hp);
  404. end;
  405. function TJasminAssembler.MakeCmdLine: TCmdStr;
  406. const
  407. jasminjarname = 'jasmin.jar';
  408. var
  409. filenames: tcmdstr;
  410. jasminjarfound: boolean;
  411. begin
  412. if jasminjar='' then
  413. begin
  414. jasminjarfound:=false;
  415. if utilsdirectory<>'' then
  416. jasminjarfound:=FindFile(jasminjarname,utilsdirectory,false,jasminjar);
  417. if not jasminjarfound then
  418. jasminjarfound:=FindFileInExeLocations(jasminjarname,false,jasminjar);
  419. if (not jasminjarfound) and not(cs_asm_extern in current_settings.globalswitches) then
  420. begin
  421. Message1(exec_e_assembler_not_found,jasminjarname);
  422. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  423. end;
  424. if jasminjarfound then
  425. Message1(exec_t_using_assembler,jasminjar);
  426. end;
  427. result:=target_asm.asmcmd;
  428. filenames:=maybequoted(ScriptFixFileName(AsmFileName));
  429. while not asmfiles.empty do
  430. filenames:=filenames+' '+asmfiles.GetFirst;
  431. Replace(result,'$ASM',filenames);
  432. if (path<>'') then
  433. Replace(result,'$OBJDIR',maybequoted(ScriptFixFileName(path)))
  434. else
  435. Replace(result,'$OBJDIR','.');
  436. Replace(result,'$JASMINJAR',maybequoted(ScriptFixFileName(jasminjar)));
  437. end;
  438. procedure TJasminAssembler.NewAsmFileForObjectDef(obj: tobjectdef);
  439. var
  440. enclosingobj: tobjectdef;
  441. st: tsymtable;
  442. begin
  443. if AsmSize<>AsmStartSize then
  444. begin
  445. AsmClose;
  446. asmfiles.Concat(maybequoted(ScriptFixFileName(AsmFileName)));
  447. end
  448. else
  449. AsmClear;
  450. AsmFileName:=obj.objextname^;
  451. st:=obj.owner;
  452. while assigned(st) and
  453. (st.symtabletype=objectsymtable) do
  454. begin
  455. { nested classes are named as "OuterClass$InnerClass" }
  456. enclosingobj:=tobjectdef(st.defowner);
  457. AsmFileName:=enclosingobj.objextname^+'$'+AsmFileName;
  458. st:=enclosingobj.owner;
  459. end;
  460. AsmFileName:=Path+FixFileName(AsmFileName)+target_info.asmext;
  461. AsmCreate(cut_normal);
  462. end;
  463. function TJasminAssembler.VisibilityToStr(vis: tvisibility): string;
  464. begin
  465. case vis of
  466. vis_hidden,
  467. vis_strictprivate:
  468. result:='private ';
  469. vis_strictprotected:
  470. result:='protected ';
  471. vis_protected,
  472. vis_private:
  473. { pick default visibility = "package" visibility; required because
  474. other classes in the same unit can also access these symbols }
  475. result:='';
  476. vis_public:
  477. result:='public '
  478. else
  479. internalerror(2010122609);
  480. end;
  481. end;
  482. function TJasminAssembler.MethodDefinition(pd: tprocdef): string;
  483. begin
  484. result:=VisibilityToStr(pd.visibility);
  485. if (pd.procsym.owner.symtabletype in [globalsymtable,staticsymtable,localsymtable]) or
  486. (po_staticmethod in pd.procoptions) then
  487. result:=result+'static ';
  488. if is_javainterface(tdef(pd.owner.defowner)) then
  489. result:=result+'abstract ';
  490. result:=result+pd.jvmmangledbasename;
  491. end;
  492. function TJasminAssembler.FieldDefinition(sym: tabstractvarsym): string;
  493. var
  494. vissym: tabstractvarsym;
  495. begin
  496. vissym:=sym;
  497. { static field definition -> get original field definition for
  498. visibility }
  499. if (vissym.typ=staticvarsym) and
  500. (vissym.owner.symtabletype=objectsymtable) then
  501. begin
  502. vissym:=tabstractvarsym(search_struct_member(
  503. tobjectdef(vissym.owner.defowner),
  504. jvminternalstaticfieldname(vissym.name)));
  505. if not assigned(vissym) or
  506. (vissym.typ<>fieldvarsym) then
  507. internalerror(2011011501);
  508. end;
  509. case vissym.typ of
  510. staticvarsym:
  511. begin
  512. if vissym.owner.symtabletype=globalsymtable then
  513. result:='public '
  514. else
  515. { package visbility }
  516. result:='';
  517. end;
  518. fieldvarsym:
  519. result:=VisibilityToStr(tfieldvarsym(vissym).visibility);
  520. else
  521. internalerror(2011011204);
  522. end;
  523. if (vissym.owner.symtabletype in [staticsymtable,globalsymtable]) or
  524. (sp_static in vissym.symoptions) then
  525. result:=result+'static ';
  526. result:=result+sym.jvmmangledbasename;
  527. end;
  528. procedure TJasminAssembler.WriteProcDef(pd: tprocdef);
  529. begin
  530. if not assigned(pd.exprasmlist) and
  531. (not is_javainterface(pd.struct) or
  532. (pd.proctypeoption in [potype_unitinit,potype_unitfinalize])) then
  533. exit;
  534. AsmWrite('.method ');
  535. AsmWriteln(MethodDefinition(pd));
  536. WriteTree(pd.exprasmlist);
  537. AsmWriteln('.end method');
  538. AsmLn;
  539. end;
  540. procedure TJasminAssembler.WriteFieldSym(sym: tabstractvarsym);
  541. begin
  542. { internal static field definition alias -> skip }
  543. if sp_static in sym.symoptions then
  544. exit;
  545. AsmWrite('.field ');
  546. AsmWriteln(FieldDefinition(sym));
  547. end;
  548. procedure TJasminAssembler.WriteSymtableVarSyms(st: TSymtable);
  549. var
  550. sym : tsym;
  551. i : longint;
  552. begin
  553. if not assigned(st) then
  554. exit;
  555. for i:=0 to st.SymList.Count-1 do
  556. begin
  557. sym:=tsym(st.SymList[i]);
  558. case sym.typ of
  559. staticvarsym,
  560. fieldvarsym:
  561. begin
  562. WriteFieldSym(tabstractvarsym(sym));
  563. end;
  564. end;
  565. end;
  566. end;
  567. procedure TJasminAssembler.WriteSymtableProcdefs(st: TSymtable);
  568. var
  569. i : longint;
  570. def : tdef;
  571. begin
  572. if not assigned(st) then
  573. exit;
  574. for i:=0 to st.DefList.Count-1 do
  575. begin
  576. def:=tdef(st.DefList[i]);
  577. case def.typ of
  578. procdef :
  579. begin
  580. { methods are also in the static/globalsymtable of the unit
  581. -> make sure they are only written for the objectdefs that
  582. own them }
  583. if not(st.symtabletype in [staticsymtable,globalsymtable]) or
  584. (def.owner=st) then
  585. begin
  586. WriteProcDef(tprocdef(def));
  587. if assigned(tprocdef(def).localst) then
  588. WriteSymtableProcdefs(tprocdef(def).localst);
  589. end;
  590. end;
  591. end;
  592. end;
  593. end;
  594. procedure TJasminAssembler.WriteSymtableObjectDefs(st: TSymtable);
  595. var
  596. i : longint;
  597. def : tdef;
  598. obj : tobjectdef;
  599. nestedclasses: tfpobjectlist;
  600. begin
  601. if not assigned(st) then
  602. exit;
  603. nestedclasses:=tfpobjectlist.create(false);
  604. for i:=0 to st.DefList.Count-1 do
  605. begin
  606. def:=tdef(st.DefList[i]);
  607. case def.typ of
  608. objectdef:
  609. if not(oo_is_external in tobjectdef(def).objectoptions) then
  610. nestedclasses.add(def);
  611. end;
  612. end;
  613. for i:=0 to nestedclasses.count-1 do
  614. begin
  615. obj:=tobjectdef(nestedclasses[i]);
  616. NewAsmFileForObjectDef(obj);
  617. WriteExtraHeader(obj);
  618. WriteSymtableVarSyms(obj.symtable);
  619. AsmLn;
  620. WriteSymtableProcDefs(obj.symtable);
  621. WriteSymtableObjectDefs(obj.symtable);
  622. end;
  623. nestedclasses.free;
  624. end;
  625. constructor TJasminAssembler.Create(smart: boolean);
  626. begin
  627. inherited create(smart);
  628. InstrWriter:=TJasminInstrWriter.Create(self);
  629. asmfiles:=TCmdStrList.Create;
  630. end;
  631. procedure TJasminAssembler.WriteAsmList;
  632. var
  633. hal : tasmlisttype;
  634. i: longint;
  635. begin
  636. {$ifdef EXTDEBUG}
  637. if assigned(current_module.mainsource) then
  638. Comment(V_Debug,'Start writing Jasmin-styled assembler output for '+current_module.mainsource^);
  639. {$endif}
  640. AsmStartSize:=AsmSize;
  641. WriteExtraHeader(nil);
  642. (*
  643. for hal:=low(TasmlistType) to high(TasmlistType) do
  644. begin
  645. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  646. writetree(current_asmdata.asmlists[hal]);
  647. AsmWriteLn(target_asm.comment+'End asmlist '+AsmlistTypeStr[hal]);
  648. end;
  649. *)
  650. { print all global variables }
  651. WriteSymtableVarSyms(current_module.globalsymtable);
  652. WriteSymtableVarSyms(current_module.localsymtable);
  653. AsmLn;
  654. { print all global procedures/functions }
  655. WriteSymtableProcdefs(current_module.globalsymtable);
  656. WriteSymtableProcdefs(current_module.localsymtable);
  657. WriteSymtableObjectDefs(current_module.globalsymtable);
  658. WriteSymtableObjectDefs(current_module.localsymtable);
  659. AsmLn;
  660. {$ifdef EXTDEBUG}
  661. if assigned(current_module.mainsource) then
  662. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource^);
  663. {$endif EXTDEBUG}
  664. end;
  665. {****************************************************************************}
  666. { Jasmin Instruction Writer }
  667. {****************************************************************************}
  668. constructor TJasminInstrWriter.create(_owner: TJasminAssembler);
  669. begin
  670. inherited create;
  671. owner := _owner;
  672. end;
  673. function getreferencestring(var ref : treference) : string;
  674. begin
  675. if (ref.arrayreftype<>art_none) or
  676. (ref.index<>NR_NO) then
  677. internalerror(2010122809);
  678. if assigned(ref.symbol) then
  679. begin
  680. // global symbol or field -> full type and name
  681. // ref.base can be <> NR_NO in case an instance field is loaded.
  682. // This register is not part of this instruction, it will have
  683. // been placed on the stack by the previous one.
  684. if (ref.offset<>0) then
  685. internalerror(2010122811);
  686. result:=ref.symbol.name;
  687. end
  688. else
  689. begin
  690. // local symbol -> stack slot, stored in offset
  691. if ref.base<>NR_STACK_POINTER_REG then
  692. internalerror(2010122810);
  693. result:=tostr(ref.offset);
  694. end;
  695. end;
  696. function getopstr(const o:toper) : ansistring;
  697. var
  698. i,runstart,runlen: longint;
  699. num: string[4];
  700. begin
  701. case o.typ of
  702. top_reg:
  703. // should have been translated into a memory location by the
  704. // register allocator)
  705. if (cs_no_regalloc in current_settings.globalswitches) then
  706. getopstr:=std_regname(o.reg)
  707. else
  708. internalerror(2010122803);
  709. top_const:
  710. str(o.val,result);
  711. top_ref:
  712. getopstr:=getreferencestring(o.ref^);
  713. top_single:
  714. str(o.sval:0:20,result);
  715. top_double:
  716. begin
  717. str(o.dval:0:20,result);
  718. // force interpretation as double
  719. result:=result+'d';
  720. end;
  721. top_string:
  722. begin
  723. { escape control codes }
  724. runlen:=0;
  725. runstart:=0;
  726. for i:=1 to o.pcvallen do
  727. begin
  728. if o.pcval[i]<#32 then
  729. begin
  730. if runlen>0 then
  731. begin
  732. setlength(result,length(result)+runlen);
  733. move(result[length(result)-runlen],o.pcval[runstart],runlen);
  734. runlen:=0;
  735. end;
  736. result:=result+'\u'+hexstr(ord(o.pcval[i]),4);
  737. end
  738. else if o.pcval[i]<#127 then
  739. begin
  740. if runlen=0 then
  741. runstart:=i;
  742. inc(runlen);
  743. end
  744. else
  745. // since Jasmin expects an UTF-16 string, we can't safely
  746. // have high ASCII characters since they'll be
  747. // re-interpreted as utf-16 anyway
  748. internalerror(2010122808);
  749. end;
  750. if runlen>0 then
  751. begin
  752. setlength(result,length(result)+runlen);
  753. move(result[length(result)-runlen],o.pcval[runstart],runlen);
  754. end;
  755. end;
  756. top_wstring:
  757. begin
  758. { escape control codes }
  759. for i:=1 to getlengthwidestring(o.pwstrval) do
  760. begin
  761. if (o.pwstrval^.data[i]<32) or
  762. (o.pwstrval^.data[i]>127) then
  763. result:=result+'\u'+hexstr(o.pwstrval^.data[i],4)
  764. else
  765. result:=result+char(o.pwstrval^.data[i]);
  766. end;
  767. end
  768. else
  769. internalerror(2010122802);
  770. end;
  771. end;
  772. procedure TJasminInstrWriter.WriteInstruction(hp: tai);
  773. var
  774. s: ansistring;
  775. i: byte;
  776. sep: string[3];
  777. begin
  778. s:=#9+jas_op2str[taicpu(hp).opcode];
  779. if taicpu(hp).ops<>0 then
  780. begin
  781. sep:=#9;
  782. for i:=0 to taicpu(hp).ops-1 do
  783. begin
  784. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  785. sep:=' ';
  786. end;
  787. end;
  788. owner.AsmWriteLn(s);
  789. end;
  790. {****************************************************************************}
  791. { Jasmin Instruction Writer }
  792. {****************************************************************************}
  793. const
  794. as_jvm_jasmin_info : tasminfo =
  795. (
  796. id : as_jvm_jasmin;
  797. idtxt : 'Jasmin';
  798. asmbin : 'java';
  799. asmcmd : '-jar $JASMINJAR $ASM -d $OBJDIR';
  800. supported_targets : [system_jvm_java32];
  801. flags : [];
  802. labelprefix : 'L';
  803. comment : ' ; ';
  804. );
  805. begin
  806. RegisterAssembler(as_jvm_jasmin_info,TJasminAssembler);
  807. end.