wasa.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 wasa;
  18. {$mode objfpc}{$H+}
  19. uses
  20. SysUtils, Classes, watparser, watscanner, wasmmodule, wasmbinwriter,
  21. wasmnormalize;
  22. type
  23. TAsmParams = record
  24. SrcFile : string;
  25. Reloc : Boolean;
  26. DstObjFile : string;
  27. end;
  28. procedure DefaultParams(out p: TAsmParams);
  29. begin
  30. p.SrcFile := '';
  31. p.Reloc := false;
  32. p.DstObjFile := '';
  33. end;
  34. procedure WriteBin(const p: TAsmParams; m: TWasmModule);
  35. var
  36. f : TFileStream;
  37. begin
  38. f := TFileStream.Create(p.DstObjFile, fmCreate);
  39. try
  40. Normalize(m);
  41. WriteModule(m, f, p.Reloc, p.Reloc);
  42. finally
  43. f.Free;
  44. end;
  45. end;
  46. procedure Run(const prm: TAsmParams);
  47. var
  48. st : TFileStream;
  49. s : string;
  50. p : TWatScanner;
  51. m : TWasmModule;
  52. err : string;
  53. begin
  54. st := TFileStream.Create(prm.SrcFile, fmOpenRead or fmShareDenyNone);
  55. p := TWatScanner.Create;
  56. try
  57. SetLength(s, st.Size);
  58. if length(s)>0 then st.Read(s[1], length(s));
  59. p.SetSource(s);
  60. m := TWasmModule.Create;
  61. try
  62. if not ParseModule(p, m, err) then
  63. writeln('Error: ', err)
  64. else
  65. WriteBin(prm, m);
  66. finally
  67. m.Free;
  68. end;
  69. finally
  70. p.Free;
  71. st.Free;
  72. end;
  73. end;
  74. procedure ParseParams(var p: TAsmParams);
  75. var
  76. i : integer;
  77. s : string;
  78. ls : string;
  79. begin
  80. i:=1;
  81. while i<=ParamCount do begin
  82. s := ParamStr(i);
  83. if (s<>'') and (s[1]='-') then begin
  84. ls := AnsiLowerCase(s);
  85. if ls = '-o' then begin
  86. inc(i);
  87. if (i<=ParamCount) then
  88. p.DstObjFile:=ParamStr(i);
  89. end else if ls = '-r' then
  90. p.Reloc := true;
  91. end else
  92. p.SrcFile := s;
  93. inc(i);
  94. end;
  95. if (p.SrcFile<>'') then begin
  96. p.SrcFile := ExpandFileName(p.SrcFile);
  97. if (p.DstObjFile = '') then
  98. p.DstObjFile := ChangeFileExt(p.SrcFile, '.wasm')
  99. end;
  100. end;
  101. var
  102. prm : TAsmParams;
  103. begin
  104. DefaultParams(prm);
  105. ParseParams(prm);
  106. if (prm.SrcFile='') then begin
  107. writeln('please specify the input .wat file');
  108. exit;
  109. end;
  110. if not FileExists(prm.SrcFile) then begin
  111. writeln('file doesn''t exist: ', prm.SrcFile);
  112. exit;
  113. end;
  114. try
  115. Run(prm);
  116. except
  117. on e: exception do
  118. writeln(e.message);
  119. end
  120. end.