wasm32.inc 5.0 KB

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