wattest.lpr 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. { This file is part of wasmbin - a collection of WebAssembly binary utils.
  2. Copyright (C) 2019, 2020 Dmitry Boyarintsev <[email protected]>
  3. Copyright (C) 2020 by the Free Pascal development team
  4. This source is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 2 of the License, or (at your option)
  7. any later version.
  8. This code is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  11. details.
  12. A copy of the GNU General Public License is available on the World Wide Web
  13. at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
  14. to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1335, USA.
  16. }
  17. program wattest;
  18. {$mode objfpc}{$H+}
  19. uses
  20. SysUtils, Classes, watparser, watscanner, wasmmodule, wasmbinwriter,
  21. wasmnormalize;
  22. procedure Traverse(p: TWatScanner);
  23. begin
  24. while p.Next do begin
  25. write(p.token,' ', p.resText);
  26. if p.token = weInstr then
  27. write('; inst = $', IntToHex(p.instrCode,2))
  28. else if p.token = weError then begin
  29. writeln('offset = ',p.ofs,' ',p.resText);
  30. break;
  31. end;
  32. writeln;
  33. end;
  34. end;
  35. procedure WriteBin(const fndst: string; m: TWasmModule; WriteReloc: Boolean);
  36. var
  37. f : TFileStream;
  38. begin
  39. f := TFileStream.Create(fndst, fmCreate);
  40. try
  41. Normalize(m);
  42. WriteModule(m, f, WriteReloc, WriteReloc);
  43. finally
  44. f.Free;
  45. end;
  46. end;
  47. procedure Run(const fn: string; const doTraverse: Boolean; doReloc: Boolean);
  48. var
  49. st : TFileStream;
  50. s : string;
  51. p : TWatScanner;
  52. m : TWasmModule;
  53. err : string;
  54. begin
  55. st := TFileStream.Create(fn, fmOpenRead or fmShareDenyNone);
  56. p := TWatScanner.Create;
  57. try
  58. SetLength(s, st.Size);
  59. if length(s)>0 then st.Read(s[1], length(s));
  60. p.SetSource(s);
  61. if doTraverse then begin
  62. Traverse(p);
  63. Exit;
  64. end;
  65. m := TWasmModule.Create;
  66. try
  67. if not ParseModule(p, m, err) then
  68. writeln('Error: ', err)
  69. else
  70. WriteBin( ChangeFileExt(fn,'.wasm'), m, doReloc);
  71. finally
  72. m.Free;
  73. end;
  74. finally
  75. p.Free;
  76. st.Free;
  77. end;
  78. end;
  79. var
  80. gFn : string;
  81. gCommand : string = '';
  82. gReloc : Boolean = true;
  83. gCatch : Boolean = false;
  84. procedure ParseParams;
  85. var
  86. i : integer;
  87. s : string;
  88. ls : string;
  89. begin
  90. i:=1;
  91. while i<=ParamCount do begin
  92. s := ParamStr(i);
  93. if (s<>'') and (s[1]='-') then begin
  94. ls := AnsiLowerCase(s);
  95. if ls = '-noreloc' then gReloc := false
  96. else if ls = '-catch' then gCatch := true
  97. else gCommand:=ls;
  98. end else
  99. gFn := s;
  100. inc(i);
  101. end;
  102. end;
  103. begin
  104. ParseParams;
  105. if (gFn='') then begin
  106. writeln('please sepcify the input .wat file');
  107. writeln('other use:');
  108. writeln(' -compile %inpfn%');
  109. writeln(' -noreloc - prevents relocation information from being written');
  110. writeln(' -traverse %inpfn%');
  111. exit;
  112. end;
  113. if not FileExists(gFn) then begin
  114. writeln('file doesn''t exist: ', gFn);
  115. exit;
  116. end;
  117. if gCatch then
  118. try
  119. Run(gFn, gCommand = '-traverse', gReloc);
  120. except
  121. on e: exception do
  122. writeln(e.message);
  123. end
  124. else
  125. Run(gFn, gCommand = '-traverse', gReloc);
  126. end.