wasm32.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. {%MainUnit system.pp}
  2. {
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2017 by the Free Pascal development team.
  5. Processor dependent implementation for the system unit for
  6. WebAssembly 32-bit
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$ifdef FPC_WASM_THREADS}
  14. procedure fpc_wasm32_init_tls(memory: Pointer);external name '__wasm_init_tls';
  15. {$endif FPC_WASM_THREADS}
  16. procedure fpc_wasm_invoke_helper(CodeAddress: CodePointer; Args: Pointer; Result: Pointer);assembler;nostackframe;
  17. asm
  18. unreachable
  19. end;
  20. {$define FPC_SYSTEM_HAS_FPC_CPUINIT}
  21. procedure fpc_cpuinit;
  22. begin
  23. end;
  24. {$define FPC_SYSTEM_HAS_FILLCHAR}
  25. Procedure FillChar(var x;count:SizeInt;value:byte);
  26. begin
  27. if count>0 then
  28. fpc_wasm32_memory_fill(PtrUInt(@x),value,count);
  29. end;
  30. {$define FPC_SYSTEM_HAS_MOVE}
  31. procedure Move(const source;var dest;count:SizeInt);[public, alias: 'FPC_MOVE'];
  32. begin
  33. if count>0 then
  34. fpc_wasm32_memory_copy(PtrUInt(@dest),PtrUInt(@source),count);
  35. end;
  36. {$define FPC_SYSTEM_HAS_GET_PC_ADDR}
  37. Function Get_pc_addr : CodePointer;
  38. begin
  39. { dummy, produces a small, fake backtrace, otherwise programs terminate
  40. with no output at all, in case of a runtime error }
  41. result:=CodePointer($eeeeeeef);
  42. end;
  43. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  44. function get_caller_addr(framebp:pointer;addr:codepointer=nil):pointer;
  45. begin
  46. { dummy, produces a small, fake backtrace, otherwise programs terminate
  47. with no output at all, in case of a runtime error }
  48. if addr=CodePointer($eeeeeeef) then
  49. result:=CodePointer($eeeeeeee)
  50. else
  51. result:=nil;
  52. end;
  53. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  54. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;
  55. begin
  56. result:=nil;
  57. end;
  58. {$define FPC_SYSTEM_HAS_SPTR}
  59. function Sptr : pointer; assembler; nostackframe;
  60. asm
  61. global.get $__stack_pointer
  62. end;
  63. {$ifdef VER3_2}
  64. function InterLockedDecrement (var Target: longint) : longint;
  65. {$else VER3_2}
  66. {$define FPC_SYSTEM_HAS_ATOMIC_DEC_32}
  67. function fpc_atomic_dec_32 (var Target: longint) : longint;
  68. {$endif VER3_2}
  69. begin
  70. {$ifdef FPC_WASM_THREADS}
  71. {$push}{$R-,Q-}
  72. Result:=fpc_wasm32_i32_atomic_rmw_sub(@Target,1)-1;
  73. {$pop}
  74. {$else FPC_WASM_THREADS}
  75. dec(Target);
  76. Result:=Target;
  77. {$endif FPC_WASM_THREADS}
  78. end;
  79. {$ifdef VER3_2}
  80. function InterLockedIncrement (var Target: longint) : longint;
  81. {$else VER3_2}
  82. {$define FPC_SYSTEM_HAS_ATOMIC_INC_32}
  83. function fpc_atomic_inc_32 (var Target: longint) : longint;
  84. {$endif VER3_2}
  85. begin
  86. {$ifdef FPC_WASM_THREADS}
  87. {$push}{$R-,Q-}
  88. Result:=fpc_wasm32_i32_atomic_rmw_add(@Target,1)+1;
  89. {$pop}
  90. {$else FPC_WASM_THREADS}
  91. inc(Target);
  92. Result:=Target;
  93. {$endif FPC_WASM_THREADS}
  94. end;
  95. {$ifdef VER3_2}
  96. function InterLockedExchange (var Target: longint;Source : longint) : longint;
  97. {$else VER3_2}
  98. {$define FPC_SYSTEM_HAS_ATOMIC_XCHG_32}
  99. function fpc_atomic_xchg_32 (var Target: longint;Source : longint) : longint;
  100. {$endif VER3_2}
  101. begin
  102. {$ifdef FPC_WASM_THREADS}
  103. Result:=LongInt(fpc_wasm32_i32_atomic_rmw_xchg(@Target,LongWord(Source)));
  104. {$else FPC_WASM_THREADS}
  105. Result:=Target;
  106. Target:=Source;
  107. {$endif FPC_WASM_THREADS}
  108. end;
  109. {$ifdef VER3_2}
  110. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint;
  111. {$else VER3_2}
  112. {$define FPC_SYSTEM_HAS_ATOMIC_CMP_XCHG_32}
  113. function fpc_atomic_cmp_xchg_32 (var Target: longint; NewValue: longint; Comparand: longint) : longint; [public,alias:'FPC_ATOMIC_CMP_XCHG_32'];
  114. {$endif VER3_2}
  115. begin
  116. {$ifdef FPC_WASM_THREADS}
  117. Result:=LongInt(fpc_wasm32_i32_atomic_rmw_cmpxchg_u(@Target,LongWord({$ifdef VER3_2}Comperand{$else}Comparand{$endif}),LongWord(NewValue)));
  118. {$else FPC_WASM_THREADS}
  119. Result:=Target;
  120. if Target={$ifdef VER3_2}Comperand{$else}Comparand{$endif} then
  121. Target:=NewValue;
  122. {$endif FPC_WASM_THREADS}
  123. end;
  124. {$ifndef VER3_2}
  125. {$define FPC_SYSTEM_HAS_ATOMIC_CMP_XCHG_64}
  126. function fpc_atomic_cmp_xchg_64 (var Target: Int64; NewValue: Int64; Comparand: Int64) : Int64; [public,alias:'FPC_ATOMIC_CMP_XCHG_64'];
  127. begin
  128. {$ifdef FPC_WASM_THREADS}
  129. Result:=Int64(fpc_wasm32_i64_atomic_rmw_cmpxchg_u(@Target,QWord(Comparand),QWord(NewValue)));
  130. {$else FPC_WASM_THREADS}
  131. Result:=Target;
  132. if Target=Comparand then
  133. Target:=NewValue;
  134. {$endif FPC_WASM_THREADS}
  135. end;
  136. {$endif VER3_2}
  137. {$ifdef VER3_2}
  138. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint;
  139. {$else VER3_2}
  140. {$define FPC_SYSTEM_HAS_ATOMIC_ADD_32}
  141. function fpc_atomic_add_32 (var Target: longint;Value : longint) : longint;
  142. {$endif VER3_2}
  143. begin
  144. {$ifdef FPC_WASM_THREADS}
  145. Result:=LongInt(fpc_wasm32_i32_atomic_rmw_add(@Target,LongWord({$ifdef VER3_2}Source{$else}Value{$endif})));
  146. {$else FPC_WASM_THREADS}
  147. Result:=Target;
  148. inc(Target,{$ifdef VER3_2}Source{$else}Value{$endif});
  149. {$endif FPC_WASM_THREADS}
  150. end;
  151. {$ifdef VER3_2}
  152. function InterLockedDecrement (var Target: smallint) : smallint;
  153. {$else VER3_2}
  154. {$define FPC_SYSTEM_HAS_ATOMIC_DEC_16}
  155. function fpc_atomic_dec_16 (var Target: smallint) : smallint;
  156. {$endif VER3_2}
  157. begin
  158. {$ifdef FPC_WASM_THREADS}
  159. {$push}{$R-,Q-}
  160. Result:=smallint(fpc_wasm32_i32_atomic_rmw16_sub_u(@Target,1)-1);
  161. {$pop}
  162. {$else FPC_WASM_THREADS}
  163. dec(Target);
  164. Result:=Target;
  165. {$endif FPC_WASM_THREADS}
  166. end;
  167. {$ifdef VER3_2}
  168. function InterLockedIncrement (var Target: smallint) : smallint;
  169. {$else VER3_2}
  170. {$define FPC_SYSTEM_HAS_ATOMIC_INC_16}
  171. function fpc_atomic_inc_16 (var Target: smallint) : smallint;
  172. {$endif VER3_2}
  173. begin
  174. {$ifdef FPC_WASM_THREADS}
  175. {$push}{$R-,Q-}
  176. Result:=smallint(fpc_wasm32_i32_atomic_rmw16_add_u(@Target,1)+1);
  177. {$pop}
  178. {$else FPC_WASM_THREADS}
  179. inc(Target);
  180. Result:=Target;
  181. {$endif FPC_WASM_THREADS}
  182. end;
  183. {$ifdef VER3_2}
  184. function InterLockedExchange (var Target: smallint;Source : smallint) : smallint;
  185. {$else VER3_2}
  186. {$define FPC_SYSTEM_HAS_ATOMIC_XCHG_16}
  187. function fpc_atomic_xchg_16 (var Target: smallint;Source : smallint) : smallint;
  188. {$endif VER3_2}
  189. begin
  190. {$ifdef FPC_WASM_THREADS}
  191. Result:=SmallInt(fpc_wasm32_i32_atomic_rmw16_xchg_u(@Target,Word(Source)));
  192. {$else FPC_WASM_THREADS}
  193. Result:=Target;
  194. Target:=Source;
  195. {$endif FPC_WASM_THREADS}
  196. end;
  197. {$ifdef VER3_2}
  198. function InterlockedCompareExchange(var Target: smallint; NewValue: smallint; Comperand: smallint): smallint;
  199. {$else VER3_2}
  200. {$define FPC_SYSTEM_HAS_ATOMIC_CMP_XCHG_16}
  201. function fpc_atomic_cmp_xchg_16 (var Target: smallint; NewValue: smallint; Comparand: smallint) : smallint;
  202. {$endif VER3_2}
  203. begin
  204. {$ifdef FPC_WASM_THREADS}
  205. Result:=SmallInt(fpc_wasm32_i32_atomic_rmw16_cmpxchg_u(@Target,Word({$ifdef VER3_2}Comperand{$else}Comparand{$endif}),Word(NewValue)));
  206. {$else FPC_WASM_THREADS}
  207. Result:=Target;
  208. if Target={$ifdef VER3_2}Comperand{$else}Comparand{$endif} then
  209. Target:=NewValue;
  210. {$endif FPC_WASM_THREADS}
  211. end;
  212. {$ifdef VER3_2}
  213. function InterLockedExchangeAdd (var Target: smallint;Source : smallint) : smallint;
  214. {$else VER3_2}
  215. {$define FPC_SYSTEM_HAS_ATOMIC_ADD_16}
  216. function fpc_atomic_add_16 (var Target: smallint;Value : smallint) : smallint;
  217. {$endif VER3_2}
  218. begin
  219. {$ifdef FPC_WASM_THREADS}
  220. Result:=SmallInt(fpc_wasm32_i32_atomic_rmw16_add_u(@Target,Word({$ifdef VER3_2}Source{$else}Value{$endif})));
  221. {$else FPC_WASM_THREADS}
  222. Result:=Target;
  223. inc(Target,{$ifdef VER3_2}Source{$else}Value{$endif});
  224. {$endif FPC_WASM_THREADS}
  225. end;
  226. {$ifndef VER3_2}
  227. {$define FPC_SYSTEM_HAS_ATOMIC_CMP_XCHG_8}
  228. function fpc_atomic_cmp_xchg_8 (var Target : shortint; NewValue : shortint; Comparand : shortint) : shortint;
  229. begin
  230. {$ifdef FPC_WASM_THREADS}
  231. Result:=ShortInt(fpc_wasm32_i32_atomic_rmw8_cmpxchg_u(@Target, Byte(Comparand), Byte(NewValue)));
  232. {$else FPC_WASM_THREADS}
  233. Result:=Target;
  234. if Target=Comparand then
  235. Target:=NewValue;
  236. {$endif FPC_WASM_THREADS}
  237. end;
  238. {$endif VER3_2}