agjasmin.pas 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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: tabstractrecorddef);
  39. procedure WriteInstruction(hp: tai);
  40. procedure NewAsmFileForStructDef(obj: tabstractrecorddef);
  41. function VisibilityToStr(vis: tvisibility): ansistring;
  42. function MethodDefinition(pd: tprocdef): ansistring;
  43. function ConstValue(csym: tconstsym): ansistring;
  44. function ConstAssignmentValue(csym: tconstsym): ansistring;
  45. function ConstDefinition(sym: tconstsym): ansistring;
  46. function FieldDefinition(sym: tabstractvarsym): ansistring;
  47. function InnerStructDef(obj: tabstractrecorddef): ansistring;
  48. procedure WriteProcDef(pd: tprocdef);
  49. procedure WriteFieldSym(sym: tabstractvarsym);
  50. procedure WriteConstSym(sym: tconstsym);
  51. procedure WriteSymtableVarSyms(st: TSymtable);
  52. procedure WriteSymtableProcdefs(st: TSymtable);
  53. procedure WriteSymtableStructDefs(st: TSymtable);
  54. public
  55. constructor Create(smart: boolean); override;
  56. function MakeCmdLine: TCmdStr;override;
  57. procedure WriteTree(p:TAsmList);override;
  58. procedure WriteAsmList;override;
  59. destructor destroy; override;
  60. protected
  61. InstrWriter: TJasminInstrWriter;
  62. end;
  63. {# This is the base class for writing instructions.
  64. The WriteInstruction() method must be overridden
  65. to write a single instruction to the assembler
  66. file.
  67. }
  68. { TJasminInstrWriter }
  69. TJasminInstrWriter = class
  70. constructor create(_owner: TJasminAssembler);
  71. procedure WriteInstruction(hp : tai); virtual;
  72. protected
  73. owner: TJasminAssembler;
  74. end;
  75. implementation
  76. uses
  77. SysUtils,
  78. cutils,cfileutl,systems,script,
  79. fmodule,finput,verbose,
  80. symtype,symtable,jvmdef,
  81. itcpujas,cpubase,cpuinfo,cgutils,
  82. widestr
  83. ;
  84. const
  85. line_length = 70;
  86. type
  87. t64bitarray = array[0..7] of byte;
  88. t32bitarray = array[0..3] of byte;
  89. {****************************************************************************}
  90. { Support routines }
  91. {****************************************************************************}
  92. function fixline(s:string):string;
  93. {
  94. return s with all leading and ending spaces and tabs removed
  95. }
  96. var
  97. i,j,k : integer;
  98. begin
  99. i:=length(s);
  100. while (i>0) and (s[i] in [#9,' ']) do
  101. dec(i);
  102. j:=1;
  103. while (j<i) and (s[j] in [#9,' ']) do
  104. inc(j);
  105. for k:=j to i do
  106. if s[k] in [#0..#31,#127..#255] then
  107. s[k]:='.';
  108. fixline:=Copy(s,j,i-j+1);
  109. end;
  110. function constastr(p: pchar; len: longint): ansistring;
  111. var
  112. i,runstart,runlen: longint;
  113. procedure flush;
  114. begin
  115. if runlen>0 then
  116. begin
  117. setlength(result,length(result)+runlen);
  118. move(p[runstart],result[length(result)-runlen+1],runlen);
  119. runlen:=0;
  120. end;
  121. end;
  122. begin
  123. result:='"';
  124. runlen:=0;
  125. runstart:=0;
  126. for i:=0 to len-1 do
  127. begin
  128. { escape control codes }
  129. case p[i] of
  130. { LF and CR must be escaped specially, because \uXXXX parsing
  131. happens in the pre-processor, so it's the same as actually
  132. inserting a newline in the middle of a string constant }
  133. #10:
  134. begin
  135. flush;
  136. result:=result+'\n';
  137. end;
  138. #13:
  139. begin
  140. flush;
  141. result:=result+'\r';
  142. end;
  143. '"','\':
  144. begin
  145. flush;
  146. result:=result+'\'+p[i];
  147. end
  148. else if p[i]<#32 then
  149. begin
  150. flush;
  151. result:=result+'\u'+hexstr(ord(p[i]),4);
  152. end
  153. else if p[i]<#127 then
  154. begin
  155. if runlen=0 then
  156. runstart:=i;
  157. inc(runlen);
  158. end
  159. else
  160. begin
  161. { see comments in njvmcon }
  162. flush;
  163. result:=result+'\u'+hexstr(ord(p[i]),4)
  164. end;
  165. end;
  166. end;
  167. flush;
  168. result:=result+'"';
  169. end;
  170. function constwstr(w: pcompilerwidechar; len: longint): ansistring;
  171. var
  172. i: longint;
  173. begin
  174. result:='"';
  175. for i:=0 to len-1 do
  176. begin
  177. { escape control codes }
  178. case w[i] of
  179. 10:
  180. result:=result+'\n';
  181. 13:
  182. result:=result+'\r';
  183. ord('"'),ord('\'):
  184. result:=result+'\'+chr(w[i]);
  185. else if (w[i]<32) or
  186. (w[i]>=127) then
  187. result:=result+'\u'+hexstr(w[i],4)
  188. else
  189. result:=result+char(w[i]);
  190. end;
  191. end;
  192. result:=result+'"';
  193. end;
  194. function constsingle(s: single): ansistring;
  195. begin
  196. result:='0fx'+hexstr(longint(t32bitarray(s)),8);
  197. end;
  198. function constdouble(d: double): ansistring;
  199. begin
  200. // force interpretation as double (since we write it out as an
  201. // integer, we never have to swap the endianess). We have to
  202. // include the sign separately because of the way Java parses
  203. // hex numbers (0x8000000000000000 is not a valid long)
  204. result:=hexstr(abs(int64(t64bitarray(d))),16);
  205. if int64(t64bitarray(d))<0 then
  206. result:='-'+result;
  207. result:='0dx'+result;
  208. end;
  209. {****************************************************************************}
  210. { Jasmin Assembler writer }
  211. {****************************************************************************}
  212. destructor TJasminAssembler.Destroy;
  213. begin
  214. InstrWriter.free;
  215. asmfiles.free;
  216. inherited destroy;
  217. end;
  218. procedure TJasminAssembler.WriteTree(p:TAsmList);
  219. var
  220. ch : char;
  221. hp : tai;
  222. hp1 : tailineinfo;
  223. s : ansistring;
  224. i,pos : longint;
  225. InlineLevel : longint;
  226. do_line : boolean;
  227. begin
  228. if not assigned(p) then
  229. exit;
  230. InlineLevel:=0;
  231. { lineinfo is only needed for al_procedures (PFV) }
  232. do_line:=(cs_asm_source in current_settings.globalswitches);
  233. hp:=tai(p.first);
  234. while assigned(hp) do
  235. begin
  236. prefetch(pointer(hp.next)^);
  237. if not(hp.typ in SkipLineInfo) then
  238. begin
  239. hp1 := hp as tailineinfo;
  240. current_filepos:=hp1.fileinfo;
  241. { no line info for inlined code }
  242. if do_line and (inlinelevel=0) then
  243. begin
  244. { load infile }
  245. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  246. begin
  247. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  248. if assigned(infile) then
  249. begin
  250. { open only if needed !! }
  251. if (cs_asm_source in current_settings.globalswitches) then
  252. infile.open;
  253. end;
  254. { avoid unnecessary reopens of the same file !! }
  255. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  256. { be sure to change line !! }
  257. lastfileinfo.line:=-1;
  258. end;
  259. { write source }
  260. if (cs_asm_source in current_settings.globalswitches) and
  261. assigned(infile) then
  262. begin
  263. if (infile<>lastinfile) then
  264. begin
  265. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  266. if assigned(lastinfile) then
  267. lastinfile.close;
  268. end;
  269. if (hp1.fileinfo.line<>lastfileinfo.line) and
  270. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  271. begin
  272. if (hp1.fileinfo.line<>0) and
  273. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  274. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  275. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  276. { set it to a negative value !
  277. to make that is has been read already !! PM }
  278. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  279. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  280. end;
  281. end;
  282. lastfileinfo:=hp1.fileinfo;
  283. lastinfile:=infile;
  284. end;
  285. end;
  286. case hp.typ of
  287. ait_comment :
  288. Begin
  289. AsmWrite(target_asm.comment);
  290. AsmWritePChar(tai_comment(hp).str);
  291. AsmLn;
  292. End;
  293. ait_regalloc :
  294. begin
  295. if (cs_asm_regalloc in current_settings.globalswitches) then
  296. begin
  297. AsmWrite(#9+target_asm.comment+'Register ');
  298. repeat
  299. AsmWrite(std_regname(Tai_regalloc(hp).reg));
  300. if (hp.next=nil) or
  301. (tai(hp.next).typ<>ait_regalloc) or
  302. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  303. break;
  304. hp:=tai(hp.next);
  305. AsmWrite(',');
  306. until false;
  307. AsmWrite(' ');
  308. AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  309. end;
  310. end;
  311. ait_tempalloc :
  312. begin
  313. if (cs_asm_tempalloc in current_settings.globalswitches) then
  314. begin
  315. {$ifdef EXTDEBUG}
  316. if assigned(tai_tempalloc(hp).problem) then
  317. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  318. tostr(tai_tempalloc(hp).tempsize)+' '+tai_tempalloc(hp).problem^)
  319. else
  320. {$endif EXTDEBUG}
  321. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  322. tostr(tai_tempalloc(hp).tempsize)+' '+tempallocstr[tai_tempalloc(hp).allocation]);
  323. end;
  324. end;
  325. ait_align :
  326. begin
  327. end;
  328. ait_section :
  329. begin
  330. end;
  331. ait_datablock :
  332. begin
  333. internalerror(2010122701);
  334. end;
  335. ait_const:
  336. begin
  337. AsmWriteln('constant');
  338. // internalerror(2010122702);
  339. end;
  340. ait_real_64bit :
  341. begin
  342. internalerror(2010122703);
  343. end;
  344. ait_real_32bit :
  345. begin
  346. internalerror(2010122703);
  347. end;
  348. ait_comp_64bit :
  349. begin
  350. internalerror(2010122704);
  351. end;
  352. ait_string :
  353. begin
  354. pos:=0;
  355. for i:=1 to tai_string(hp).len do
  356. begin
  357. if pos=0 then
  358. begin
  359. AsmWrite(#9'strconst: '#9'"');
  360. pos:=20;
  361. end;
  362. ch:=tai_string(hp).str[i-1];
  363. case ch of
  364. #0, {This can't be done by range, because a bug in FPC}
  365. #1..#31,
  366. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  367. '"' : s:='\"';
  368. '\' : s:='\\';
  369. else
  370. s:=ch;
  371. end;
  372. AsmWrite(s);
  373. inc(pos,length(s));
  374. if (pos>line_length) or (i=tai_string(hp).len) then
  375. begin
  376. AsmWriteLn('"');
  377. pos:=0;
  378. end;
  379. end;
  380. end;
  381. ait_label :
  382. begin
  383. if (tai_label(hp).labsym.is_used) then
  384. begin
  385. AsmWrite(tai_label(hp).labsym.name);
  386. AsmWriteLn(':');
  387. end;
  388. end;
  389. ait_symbol :
  390. begin
  391. if (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  392. begin
  393. end
  394. else
  395. begin
  396. AsmWrite('data symbol: ');
  397. AsmWriteln(tai_symbol(hp).sym.name);
  398. // internalerror(2010122706);
  399. end;
  400. end;
  401. ait_symbol_end :
  402. begin
  403. end;
  404. ait_instruction :
  405. begin
  406. WriteInstruction(hp);
  407. end;
  408. ait_force_line,
  409. ait_function_name : ;
  410. ait_cutobject :
  411. begin
  412. end;
  413. ait_marker :
  414. if tai_marker(hp).kind=mark_NoLineInfoStart then
  415. inc(InlineLevel)
  416. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  417. dec(InlineLevel);
  418. ait_directive :
  419. begin
  420. AsmWrite('.'+directivestr[tai_directive(hp).directive]+' ');
  421. if assigned(tai_directive(hp).name) then
  422. AsmWrite(tai_directive(hp).name^);
  423. AsmLn;
  424. end;
  425. ait_jvar:
  426. begin
  427. AsmWrite('.var ');
  428. AsmWrite(tostr(tai_jvar(hp).stackslot));
  429. AsmWrite(' is ');
  430. AsmWrite(tai_jvar(hp).desc^);
  431. AsmWrite(' from ');
  432. AsmWrite(tai_jvar(hp).startlab.name);
  433. AsmWrite(' to ');
  434. AsmWriteLn(tai_jvar(hp).stoplab.name);
  435. end;
  436. ait_jcatch:
  437. begin
  438. AsmWrite('.catch ');
  439. AsmWrite(tai_jcatch(hp).name^);
  440. AsmWrite(' from ');
  441. AsmWrite(tai_jcatch(hp).startlab.name);
  442. AsmWrite(' to ');
  443. AsmWrite(tai_jcatch(hp).stoplab.name);
  444. AsmWrite(' using ');
  445. AsmWriteLn(tai_jcatch(hp).handlerlab.name);
  446. end;
  447. else
  448. internalerror(2010122707);
  449. end;
  450. hp:=tai(hp.next);
  451. end;
  452. end;
  453. procedure TJasminAssembler.WriteExtraHeader(obj: tabstractrecorddef);
  454. var
  455. superclass,
  456. intf: tobjectdef;
  457. n: ansistring;
  458. i: longint;
  459. toplevelowner: tsymtable;
  460. begin
  461. { JVM 1.5+ }
  462. AsmWriteLn('.bytecode 49.0');
  463. // include files are not support by Java, and the directory of the main
  464. // source file must not be specified
  465. if assigned(current_module.mainsource) then
  466. n:=ExtractFileName(current_module.mainsource^)
  467. else
  468. n:=InputFileName;
  469. AsmWriteLn('.source '+ExtractFileName(n));
  470. { class/interface name }
  471. if not assigned(obj) then
  472. begin
  473. { fake class type for unit -> name=unitname and
  474. superclass=java.lang.object, make final so you cannot descend
  475. from it }
  476. AsmWrite('.class final public ');
  477. if assigned(current_module.namespace) then
  478. AsmWrite(current_module.namespace^+'.');
  479. AsmWriteln(current_module.realmodulename^);
  480. AsmWriteLn('.super java/lang/Object');
  481. end
  482. else
  483. begin
  484. toplevelowner:=obj.owner;
  485. while not(toplevelowner.symtabletype in [staticsymtable,globalsymtable]) do
  486. toplevelowner:=toplevelowner.defowner.owner;
  487. case obj.typ of
  488. recorddef:
  489. begin
  490. { can't inherit from records }
  491. AsmWrite('.class final ');
  492. if toplevelowner.symtabletype=globalsymtable then
  493. AsmWrite('public ');
  494. AsmWriteln(obj.jvm_full_typename(true));
  495. superclass:=java_fpcbaserecordtype;
  496. end;
  497. objectdef:
  498. begin
  499. case tobjectdef(obj).objecttype of
  500. odt_javaclass:
  501. begin
  502. AsmWrite('.class ');
  503. if oo_is_sealed in tobjectdef(obj).objectoptions then
  504. AsmWrite('final ');
  505. if toplevelowner.symtabletype=globalsymtable then
  506. AsmWrite('public ');
  507. if (oo_is_enum_class in tobjectdef(obj).objectoptions) then
  508. AsmWrite('enum ');
  509. AsmWriteln(obj.jvm_full_typename(true));
  510. superclass:=tobjectdef(obj).childof;
  511. end;
  512. odt_interfacejava:
  513. begin
  514. AsmWrite('.interface abstract ');
  515. if toplevelowner.symtabletype=globalsymtable then
  516. AsmWrite('public ');
  517. AsmWriteLn(obj.jvm_full_typename(true));
  518. { interfaces must always specify Java.lang.object as
  519. superclass }
  520. superclass:=java_jlobject;
  521. end
  522. else
  523. internalerror(2011010906);
  524. end;
  525. end;
  526. end;
  527. { superclass }
  528. if assigned(superclass) then
  529. begin
  530. AsmWrite('.super ');
  531. if assigned(superclass.import_lib) then
  532. AsmWrite(superclass.import_lib^+'/');
  533. AsmWriteln(superclass.objextname^);
  534. end;
  535. { signature for enum classes (must come after superclass) }
  536. if (obj.typ=objectdef) and
  537. (oo_is_enum_class in tobjectdef(obj).objectoptions) then
  538. AsmWriteln('.signature "Ljava/lang/Enum<'+obj.jvm_full_typename(true)+';>;"');
  539. { implemented interfaces }
  540. if (obj.typ=objectdef) and
  541. assigned(tobjectdef(obj).ImplementedInterfaces) then
  542. begin
  543. for i:=0 to tobjectdef(obj).ImplementedInterfaces.count-1 do
  544. begin
  545. intf:=TImplementedInterface(tobjectdef(obj).ImplementedInterfaces[i]).IntfDef;
  546. AsmWrite('.implements ');
  547. if assigned(intf.import_lib) then
  548. AsmWrite(intf.import_lib^+'/');
  549. AsmWriteln(intf.objextname^);
  550. end;
  551. end;
  552. { in case of nested class: relation to parent class }
  553. if obj.owner.symtabletype in [objectsymtable,recordsymtable] then
  554. AsmWriteln(InnerStructDef(obj));
  555. { all all nested classes }
  556. for i:=0 to obj.symtable.deflist.count-1 do
  557. if is_java_class_or_interface(tdef(obj.symtable.deflist[i])) or
  558. (tdef(obj.symtable.deflist[i]).typ=recorddef) then
  559. AsmWriteln(InnerStructDef(tabstractrecorddef(obj.symtable.deflist[i])));
  560. end;
  561. AsmLn;
  562. end;
  563. procedure TJasminAssembler.WriteInstruction(hp: tai);
  564. begin
  565. InstrWriter.WriteInstruction(hp);
  566. end;
  567. function TJasminAssembler.MakeCmdLine: TCmdStr;
  568. const
  569. jasminjarname = 'jasmin.jar';
  570. var
  571. filenames: tcmdstr;
  572. jasminjarfound: boolean;
  573. begin
  574. if jasminjar='' then
  575. begin
  576. jasminjarfound:=false;
  577. if utilsdirectory<>'' then
  578. jasminjarfound:=FindFile(jasminjarname,utilsdirectory,false,jasminjar);
  579. if not jasminjarfound then
  580. jasminjarfound:=FindFileInExeLocations(jasminjarname,false,jasminjar);
  581. if (not jasminjarfound) and not(cs_asm_extern in current_settings.globalswitches) then
  582. begin
  583. Message1(exec_e_assembler_not_found,jasminjarname);
  584. current_settings.globalswitches:=current_settings.globalswitches+[cs_asm_extern];
  585. end;
  586. if jasminjarfound then
  587. Message1(exec_t_using_assembler,jasminjar);
  588. end;
  589. result:=target_asm.asmcmd;
  590. filenames:=maybequoted(ScriptFixFileName(AsmFileName));
  591. while not asmfiles.empty do
  592. filenames:=filenames+' '+asmfiles.GetFirst;
  593. Replace(result,'$ASM',filenames);
  594. if (path<>'') then
  595. Replace(result,'$OBJDIR',maybequoted(ScriptFixFileName(path)))
  596. else
  597. Replace(result,'$OBJDIR','.');
  598. Replace(result,'$JASMINJAR',maybequoted(ScriptFixFileName(jasminjar)));
  599. end;
  600. procedure TJasminAssembler.NewAsmFileForStructDef(obj: tabstractrecorddef);
  601. begin
  602. if AsmSize<>AsmStartSize then
  603. begin
  604. AsmClose;
  605. asmfiles.Concat(maybequoted(ScriptFixFileName(AsmFileName)));
  606. end
  607. else
  608. AsmClear;
  609. AsmFileName:=obj.jvm_full_typename(false);
  610. AsmFileName:=Path+FixFileName(AsmFileName)+target_info.asmext;
  611. AsmCreate(cut_normal);
  612. end;
  613. function TJasminAssembler.VisibilityToStr(vis: tvisibility): ansistring;
  614. begin
  615. case vis of
  616. vis_hidden,
  617. vis_strictprivate:
  618. result:='private ';
  619. vis_strictprotected:
  620. result:='protected ';
  621. vis_protected,
  622. vis_private:
  623. { pick default visibility = "package" visibility; required because
  624. other classes in the same unit can also access these symbols }
  625. result:='';
  626. vis_public:
  627. result:='public '
  628. else
  629. internalerror(2010122609);
  630. end;
  631. end;
  632. function TJasminAssembler.MethodDefinition(pd: tprocdef): ansistring;
  633. begin
  634. result:=VisibilityToStr(pd.visibility);
  635. if (pd.procsym.owner.symtabletype in [globalsymtable,staticsymtable,localsymtable]) or
  636. (po_classmethod in pd.procoptions) then
  637. result:=result+'static ';
  638. if is_javainterface(tdef(pd.owner.defowner)) then
  639. result:=result+'abstract ';
  640. if (pd.procsym.owner.symtabletype in [globalsymtable,staticsymtable,localsymtable]) or
  641. (po_finalmethod in pd.procoptions) or
  642. (not(po_virtualmethod in pd.procoptions) and
  643. not(pd.proctypeoption in [potype_constructor,potype_class_constructor])) then
  644. result:=result+'final ';
  645. result:=result+pd.jvmmangledbasename(false);
  646. end;
  647. function TJasminAssembler.ConstValue(csym: tconstsym): ansistring;
  648. begin
  649. case csym.consttyp of
  650. constord:
  651. { always interpret as signed value, because the JVM does not
  652. support unsigned 64 bit values }
  653. result:=tostr(csym.value.valueord.svalue);
  654. conststring:
  655. result:=constastr(pchar(csym.value.valueptr),csym.value.len);
  656. constreal:
  657. case tfloatdef(csym.constdef).floattype of
  658. s32real:
  659. result:=constsingle(pbestreal(csym.value.valueptr)^);
  660. s64real:
  661. result:=constdouble(pbestreal(csym.value.valueptr)^);
  662. else
  663. internalerror(2011021204);
  664. end;
  665. constset:
  666. result:='TODO: add support for constant sets';
  667. constpointer:
  668. { can only be null, but that's the default value and should not
  669. be written; there's no primitive type that can hold nill }
  670. internalerror(2011021201);
  671. constnil:
  672. internalerror(2011021202);
  673. constresourcestring:
  674. result:='TODO: add support for constant resource strings';
  675. constwstring:
  676. result:=constwstr(pcompilerwidestring(csym.value.valueptr)^.data,pcompilerwidestring(csym.value.valueptr)^.len);
  677. constguid:
  678. result:='TODO: add support for constant guids';
  679. else
  680. internalerror(2011021205);
  681. end;
  682. end;
  683. function TJasminAssembler.ConstAssignmentValue(csym: tconstsym): ansistring;
  684. begin
  685. { nil is the default value -> don't write explicitly }
  686. case csym.consttyp of
  687. constpointer:
  688. begin
  689. if csym.value.valueordptr<>0 then
  690. internalerror(2011021206);
  691. result:='';
  692. end;
  693. constnil:
  694. result:='';
  695. else
  696. result:=' = '+ConstValue(csym)
  697. end;
  698. end;
  699. function TJasminAssembler.ConstDefinition(sym: tconstsym): ansistring;
  700. begin
  701. result:=VisibilityToStr(sym.visibility);
  702. { formal constants are always class-level, not instance-level }
  703. result:=result+'static final ';
  704. result:=result+jvmmangledbasename(sym,true);
  705. result:=result+ConstAssignmentValue(tconstsym(sym));
  706. end;
  707. function TJasminAssembler.FieldDefinition(sym: tabstractvarsym): ansistring;
  708. var
  709. vissym: tabstractvarsym;
  710. begin
  711. vissym:=sym;
  712. { static field definition -> get original field definition for
  713. visibility }
  714. if (sym.typ=staticvarsym) and
  715. (sym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  716. begin
  717. vissym:=tabstractvarsym(
  718. tabstractrecorddef(sym.owner.defowner).symtable.find(
  719. internal_static_field_name(sym.name)));
  720. if not assigned(vissym) then
  721. vissym:=tabstractvarsym(
  722. tabstractrecorddef(sym.owner.defowner).symtable.find(
  723. generate_nested_name(sym.owner,'_')+'_'+sym.name));
  724. if not assigned(vissym) or
  725. not(vissym.typ in [fieldvarsym,absolutevarsym]) then
  726. internalerror(2011011501);
  727. end;
  728. case vissym.typ of
  729. staticvarsym:
  730. begin
  731. if vissym.owner.symtabletype=globalsymtable then
  732. result:='public '
  733. else
  734. { package visbility }
  735. result:='';
  736. end;
  737. fieldvarsym,
  738. absolutevarsym:
  739. result:=VisibilityToStr(tstoredsym(vissym).visibility);
  740. else
  741. internalerror(2011011204);
  742. end;
  743. if (sym.typ=staticvarsym) or
  744. (sp_static in sym.symoptions) then
  745. result:=result+'static ';
  746. if sym.varspez in [vs_const,vs_final] then
  747. result:=result+'final ';
  748. if sp_internal in sym.symoptions then
  749. result:=result+'synthetic ';
  750. { mark the class fields of enum classes that contain the initialised
  751. enum instances as "enum" (recognise them by the fact that their type
  752. is the same as their parent class, and that this parent class is
  753. marked as oo_is_enum_class) }
  754. if assigned(sym.owner.defowner) and
  755. (tdef(sym.owner.defowner).typ=objectdef) and
  756. (oo_is_enum_class in tobjectdef(sym.owner.defowner).objectoptions) and
  757. (sym.typ=staticvarsym) and
  758. (tstaticvarsym(sym).vardef=tdef(sym.owner.defowner)) then
  759. result:=result+'enum ';
  760. result:=result+jvmmangledbasename(sym,true);
  761. end;
  762. function TJasminAssembler.InnerStructDef(obj: tabstractrecorddef): ansistring;
  763. var
  764. extname: pshortstring;
  765. kindname: ansistring;
  766. begin
  767. if not(obj.owner.defowner.typ in [objectdef,recorddef]) then
  768. internalerror(2011021701);
  769. case obj.typ of
  770. recorddef:
  771. begin
  772. kindname:='class ';
  773. extname:=obj.symtable.realname;
  774. end;
  775. objectdef:
  776. begin
  777. extname:=tobjectdef(obj).objextname;
  778. case tobjectdef(obj).objecttype of
  779. odt_javaclass:
  780. kindname:='class ';
  781. odt_interfacejava:
  782. kindname:='interface ';
  783. else
  784. internalerror(2011021702);
  785. end;
  786. end;
  787. else
  788. internalerror(2011032809);
  789. end;
  790. result:=
  791. '.inner '+
  792. kindname+
  793. VisibilityToStr(obj.typesym.visibility)+
  794. { Nested classes in the Pascal sense are equivalent to "static"
  795. inner classes in Java -- will be changed when support for
  796. Java-style non-static classes is added }
  797. ' static '+
  798. extname^+
  799. ' inner '+
  800. obj.jvm_full_typename(true)+
  801. ' outer '+
  802. tabstractrecorddef(obj.owner.defowner).jvm_full_typename(true);
  803. end;
  804. procedure TJasminAssembler.WriteProcDef(pd: tprocdef);
  805. begin
  806. if not assigned(pd.exprasmlist) and
  807. (not is_javainterface(pd.struct) or
  808. (pd.proctypeoption in [potype_unitinit,potype_unitfinalize])) then
  809. exit;
  810. AsmWrite('.method ');
  811. AsmWriteln(MethodDefinition(pd));
  812. if jvmtypeneedssignature(pd) then
  813. begin
  814. AsmWrite('.signature "');
  815. AsmWrite(pd.jvmmangledbasename(true));
  816. AsmWriteln('"');
  817. end;
  818. WriteTree(pd.exprasmlist);
  819. AsmWriteln('.end method');
  820. AsmLn;
  821. end;
  822. procedure TJasminAssembler.WriteFieldSym(sym: tabstractvarsym);
  823. begin
  824. { internal static field definition alias -> skip }
  825. if sp_static in sym.symoptions then
  826. exit;
  827. AsmWrite('.field ');
  828. AsmWriteln(FieldDefinition(sym));
  829. end;
  830. procedure TJasminAssembler.WriteConstSym(sym: tconstsym);
  831. begin
  832. AsmWrite('.field ');
  833. AsmWriteln(ConstDefinition(sym));
  834. end;
  835. procedure TJasminAssembler.WriteSymtableVarSyms(st: TSymtable);
  836. var
  837. sym : tsym;
  838. i,j : longint;
  839. begin
  840. if not assigned(st) then
  841. exit;
  842. for i:=0 to st.SymList.Count-1 do
  843. begin
  844. sym:=tsym(st.SymList[i]);
  845. case sym.typ of
  846. staticvarsym,
  847. fieldvarsym:
  848. begin
  849. WriteFieldSym(tabstractvarsym(sym));
  850. if (sym.typ=staticvarsym) and
  851. assigned(tstaticvarsym(sym).defaultconstsym) then
  852. WriteFieldSym(tabstractvarsym(tstaticvarsym(sym).defaultconstsym));
  853. end;
  854. constsym:
  855. begin
  856. { multiple procedures can have constants with the same name }
  857. if not assigned(sym.owner.defowner) or
  858. (tdef(sym.owner.defowner).typ<>procdef) then
  859. WriteConstSym(tconstsym(sym));
  860. end;
  861. procsym:
  862. begin
  863. for j:=0 to tprocsym(sym).procdeflist.count-1 do
  864. WriteSymtableVarSyms(tprocdef(tprocsym(sym).procdeflist[j]).localst);
  865. end;
  866. end;
  867. end;
  868. end;
  869. procedure TJasminAssembler.WriteSymtableProcdefs(st: TSymtable);
  870. var
  871. i : longint;
  872. def : tdef;
  873. begin
  874. if not assigned(st) then
  875. exit;
  876. for i:=0 to st.DefList.Count-1 do
  877. begin
  878. def:=tdef(st.DefList[i]);
  879. case def.typ of
  880. procdef :
  881. begin
  882. { methods are also in the static/globalsymtable of the unit
  883. -> make sure they are only written for the objectdefs that
  884. own them }
  885. if not(st.symtabletype in [staticsymtable,globalsymtable]) or
  886. (def.owner=st) then
  887. begin
  888. WriteProcDef(tprocdef(def));
  889. if assigned(tprocdef(def).localst) then
  890. WriteSymtableProcdefs(tprocdef(def).localst);
  891. end;
  892. end;
  893. end;
  894. end;
  895. end;
  896. procedure TJasminAssembler.WriteSymtableStructDefs(st: TSymtable);
  897. var
  898. i : longint;
  899. def : tdef;
  900. obj : tabstractrecorddef;
  901. nestedstructs: tfpobjectlist;
  902. begin
  903. if not assigned(st) then
  904. exit;
  905. nestedstructs:=tfpobjectlist.create(false);
  906. for i:=0 to st.DefList.Count-1 do
  907. begin
  908. def:=tdef(st.DefList[i]);
  909. case def.typ of
  910. objectdef:
  911. if not(oo_is_external in tobjectdef(def).objectoptions) then
  912. nestedstructs.add(def);
  913. recorddef:
  914. nestedstructs.add(def);
  915. end;
  916. end;
  917. for i:=0 to nestedstructs.count-1 do
  918. begin
  919. obj:=tabstractrecorddef(nestedstructs[i]);
  920. NewAsmFileForStructDef(obj);
  921. WriteExtraHeader(obj);
  922. WriteSymtableVarSyms(obj.symtable);
  923. AsmLn;
  924. WriteSymtableProcDefs(obj.symtable);
  925. WriteSymtableStructDefs(obj.symtable);
  926. end;
  927. nestedstructs.free;
  928. end;
  929. constructor TJasminAssembler.Create(smart: boolean);
  930. begin
  931. inherited create(smart);
  932. InstrWriter:=TJasminInstrWriter.Create(self);
  933. asmfiles:=TCmdStrList.Create;
  934. end;
  935. procedure TJasminAssembler.WriteAsmList;
  936. begin
  937. {$ifdef EXTDEBUG}
  938. if assigned(current_module.mainsource) then
  939. Comment(V_Debug,'Start writing Jasmin-styled assembler output for '+current_module.mainsource^);
  940. {$endif}
  941. AsmStartSize:=AsmSize;
  942. WriteExtraHeader(nil);
  943. (*
  944. for hal:=low(TasmlistType) to high(TasmlistType) do
  945. begin
  946. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  947. writetree(current_asmdata.asmlists[hal]);
  948. AsmWriteLn(target_asm.comment+'End asmlist '+AsmlistTypeStr[hal]);
  949. end;
  950. *)
  951. { print all global variables }
  952. WriteSymtableVarSyms(current_module.globalsymtable);
  953. WriteSymtableVarSyms(current_module.localsymtable);
  954. AsmLn;
  955. { print all global procedures/functions }
  956. WriteSymtableProcdefs(current_module.globalsymtable);
  957. WriteSymtableProcdefs(current_module.localsymtable);
  958. WriteSymtableStructDefs(current_module.globalsymtable);
  959. WriteSymtableStructDefs(current_module.localsymtable);
  960. AsmLn;
  961. {$ifdef EXTDEBUG}
  962. if assigned(current_module.mainsource) then
  963. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource^);
  964. {$endif EXTDEBUG}
  965. end;
  966. {****************************************************************************}
  967. { Jasmin Instruction Writer }
  968. {****************************************************************************}
  969. constructor TJasminInstrWriter.create(_owner: TJasminAssembler);
  970. begin
  971. inherited create;
  972. owner := _owner;
  973. end;
  974. function getreferencestring(var ref : treference) : ansistring;
  975. begin
  976. if (ref.arrayreftype<>art_none) or
  977. (ref.index<>NR_NO) then
  978. internalerror(2010122809);
  979. if assigned(ref.symbol) then
  980. begin
  981. // global symbol or field -> full type and name
  982. // ref.base can be <> NR_NO in case an instance field is loaded.
  983. // This register is not part of this instruction, it will have
  984. // been placed on the stack by the previous one.
  985. if (ref.offset<>0) then
  986. internalerror(2010122811);
  987. result:=ref.symbol.name;
  988. end
  989. else
  990. begin
  991. // local symbol -> stack slot, stored in offset
  992. if ref.base<>NR_STACK_POINTER_REG then
  993. internalerror(2010122810);
  994. result:=tostr(ref.offset);
  995. end;
  996. end;
  997. function getopstr(const o:toper) : ansistring;
  998. var
  999. d: double;
  1000. s: single;
  1001. begin
  1002. case o.typ of
  1003. top_reg:
  1004. // should have been translated into a memory location by the
  1005. // register allocator)
  1006. if (cs_no_regalloc in current_settings.globalswitches) then
  1007. getopstr:=std_regname(o.reg)
  1008. else
  1009. internalerror(2010122803);
  1010. top_const:
  1011. str(o.val,result);
  1012. top_ref:
  1013. getopstr:=getreferencestring(o.ref^);
  1014. top_single:
  1015. begin
  1016. result:=constsingle(o.sval);
  1017. end;
  1018. top_double:
  1019. begin
  1020. result:=constdouble(o.dval);
  1021. end;
  1022. top_string:
  1023. begin
  1024. result:=constastr(o.pcval,o.pcvallen);
  1025. end;
  1026. top_wstring:
  1027. begin
  1028. result:=constwstr(o.pwstrval^.data,getlengthwidestring(o.pwstrval));
  1029. end
  1030. else
  1031. internalerror(2010122802);
  1032. end;
  1033. end;
  1034. procedure TJasminInstrWriter.WriteInstruction(hp: tai);
  1035. var
  1036. s: ansistring;
  1037. i: byte;
  1038. sep: ansistring;
  1039. begin
  1040. s:=#9+jas_op2str[taicpu(hp).opcode];
  1041. if taicpu(hp).ops<>0 then
  1042. begin
  1043. sep:=#9;
  1044. for i:=0 to taicpu(hp).ops-1 do
  1045. begin
  1046. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  1047. sep:=' ';
  1048. end;
  1049. end;
  1050. owner.AsmWriteLn(s);
  1051. end;
  1052. {****************************************************************************}
  1053. { Jasmin Instruction Writer }
  1054. {****************************************************************************}
  1055. const
  1056. as_jvm_jasmin_info : tasminfo =
  1057. (
  1058. id : as_jvm_jasmin;
  1059. idtxt : 'Jasmin';
  1060. asmbin : 'java';
  1061. asmcmd : '-jar $JASMINJAR $ASM -d $OBJDIR';
  1062. supported_targets : [system_jvm_java32];
  1063. flags : [];
  1064. labelprefix : 'L';
  1065. comment : ' ; ';
  1066. );
  1067. begin
  1068. RegisterAssembler(as_jvm_jasmin_info,TJasminAssembler);
  1069. end.