wasm32.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. {$define FPC_SYSTEM_HAS_FPC_CPUINIT}
  17. procedure fpc_cpuinit;
  18. begin
  19. end;
  20. {$define FPC_SYSTEM_HAS_FILLCHAR}
  21. Procedure FillChar(var x;count:SizeInt;value:byte);
  22. begin
  23. if count>0 then
  24. fpc_wasm32_memory_fill(PtrUInt(@x),value,count);
  25. end;
  26. {$define FPC_SYSTEM_HAS_MOVE}
  27. procedure Move(const source;var dest;count:SizeInt);[public, alias: 'FPC_MOVE'];
  28. begin
  29. if count>0 then
  30. fpc_wasm32_memory_copy(PtrUInt(@dest),PtrUInt(@source),count);
  31. end;
  32. {$define FPC_SYSTEM_HAS_GET_PC_ADDR}
  33. Function Get_pc_addr : CodePointer;
  34. begin
  35. { dummy, produces a small, fake backtrace, otherwise programs terminate
  36. with no output at all, in case of a runtime error }
  37. result:=CodePointer($eeeeeeef);
  38. end;
  39. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  40. function get_caller_addr(framebp:pointer;addr:codepointer=nil):pointer;
  41. begin
  42. { dummy, produces a small, fake backtrace, otherwise programs terminate
  43. with no output at all, in case of a runtime error }
  44. if addr=CodePointer($eeeeeeef) then
  45. result:=CodePointer($eeeeeeee)
  46. else
  47. result:=nil;
  48. end;
  49. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  50. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;
  51. begin
  52. result:=nil;
  53. end;
  54. {$define FPC_SYSTEM_HAS_SPTR}
  55. function Sptr : pointer; assembler; nostackframe;
  56. asm
  57. global.get $__stack_pointer
  58. end;
  59. function InterLockedDecrement (var Target: longint) : longint;
  60. begin
  61. {$ifdef FPC_WASM_THREADS}
  62. {$push}{$R-,Q-}
  63. Result:=fpc_wasm32_i32_atomic_rmw_sub(@Target,1)-1;
  64. {$pop}
  65. {$else FPC_WASM_THREADS}
  66. dec(Target);
  67. Result:=Target;
  68. {$endif FPC_WASM_THREADS}
  69. end;
  70. function InterLockedIncrement (var Target: longint) : longint;
  71. begin
  72. {$ifdef FPC_WASM_THREADS}
  73. {$push}{$R-,Q-}
  74. Result:=fpc_wasm32_i32_atomic_rmw_add(@Target,1)+1;
  75. {$pop}
  76. {$else FPC_WASM_THREADS}
  77. inc(Target);
  78. Result:=Target;
  79. {$endif FPC_WASM_THREADS}
  80. end;
  81. function InterLockedExchange (var Target: longint;Source : longint) : longint;
  82. begin
  83. {$ifdef FPC_WASM_THREADS}
  84. Result:=LongInt(fpc_wasm32_i32_atomic_rmw_xchg(@Target,LongWord(Source)));
  85. {$else FPC_WASM_THREADS}
  86. Result:=Target;
  87. Target:=Source;
  88. {$endif FPC_WASM_THREADS}
  89. end;
  90. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint;
  91. begin
  92. {$ifdef FPC_WASM_THREADS}
  93. Result:=LongInt(fpc_wasm32_i32_atomic_rmw_cmpxchg_u(@Target,LongWord(Comperand),LongWord(NewValue)));
  94. {$else FPC_WASM_THREADS}
  95. Result:=Target;
  96. if Target=Comperand then
  97. Target:=NewValue;
  98. {$endif FPC_WASM_THREADS}
  99. end;
  100. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint;
  101. begin
  102. {$ifdef FPC_WASM_THREADS}
  103. Result:=LongInt(fpc_wasm32_i32_atomic_rmw_add(@Target,LongWord(Source)));
  104. {$else FPC_WASM_THREADS}
  105. Result:=Target;
  106. inc(Target,Source);
  107. {$endif FPC_WASM_THREADS}
  108. end;
  109. function InterLockedDecrement (var Target: smallint) : smallint;
  110. begin
  111. {$ifdef FPC_WASM_THREADS}
  112. {$push}{$R-,Q-}
  113. Result:=smallint(fpc_wasm32_i32_atomic_rmw16_sub_u(@Target,1)-1);
  114. {$pop}
  115. {$else FPC_WASM_THREADS}
  116. dec(Target);
  117. Result:=Target;
  118. {$endif FPC_WASM_THREADS}
  119. end;
  120. function InterLockedIncrement (var Target: smallint) : smallint;
  121. begin
  122. {$ifdef FPC_WASM_THREADS}
  123. {$push}{$R-,Q-}
  124. Result:=smallint(fpc_wasm32_i32_atomic_rmw16_add_u(@Target,1)+1);
  125. {$pop}
  126. {$else FPC_WASM_THREADS}
  127. inc(Target);
  128. Result:=Target;
  129. {$endif FPC_WASM_THREADS}
  130. end;
  131. function InterLockedExchange (var Target: smallint;Source : smallint) : smallint;
  132. begin
  133. {$ifdef FPC_WASM_THREADS}
  134. Result:=SmallInt(fpc_wasm32_i32_atomic_rmw16_xchg_u(@Target,Word(Source)));
  135. {$else FPC_WASM_THREADS}
  136. Result:=Target;
  137. Target:=Source;
  138. {$endif FPC_WASM_THREADS}
  139. end;
  140. function InterlockedCompareExchange(var Target: smallint; NewValue: smallint; Comperand: smallint): smallint;
  141. begin
  142. {$ifdef FPC_WASM_THREADS}
  143. Result:=SmallInt(fpc_wasm32_i32_atomic_rmw16_cmpxchg_u(@Target,Word(Comperand),Word(NewValue)));
  144. {$else FPC_WASM_THREADS}
  145. Result:=Target;
  146. if Target=Comperand then
  147. Target:=NewValue;
  148. {$endif FPC_WASM_THREADS}
  149. end;
  150. function InterLockedExchangeAdd (var Target: smallint;Source : smallint) : smallint;
  151. begin
  152. {$ifdef FPC_WASM_THREADS}
  153. Result:=SmallInt(fpc_wasm32_i32_atomic_rmw16_add_u(@Target,Word(Source)));
  154. {$else FPC_WASM_THREADS}
  155. Result:=Target;
  156. inc(Target,Source);
  157. {$endif FPC_WASM_THREADS}
  158. end;