aasmcpu.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. {
  2. Copyright (c) 2019 by Free Pascal and Lazarus foundation
  3. Contains the assembler object for the WebAssembly
  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 aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,verbose,
  23. aasmbase,aasmtai,aasmdata,aasmsym,
  24. cgbase,cgutils,cpubase,cpuinfo,
  25. widestr;
  26. { fake, there are no "mov reg,reg" instructions here }
  27. const
  28. { "mov reg,reg" source operand number }
  29. O_MOV_SOURCE = 0;
  30. { "mov reg,reg" source operand number }
  31. O_MOV_DEST = 0;
  32. type
  33. { taicpu }
  34. taicpu = class(tai_cpu_abstract_sym)
  35. constructor op_none(op : tasmop);
  36. constructor op_reg(op : tasmop;_op1 : tregister);
  37. constructor op_const(op : tasmop;_op1 : aint);
  38. constructor op_ref(op : tasmop;const _op1 : treference);
  39. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  40. constructor op_sym_const(op : tasmop;_op1 : tasmsymbol;_op2 : aint);
  41. constructor op_single(op : tasmop;_op1 : single);
  42. constructor op_double(op : tasmop;_op1 : double);
  43. constructor op_functype(op : tasmop; _op1: TWasmFuncType);
  44. procedure loadfunctype(opidx:longint;ft:TWasmFuncType);
  45. procedure loadsingle(opidx:longint;f:single);
  46. procedure loaddouble(opidx:longint;d:double);
  47. { register allocation }
  48. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  49. { register spilling code }
  50. function spilling_get_operation_type(opnr: longint): topertype;override;
  51. end;
  52. tai_align = class(tai_align_abstract)
  53. { nothing to add }
  54. end;
  55. TImpExpType= (
  56. ie_Func, // functions
  57. ie_Table, // tables (arrays of methods)
  58. ie_Memory, // memory reference
  59. ie_Global // global variables
  60. );
  61. // the actual use is defined by the assembly section used
  62. { timpexp_ai }
  63. tai_impexp = class(tai)
  64. extname : ansistring; // external name
  65. intname : ansistring; // internal name
  66. extmodule : ansistring; // external unit name
  67. symstype: TImpExpType;
  68. constructor create(const aextname, aintname: ansistring; asymtype: timpexptype); overload;
  69. constructor create(const aextmodule, aextname, aintname: ansistring; asymtype: timpexptype); overload;
  70. end;
  71. // local variable declaration
  72. { tai_local }
  73. tai_local = class(tai)
  74. bastyp: TWasmBasicType;
  75. name : string;
  76. first: boolean;
  77. last: boolean;
  78. constructor create(abasictype: TWasmBasicType; const aname: string = '');
  79. end;
  80. { tai_functype }
  81. tai_functype = class(tai)
  82. funcname: string;
  83. functype: TWasmFuncType;
  84. constructor create(const afuncname: string; afunctype: TWasmFuncType);
  85. destructor destroy;override;
  86. end;
  87. procedure InitAsm;
  88. procedure DoneAsm;
  89. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  90. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  91. implementation
  92. { tai_functype }
  93. constructor tai_functype.create(const afuncname: string; afunctype: TWasmFuncType);
  94. begin
  95. inherited Create;
  96. typ:=ait_functype;
  97. funcname:=afuncname;
  98. functype:=afunctype;
  99. end;
  100. destructor tai_functype.destroy;
  101. begin
  102. functype.free;
  103. inherited;
  104. end;
  105. { tai_local }
  106. constructor tai_local.create(abasictype: TWasmBasicType; const aname: string);
  107. begin
  108. inherited Create;
  109. bastyp := abasictype;
  110. typ := ait_local;
  111. name := aname;
  112. end;
  113. { timpexp_ai }
  114. constructor tai_impexp.create(const aextname, aintname: ansistring;
  115. asymtype: timpexptype);
  116. begin
  117. create('', aextname, aintname, asymtype);;
  118. end;
  119. constructor tai_impexp.create(const aextmodule, aextname, aintname: ansistring; asymtype: timpexptype);
  120. begin
  121. inherited create;
  122. typ := ait_importexport;
  123. extmodule := aextmodule;
  124. extname := aextname;
  125. intname := aintname;
  126. symstype:= asymtype;
  127. end;
  128. {*****************************************************************************
  129. taicpu Constructors
  130. *****************************************************************************}
  131. constructor taicpu.op_none(op : tasmop);
  132. begin
  133. inherited create(op);
  134. end;
  135. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  136. begin
  137. inherited create(op);
  138. ops:=1;
  139. {$ifdef EXTDEBUG}
  140. if getregtype(_op1)=R_INVALIDREGISTER then
  141. InternalError(2021011901);
  142. {$endif EXTDEBUG}
  143. loadreg(0,_op1);
  144. end;
  145. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  146. begin
  147. inherited create(op);
  148. ops:=1;
  149. loadref(0,_op1);
  150. if op in [a_local_get,a_local_set,a_local_tee] then
  151. begin
  152. if (_op1.base<>NR_LOCAL_STACK_POINTER_REG) or (_op1.index<>NR_NO) then
  153. internalerror(2021010201);
  154. end
  155. else
  156. begin
  157. if (_op1.base<>NR_NO) or (_op1.index<>NR_NO) then
  158. internalerror(2021010202);
  159. end;
  160. end;
  161. constructor taicpu.op_const(op : tasmop;_op1 : aint);
  162. begin
  163. inherited create(op);
  164. ops:=1;
  165. loadconst(0,_op1);
  166. end;
  167. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  168. begin
  169. inherited create(op);
  170. ops:=1;
  171. loadsymbol(0,_op1,0);
  172. end;
  173. constructor taicpu.op_sym_const(op: tasmop; _op1: tasmsymbol; _op2: aint);
  174. begin
  175. inherited create(op);
  176. ops:=2;
  177. loadsymbol(0,_op1,0);
  178. loadconst(1,_op2);
  179. end;
  180. constructor taicpu.op_single(op: tasmop; _op1: single);
  181. begin
  182. inherited create(op);
  183. ops:=1;
  184. loadsingle(0,_op1);
  185. end;
  186. constructor taicpu.op_double(op: tasmop; _op1: double);
  187. begin
  188. inherited create(op);
  189. ops:=1;
  190. loaddouble(0,_op1);
  191. end;
  192. constructor taicpu.op_functype(op: tasmop; _op1: TWasmFuncType);
  193. begin
  194. inherited create(op);
  195. ops:=1;
  196. loadfunctype(0,_op1);
  197. end;
  198. procedure taicpu.loadfunctype(opidx: longint; ft: TWasmFuncType);
  199. begin
  200. allocate_oper(opidx+1);
  201. with oper[opidx]^ do
  202. begin
  203. if typ<>top_functype then
  204. clearop(opidx);
  205. functype:=ft;
  206. typ:=top_functype;
  207. end;
  208. end;
  209. procedure taicpu.loadsingle(opidx:longint;f:single);
  210. begin
  211. allocate_oper(opidx+1);
  212. with oper[opidx]^ do
  213. begin
  214. if typ<>top_single then
  215. clearop(opidx);
  216. sval:=f;
  217. typ:=top_single;
  218. end;
  219. end;
  220. procedure taicpu.loaddouble(opidx: longint; d: double);
  221. begin
  222. allocate_oper(opidx+1);
  223. with oper[opidx]^ do
  224. begin
  225. if typ<>top_double then
  226. clearop(opidx);
  227. dval:=d;
  228. typ:=top_double;
  229. end;
  230. end;
  231. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  232. begin
  233. result:=false;
  234. end;
  235. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  236. begin
  237. if opcode in AsmOp_Store then
  238. result:=operand_write
  239. else
  240. result:=operand_read;
  241. end;
  242. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  243. begin
  244. internalerror(2010122614);
  245. result:=nil;
  246. end;
  247. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  248. begin
  249. internalerror(2010122615);
  250. result:=nil;
  251. end;
  252. procedure InitAsm;
  253. begin
  254. end;
  255. procedure DoneAsm;
  256. begin
  257. end;
  258. initialization
  259. cai_cpu:=taicpu;
  260. cai_align:=tai_align;
  261. casmdata:=TAsmData;
  262. end.