ogwasm.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. {
  2. Copyright (c) 2021 by Nikolay Nikolov
  3. Contains the WebAssembly binary module format reader and 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 ogwasm;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,globtype,
  23. { target }
  24. systems,cpubase,
  25. { assembler }
  26. aasmbase,assemble,aasmcpu,
  27. { WebAssembly module format definitions }
  28. wasmbase,
  29. { output }
  30. ogbase,
  31. owbase;
  32. type
  33. { TWasmObjSymbolExtraData }
  34. TWasmObjSymbolExtraData = class(TFPHashObject)
  35. TypeIdx: Integer;
  36. ImportModule: string;
  37. ImportName: string;
  38. Locals: array of TWasmBasicType;
  39. constructor Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  40. procedure AddLocal(bastyp: TWasmBasicType);
  41. end;
  42. { TWasmObjSection }
  43. TWasmObjSection = class(TObjSection)
  44. public
  45. SegIdx: Integer;
  46. SegOfs: qword;
  47. function IsCode: Boolean;
  48. function IsData: Boolean;
  49. end;
  50. { TWasmObjData }
  51. TWasmObjData = class(TObjData)
  52. private
  53. FFuncTypes: array of TWasmFuncType;
  54. FObjSymbolsExtraDataList: TFPHashObjectList;
  55. FLastFuncName: string;
  56. function is_smart_section(atype:TAsmSectiontype):boolean;
  57. function sectionname_gas(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  58. public
  59. constructor create(const n:string);override;
  60. destructor destroy; override;
  61. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  62. procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);override;
  63. function AddOrCreateObjSymbolExtraData(const symname:TSymStr): TWasmObjSymbolExtraData;
  64. function AddFuncType(wft: TWasmFuncType): integer;
  65. procedure DeclareFuncType(ft: tai_functype);
  66. procedure DeclareImportModule(aim: tai_import_module);
  67. procedure DeclareImportName(ain: tai_import_name);
  68. procedure DeclareLocal(al: tai_local);
  69. end;
  70. { TWasmObjOutput }
  71. TWasmObjOutput = class(tObjOutput)
  72. private
  73. FData: TWasmObjData;
  74. FWasmSections: array [TWasmSectionID] of tdynamicarray;
  75. procedure WriteUleb(d: tdynamicarray; v: uint64);
  76. procedure WriteUleb(w: TObjectWriter; v: uint64);
  77. procedure WriteSleb(d: tdynamicarray; v: int64);
  78. procedure WriteByte(d: tdynamicarray; b: byte);
  79. procedure WriteName(d: tdynamicarray; const s: string);
  80. procedure WriteWasmSection(wsid: TWasmSectionID);
  81. procedure CopyDynamicArray(src, dest: tdynamicarray; size: QWord);
  82. procedure WriteZeros(dest: tdynamicarray; size: QWord);
  83. procedure WriteWasmResultType(dest: tdynamicarray; wrt: TWasmResultType);
  84. procedure WriteWasmBasicType(dest: tdynamicarray; wbt: TWasmBasicType);
  85. function IsExternalFunction(sym: TObjSymbol): Boolean;
  86. procedure WriteFunctionLocals(dest: tdynamicarray; ed: TWasmObjSymbolExtraData);
  87. procedure WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
  88. protected
  89. function writeData(Data:TObjData):boolean;override;
  90. public
  91. constructor create(AWriter:TObjectWriter);override;
  92. destructor destroy;override;
  93. end;
  94. { TWasmAssembler }
  95. TWasmAssembler = class(tinternalassembler)
  96. constructor create(info: pasminfo; smart:boolean);override;
  97. end;
  98. implementation
  99. uses
  100. verbose;
  101. {****************************************************************************
  102. TWasmObjSymbolExtraData
  103. ****************************************************************************}
  104. constructor TWasmObjSymbolExtraData.Create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  105. begin
  106. inherited Create(HashObjectList,s);
  107. TypeIdx:=-1;
  108. end;
  109. procedure TWasmObjSymbolExtraData.AddLocal(bastyp: TWasmBasicType);
  110. begin
  111. SetLength(Locals,Length(Locals)+1);
  112. Locals[High(Locals)]:=bastyp;
  113. end;
  114. {****************************************************************************
  115. TWasmObjSection
  116. ****************************************************************************}
  117. function TWasmObjSection.IsCode: Boolean;
  118. const
  119. CodePrefix = '.text';
  120. begin
  121. result:=(Length(Name)>=Length(CodePrefix)) and
  122. (Copy(Name,1,Length(CodePrefix))=CodePrefix);
  123. end;
  124. function TWasmObjSection.IsData: Boolean;
  125. begin
  126. result:=not IsCode;
  127. end;
  128. {****************************************************************************
  129. TWasmObjData
  130. ****************************************************************************}
  131. function TWasmObjData.is_smart_section(atype: TAsmSectiontype): boolean;
  132. begin
  133. { For bss we need to set some flags that are target dependent,
  134. it is easier to disable it for smartlinking. It doesn't take up
  135. filespace }
  136. result:=not(target_info.system in systems_darwin) and
  137. create_smartlink_sections and
  138. (atype<>sec_toc) and
  139. (atype<>sec_user) and
  140. { on embedded systems every byte counts, so smartlink bss too }
  141. ((atype<>sec_bss) or (target_info.system in (systems_embedded+systems_freertos)));
  142. end;
  143. function TWasmObjData.sectionname_gas(atype: TAsmSectiontype;
  144. const aname: string; aorder: TAsmSectionOrder): string;
  145. const
  146. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  147. '.text',
  148. '.data',
  149. { why doesn't .rodata work? (FK) }
  150. { sometimes we have to create a data.rel.ro instead of .rodata, e.g. for }
  151. { vtables (and anything else containing relocations), otherwise those are }
  152. { not relocated properly on e.g. linux/ppc64. g++ generates there for a }
  153. { vtable for a class called Window: }
  154. { .section .data.rel.ro._ZTV6Window,"awG",@progbits,_ZTV6Window,comdat }
  155. { TODO: .data.ro not yet working}
  156. {$if defined(arm) or defined(riscv64) or defined(powerpc)}
  157. '.rodata',
  158. {$else defined(arm) or defined(riscv64) or defined(powerpc)}
  159. '.data',
  160. {$endif defined(arm) or defined(riscv64) or defined(powerpc)}
  161. '.rodata',
  162. '.bss',
  163. '.threadvar',
  164. '.pdata',
  165. '', { stubs }
  166. '__DATA,__nl_symbol_ptr',
  167. '__DATA,__la_symbol_ptr',
  168. '__DATA,__mod_init_func',
  169. '__DATA,__mod_term_func',
  170. '.stab',
  171. '.stabstr',
  172. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  173. '.eh_frame',
  174. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  175. '.fpc',
  176. '.toc',
  177. '.init',
  178. '.fini',
  179. '.objc_class',
  180. '.objc_meta_class',
  181. '.objc_cat_cls_meth',
  182. '.objc_cat_inst_meth',
  183. '.objc_protocol',
  184. '.objc_string_object',
  185. '.objc_cls_meth',
  186. '.objc_inst_meth',
  187. '.objc_cls_refs',
  188. '.objc_message_refs',
  189. '.objc_symbols',
  190. '.objc_category',
  191. '.objc_class_vars',
  192. '.objc_instance_vars',
  193. '.objc_module_info',
  194. '.objc_class_names',
  195. '.objc_meth_var_types',
  196. '.objc_meth_var_names',
  197. '.objc_selector_strs',
  198. '.objc_protocol_ext',
  199. '.objc_class_ext',
  200. '.objc_property',
  201. '.objc_image_info',
  202. '.objc_cstring_object',
  203. '.objc_sel_fixup',
  204. '__DATA,__objc_data',
  205. '__DATA,__objc_const',
  206. '.objc_superrefs',
  207. '__DATA, __datacoal_nt,coalesced',
  208. '.objc_classlist',
  209. '.objc_nlclasslist',
  210. '.objc_catlist',
  211. '.obcj_nlcatlist',
  212. '.objc_protolist',
  213. '.stack',
  214. '.heap',
  215. '.gcc_except_table',
  216. '.ARM.attributes'
  217. );
  218. var
  219. sep : string[3];
  220. secname : string;
  221. begin
  222. secname:=secnames[atype];
  223. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  224. begin
  225. result:=secname+'.'+aname;
  226. exit;
  227. end;
  228. if atype=sec_threadvar then
  229. begin
  230. if (target_info.system in (systems_windows+systems_wince)) then
  231. secname:='.tls'
  232. else if (target_info.system in systems_linux) then
  233. secname:='.tbss';
  234. end;
  235. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  236. Thus, data which normally goes into .rodata and .rodata_norel sections must
  237. end up in .data section }
  238. if (atype in [sec_rodata,sec_rodata_norel]) and
  239. (target_info.system in [system_i386_go32v2,system_m68k_palmos]) then
  240. secname:='.data';
  241. { Windows correctly handles reallocations in readonly sections }
  242. if (atype=sec_rodata) and
  243. (target_info.system in systems_all_windows+systems_nativent-[system_i8086_win16]) then
  244. secname:='.rodata';
  245. { section type user gives the user full controll on the section name }
  246. if atype=sec_user then
  247. secname:=aname;
  248. if is_smart_section(atype) and (aname<>'') then
  249. begin
  250. case aorder of
  251. secorder_begin :
  252. sep:='.b_';
  253. secorder_end :
  254. sep:='.z_';
  255. else
  256. sep:='.n_';
  257. end;
  258. result:=secname+sep+aname
  259. end
  260. else
  261. result:=secname;
  262. end;
  263. constructor TWasmObjData.create(const n: string);
  264. begin
  265. inherited;
  266. CObjSection:=TWasmObjSection;
  267. FObjSymbolsExtraDataList:=TFPHashObjectList.Create;
  268. end;
  269. destructor TWasmObjData.destroy;
  270. var
  271. i: Integer;
  272. begin
  273. FObjSymbolsExtraDataList.Free;
  274. for i:=low(FFuncTypes) to high(FFuncTypes) do
  275. begin
  276. FFuncTypes[i].free;
  277. FFuncTypes[i]:=nil;
  278. end;
  279. inherited destroy;
  280. end;
  281. function TWasmObjData.sectionname(atype: TAsmSectiontype;
  282. const aname: string; aorder: TAsmSectionOrder): string;
  283. begin
  284. if (atype=sec_fpc) or (atype=sec_threadvar) then
  285. atype:=sec_data;
  286. Result:=sectionname_gas(atype, aname, aorder);
  287. end;
  288. procedure TWasmObjData.writeReloc(Data: TRelocDataInt; len: aword;
  289. p: TObjSymbol; Reloctype: TObjRelocationType);
  290. begin
  291. end;
  292. function TWasmObjData.AddOrCreateObjSymbolExtraData(const symname: TSymStr): TWasmObjSymbolExtraData;
  293. begin
  294. result:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(symname));
  295. if not assigned(result) then
  296. result:=TWasmObjSymbolExtraData.Create(FObjSymbolsExtraDataList,symname);
  297. end;
  298. function TWasmObjData.AddFuncType(wft: TWasmFuncType): integer;
  299. var
  300. i: Integer;
  301. begin
  302. for i:=low(FFuncTypes) to high(FFuncTypes) do
  303. if wft.Equals(FFuncTypes[i]) then
  304. exit(i);
  305. result:=Length(FFuncTypes);
  306. SetLength(FFuncTypes,result+1);
  307. FFuncTypes[result]:=TWasmFuncType.Create(wft);
  308. end;
  309. procedure TWasmObjData.DeclareFuncType(ft: tai_functype);
  310. var
  311. i: Integer;
  312. ObjSymExtraData: TWasmObjSymbolExtraData;
  313. begin
  314. FLastFuncName:=ft.funcname;
  315. i:=AddFuncType(ft.functype);
  316. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(ft.funcname);
  317. ObjSymExtraData.TypeIdx:=i;
  318. end;
  319. procedure TWasmObjData.DeclareImportModule(aim: tai_import_module);
  320. var
  321. ObjSymExtraData: TWasmObjSymbolExtraData;
  322. begin
  323. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(aim.symname);
  324. ObjSymExtraData.ImportModule:=aim.importmodule;
  325. end;
  326. procedure TWasmObjData.DeclareImportName(ain: tai_import_name);
  327. var
  328. ObjSymExtraData: TWasmObjSymbolExtraData;
  329. begin
  330. ObjSymExtraData:=AddOrCreateObjSymbolExtraData(ain.symname);
  331. ObjSymExtraData.ImportName:=ain.importname;
  332. end;
  333. procedure TWasmObjData.DeclareLocal(al: tai_local);
  334. var
  335. ObjSymExtraData: TWasmObjSymbolExtraData;
  336. begin
  337. ObjSymExtraData:=TWasmObjSymbolExtraData(FObjSymbolsExtraDataList.Find(FLastFuncName));
  338. ObjSymExtraData.AddLocal(al.bastyp);
  339. end;
  340. {****************************************************************************
  341. TWasmObjOutput
  342. ****************************************************************************}
  343. procedure TWasmObjOutput.WriteUleb(d: tdynamicarray; v: uint64);
  344. var
  345. b: byte;
  346. begin
  347. repeat
  348. b:=byte(v) and 127;
  349. v:=v shr 7;
  350. if v<>0 then
  351. b:=b or 128;
  352. d.write(b,1);
  353. until v=0;
  354. end;
  355. procedure TWasmObjOutput.WriteUleb(w: TObjectWriter; v: uint64);
  356. var
  357. b: byte;
  358. begin
  359. repeat
  360. b:=byte(v) and 127;
  361. v:=v shr 7;
  362. if v<>0 then
  363. b:=b or 128;
  364. w.write(b,1);
  365. until v=0;
  366. end;
  367. procedure TWasmObjOutput.WriteSleb(d: tdynamicarray; v: int64);
  368. var
  369. b: byte;
  370. Done: Boolean=false;
  371. begin
  372. repeat
  373. b:=byte(v) and 127;
  374. v:=SarInt64(v,7);
  375. if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
  376. Done:=true
  377. else
  378. b:=b or 128;
  379. d.write(b,1);
  380. until Done;
  381. end;
  382. procedure TWasmObjOutput.WriteByte(d: tdynamicarray; b: byte);
  383. begin
  384. d.write(b,1);
  385. end;
  386. procedure TWasmObjOutput.WriteName(d: tdynamicarray; const s: string);
  387. begin
  388. WriteUleb(d,Length(s));
  389. d.writestr(s);
  390. end;
  391. procedure TWasmObjOutput.WriteWasmSection(wsid: TWasmSectionID);
  392. var
  393. b: byte;
  394. begin
  395. b:=ord(wsid);
  396. Writer.write(b,1);
  397. WriteUleb(Writer,FWasmSections[wsid].size);
  398. Writer.writearray(FWasmSections[wsid]);
  399. end;
  400. procedure TWasmObjOutput.CopyDynamicArray(src, dest: tdynamicarray; size: QWord);
  401. var
  402. buf: array [0..4095] of byte;
  403. bs: Integer;
  404. begin
  405. while size>0 do
  406. begin
  407. if size<SizeOf(buf) then
  408. bs:=Integer(size)
  409. else
  410. bs:=SizeOf(buf);
  411. src.read(buf,bs);
  412. dest.write(buf,bs);
  413. dec(size,bs);
  414. end;
  415. end;
  416. procedure TWasmObjOutput.WriteZeros(dest: tdynamicarray; size: QWord);
  417. var
  418. buf : array[0..1023] of byte;
  419. bs: Integer;
  420. begin
  421. fillchar(buf,sizeof(buf),0);
  422. while size>0 do
  423. begin
  424. if size<SizeOf(buf) then
  425. bs:=Integer(size)
  426. else
  427. bs:=SizeOf(buf);
  428. dest.write(buf,bs);
  429. dec(size,bs);
  430. end;
  431. end;
  432. procedure TWasmObjOutput.WriteWasmResultType(dest: tdynamicarray; wrt: TWasmResultType);
  433. var
  434. i: Integer;
  435. begin
  436. WriteUleb(dest,Length(wrt));
  437. for i:=low(wrt) to high(wrt) do
  438. WriteWasmBasicType(dest,wrt[i]);
  439. end;
  440. procedure TWasmObjOutput.WriteWasmBasicType(dest: tdynamicarray; wbt: TWasmBasicType);
  441. begin
  442. case wbt of
  443. wbt_i32:
  444. WriteByte(dest,$7F);
  445. wbt_i64:
  446. WriteByte(dest,$7E);
  447. wbt_f32:
  448. WriteByte(dest,$7D);
  449. wbt_f64:
  450. WriteByte(dest,$7C);
  451. end;
  452. end;
  453. function TWasmObjOutput.IsExternalFunction(sym: TObjSymbol): Boolean;
  454. begin
  455. result:=(sym.bind=AB_EXTERNAL) and (TWasmObjData(sym.ObjData).FObjSymbolsExtraDataList.Find(sym.Name)<>nil);
  456. end;
  457. procedure TWasmObjOutput.WriteFunctionLocals(dest: tdynamicarray; ed: TWasmObjSymbolExtraData);
  458. var
  459. i,
  460. rle_entries,
  461. cnt: Integer;
  462. lasttype: TWasmBasicType;
  463. begin
  464. if Length(ed.Locals)=0 then
  465. begin
  466. WriteUleb(dest,0);
  467. exit;
  468. end;
  469. rle_entries:=1;
  470. for i:=low(ed.Locals)+1 to high(ed.Locals) do
  471. if ed.Locals[i]<>ed.Locals[i-1] then
  472. inc(rle_entries);
  473. WriteUleb(dest,rle_entries);
  474. lasttype:=ed.Locals[Low(ed.Locals)];
  475. cnt:=1;
  476. for i:=low(ed.Locals)+1 to high(ed.Locals) do
  477. if ed.Locals[i]=ed.Locals[i-1] then
  478. inc(cnt)
  479. else
  480. begin
  481. WriteUleb(dest,cnt);
  482. WriteWasmBasicType(dest,lasttype);
  483. lasttype:=ed.Locals[i];
  484. cnt:=1;
  485. end;
  486. WriteUleb(dest,cnt);
  487. WriteWasmBasicType(dest,lasttype);
  488. end;
  489. procedure TWasmObjOutput.WriteFunctionCode(dest: tdynamicarray; objsym: TObjSymbol);
  490. var
  491. encoded_locals: tdynamicarray;
  492. ObjSymExtraData: TWasmObjSymbolExtraData;
  493. codelen: LongWord;
  494. begin
  495. ObjSymExtraData:=TWasmObjSymbolExtraData(FData.FObjSymbolsExtraDataList.Find(objsym.Name));
  496. encoded_locals:=tdynamicarray.Create(64);
  497. WriteFunctionLocals(encoded_locals,ObjSymExtraData);
  498. codelen:=encoded_locals.size+1;
  499. WriteUleb(dest,codelen);
  500. encoded_locals.seek(0);
  501. CopyDynamicArray(encoded_locals,dest,encoded_locals.size);
  502. WriteByte(dest,$0B);
  503. encoded_locals.Free;
  504. end;
  505. function TWasmObjOutput.writeData(Data:TObjData):boolean;
  506. var
  507. i: Integer;
  508. objsec: TWasmObjSection;
  509. segment_count: Integer = 0;
  510. cur_seg_ofs: qword = 0;
  511. types_count,
  512. imports_count: Integer;
  513. import_functions_count: Integer = 0;
  514. functions_count: Integer = 0;
  515. objsym: TObjSymbol;
  516. begin
  517. FData:=TWasmObjData(Data);
  518. for i:=0 to Data.ObjSymbolList.Count-1 do
  519. begin
  520. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  521. if IsExternalFunction(objsym) then
  522. Inc(import_functions_count);
  523. if objsym.typ=AT_FUNCTION then
  524. Inc(functions_count);
  525. end;
  526. types_count:=Length(TWasmObjData(Data).FFuncTypes);
  527. WriteUleb(FWasmSections[wsiType],types_count);
  528. for i:=0 to types_count-1 do
  529. with TWasmObjData(Data).FFuncTypes[i] do
  530. begin
  531. WriteByte(FWasmSections[wsiType],$60);
  532. WriteWasmResultType(FWasmSections[wsiType],params);
  533. WriteWasmResultType(FWasmSections[wsiType],results);
  534. end;
  535. for i:=0 to Data.ObjSectionList.Count-1 do
  536. begin
  537. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  538. if objsec.IsCode then
  539. objsec.SegIdx:=-1
  540. else
  541. begin
  542. objsec.SegIdx:=segment_count;
  543. objsec.SegOfs:=cur_seg_ofs;
  544. Inc(segment_count);
  545. Inc(cur_seg_ofs,objsec.Size);
  546. end;
  547. end;
  548. WriteUleb(FWasmSections[wsiData],segment_count);
  549. for i:=0 to Data.ObjSectionList.Count-1 do
  550. begin
  551. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  552. if objsec.IsData then
  553. begin
  554. WriteByte(FWasmSections[wsiData],0);
  555. WriteByte(FWasmSections[wsiData],$41);
  556. WriteSleb(FWasmSections[wsiData],objsec.SegOfs);
  557. WriteByte(FWasmSections[wsiData],$0b);
  558. WriteUleb(FWasmSections[wsiData],objsec.Size);
  559. if oso_Data in objsec.SecOptions then
  560. begin
  561. objsec.Data.seek(0);
  562. CopyDynamicArray(objsec.Data,FWasmSections[wsiData],objsec.Size);
  563. end
  564. else
  565. begin
  566. WriteZeros(FWasmSections[wsiData],objsec.Size);
  567. end;
  568. end;
  569. end;
  570. WriteUleb(FWasmSections[wsiDataCount],segment_count);
  571. imports_count:=3+import_functions_count;
  572. WriteUleb(FWasmSections[wsiImport],imports_count);
  573. { import[0] }
  574. WriteName(FWasmSections[wsiImport],'env');
  575. WriteName(FWasmSections[wsiImport],'__linear_memory');
  576. WriteByte(FWasmSections[wsiImport],$02); { mem }
  577. WriteByte(FWasmSections[wsiImport],$00); { min }
  578. WriteUleb(FWasmSections[wsiImport],1); { 1 page }
  579. { import[1] }
  580. WriteName(FWasmSections[wsiImport],'env');
  581. WriteName(FWasmSections[wsiImport],'__stack_pointer');
  582. WriteByte(FWasmSections[wsiImport],$03); { global }
  583. WriteByte(FWasmSections[wsiImport],$7F); { i32 }
  584. WriteByte(FWasmSections[wsiImport],$01); { var }
  585. { import[2]..import[imports_count-2] }
  586. for i:=0 to Data.ObjSymbolList.Count-1 do
  587. begin
  588. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  589. if IsExternalFunction(objsym) then
  590. begin
  591. WriteName(FWasmSections[wsiImport],'env');
  592. WriteName(FWasmSections[wsiImport],objsym.Name);
  593. WriteByte(FWasmSections[wsiImport],$00); { func }
  594. WriteUleb(FWasmSections[wsiImport],TWasmObjSymbolExtraData(TWasmObjData(Data).FObjSymbolsExtraDataList.Find(objsym.Name)).TypeIdx);
  595. end;
  596. end;
  597. { import[imports_count-1] }
  598. WriteName(FWasmSections[wsiImport],'env');
  599. WriteName(FWasmSections[wsiImport],'__indirect_function_table');
  600. WriteByte(FWasmSections[wsiImport],$01); { table }
  601. WriteByte(FWasmSections[wsiImport],$70); { funcref }
  602. WriteByte(FWasmSections[wsiImport],$00); { min }
  603. WriteUleb(FWasmSections[wsiImport],1); { 1 }
  604. WriteUleb(FWasmSections[wsiFunction],functions_count);
  605. WriteUleb(FWasmSections[wsiCode],functions_count);
  606. for i:=0 to Data.ObjSymbolList.Count-1 do
  607. begin
  608. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  609. if objsym.typ=AT_FUNCTION then
  610. begin
  611. WriteUleb(FWasmSections[wsiFunction],TWasmObjSymbolExtraData(TWasmObjData(Data).FObjSymbolsExtraDataList.Find(objsym.Name)).TypeIdx);
  612. WriteFunctionCode(FWasmSections[wsiCode],objsym);
  613. end;
  614. end;
  615. Writer.write(WasmModuleMagic,SizeOf(WasmModuleMagic));
  616. Writer.write(WasmVersion,SizeOf(WasmVersion));
  617. WriteWasmSection(wsiType);
  618. WriteWasmSection(wsiImport);
  619. WriteWasmSection(wsiFunction);
  620. WriteWasmSection(wsiDataCount);
  621. WriteWasmSection(wsiCode);
  622. WriteWasmSection(wsiData);
  623. Writeln('ObjSymbolList:');
  624. for i:=0 to Data.ObjSymbolList.Count-1 do
  625. begin
  626. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  627. Write(objsym.Name, ' bind=', objsym.Bind, ' typ=', objsym.typ, ' address=', objsym.address, ' objsection=');
  628. if assigned(objsym.objsection) then
  629. Write(objsym.objsection.Name)
  630. else
  631. Write('nil');
  632. Writeln;
  633. end;
  634. Writeln('ObjSectionList:');
  635. for i:=0 to Data.ObjSectionList.Count-1 do
  636. begin
  637. objsec:=TWasmObjSection(Data.ObjSectionList[i]);
  638. Writeln(objsec.Name, ' IsCode=', objsec.IsCode, ' IsData=', objsec.IsData, ' Size=', objsec.Size, ' MemPos=', objsec.MemPos, ' DataPos=', objsec.DataPos, ' SegIdx=', objsec.SegIdx);
  639. end;
  640. result:=true;
  641. end;
  642. constructor TWasmObjOutput.create(AWriter: TObjectWriter);
  643. var
  644. i: TWasmSectionID;
  645. begin
  646. inherited;
  647. cobjdata:=TWasmObjData;
  648. for i in TWasmSectionID do
  649. FWasmSections[i] := tdynamicarray.create(SectionDataMaxGrow);
  650. end;
  651. destructor TWasmObjOutput.destroy;
  652. var
  653. i: TWasmSectionID;
  654. begin
  655. for i in TWasmSectionID do
  656. FWasmSections[i].Free;
  657. inherited destroy;
  658. end;
  659. {****************************************************************************
  660. TWasmAssembler
  661. ****************************************************************************}
  662. constructor TWasmAssembler.Create(info: pasminfo; smart:boolean);
  663. begin
  664. inherited;
  665. CObjOutput:=TWasmObjOutput;
  666. end;
  667. {*****************************************************************************
  668. Initialize
  669. *****************************************************************************}
  670. {$ifdef wasm32}
  671. const
  672. as_wasm32_wasm_info : tasminfo =
  673. (
  674. id : as_wasm32_wasm;
  675. idtxt : 'OMF';
  676. asmbin : '';
  677. asmcmd : '';
  678. supported_targets : [system_wasm32_embedded,system_wasm32_wasi];
  679. flags : [af_outputbinary,af_smartlink_sections];
  680. labelprefix : '..@';
  681. labelmaxlen : -1;
  682. comment : '; ';
  683. dollarsign: '$';
  684. );
  685. {$endif wasm32}
  686. initialization
  687. {$ifdef wasm32}
  688. RegisterAssembler(as_wasm32_wasm_info,TWasmAssembler);
  689. {$endif wasm32}
  690. end.