wasm32.inc 5.0 KB

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