aasmcpu.pas 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. { tai_tagtype }
  88. tai_tagtype = class(tai)
  89. tagname: string;
  90. params: TWasmResultType;
  91. constructor create(const atagname: string; aparams: TWasmResultType);
  92. end;
  93. procedure InitAsm;
  94. procedure DoneAsm;
  95. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  96. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  97. implementation
  98. { tai_functype }
  99. constructor tai_functype.create(const afuncname: string; afunctype: TWasmFuncType);
  100. begin
  101. inherited Create;
  102. typ:=ait_functype;
  103. funcname:=afuncname;
  104. functype:=afunctype;
  105. end;
  106. destructor tai_functype.destroy;
  107. begin
  108. functype.free;
  109. inherited;
  110. end;
  111. { tai_tagtype }
  112. constructor tai_tagtype.create(const atagname: string; aparams: TWasmResultType);
  113. begin
  114. typ:=ait_tagtype;
  115. tagname:=atagname;
  116. params:=aparams;
  117. end;
  118. { tai_local }
  119. constructor tai_local.create(abasictype: TWasmBasicType; const aname: string);
  120. begin
  121. inherited Create;
  122. bastyp := abasictype;
  123. typ := ait_local;
  124. name := aname;
  125. end;
  126. { timpexp_ai }
  127. constructor tai_impexp.create(const aextname, aintname: ansistring;
  128. asymtype: timpexptype);
  129. begin
  130. create('', aextname, aintname, asymtype);;
  131. end;
  132. constructor tai_impexp.create(const aextmodule, aextname, aintname: ansistring; asymtype: timpexptype);
  133. begin
  134. inherited create;
  135. typ := ait_importexport;
  136. extmodule := aextmodule;
  137. extname := aextname;
  138. intname := aintname;
  139. symstype:= asymtype;
  140. end;
  141. {*****************************************************************************
  142. taicpu Constructors
  143. *****************************************************************************}
  144. constructor taicpu.op_none(op : tasmop);
  145. begin
  146. inherited create(op);
  147. end;
  148. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  149. begin
  150. inherited create(op);
  151. ops:=1;
  152. {$ifdef EXTDEBUG}
  153. if getregtype(_op1)=R_INVALIDREGISTER then
  154. InternalError(2021011901);
  155. {$endif EXTDEBUG}
  156. loadreg(0,_op1);
  157. end;
  158. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  159. begin
  160. inherited create(op);
  161. ops:=1;
  162. loadref(0,_op1);
  163. if op in [a_local_get,a_local_set,a_local_tee] then
  164. begin
  165. if (_op1.base<>NR_LOCAL_STACK_POINTER_REG) or (_op1.index<>NR_NO) then
  166. internalerror(2021010201);
  167. end
  168. else
  169. begin
  170. if (_op1.base<>NR_NO) or (_op1.index<>NR_NO) then
  171. internalerror(2021010202);
  172. end;
  173. end;
  174. constructor taicpu.op_const(op : tasmop;_op1 : aint);
  175. begin
  176. inherited create(op);
  177. ops:=1;
  178. loadconst(0,_op1);
  179. end;
  180. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  181. begin
  182. inherited create(op);
  183. ops:=1;
  184. loadsymbol(0,_op1,0);
  185. end;
  186. constructor taicpu.op_sym_const(op: tasmop; _op1: tasmsymbol; _op2: aint);
  187. begin
  188. inherited create(op);
  189. ops:=2;
  190. loadsymbol(0,_op1,0);
  191. loadconst(1,_op2);
  192. end;
  193. constructor taicpu.op_single(op: tasmop; _op1: single);
  194. begin
  195. inherited create(op);
  196. ops:=1;
  197. loadsingle(0,_op1);
  198. end;
  199. constructor taicpu.op_double(op: tasmop; _op1: double);
  200. begin
  201. inherited create(op);
  202. ops:=1;
  203. loaddouble(0,_op1);
  204. end;
  205. constructor taicpu.op_functype(op: tasmop; _op1: TWasmFuncType);
  206. begin
  207. inherited create(op);
  208. ops:=1;
  209. loadfunctype(0,_op1);
  210. end;
  211. procedure taicpu.loadfunctype(opidx: longint; ft: TWasmFuncType);
  212. begin
  213. allocate_oper(opidx+1);
  214. with oper[opidx]^ do
  215. begin
  216. if typ<>top_functype then
  217. clearop(opidx);
  218. functype:=ft;
  219. typ:=top_functype;
  220. end;
  221. end;
  222. procedure taicpu.loadsingle(opidx:longint;f:single);
  223. begin
  224. allocate_oper(opidx+1);
  225. with oper[opidx]^ do
  226. begin
  227. if typ<>top_single then
  228. clearop(opidx);
  229. sval:=f;
  230. typ:=top_single;
  231. end;
  232. end;
  233. procedure taicpu.loaddouble(opidx: longint; d: double);
  234. begin
  235. allocate_oper(opidx+1);
  236. with oper[opidx]^ do
  237. begin
  238. if typ<>top_double then
  239. clearop(opidx);
  240. dval:=d;
  241. typ:=top_double;
  242. end;
  243. end;
  244. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  245. begin
  246. result:=false;
  247. end;
  248. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  249. begin
  250. if opcode in AsmOp_Store then
  251. result:=operand_write
  252. else
  253. result:=operand_read;
  254. end;
  255. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  256. begin
  257. internalerror(2010122614);
  258. result:=nil;
  259. end;
  260. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  261. begin
  262. internalerror(2010122615);
  263. result:=nil;
  264. end;
  265. procedure InitAsm;
  266. begin
  267. end;
  268. procedure DoneAsm;
  269. begin
  270. end;
  271. initialization
  272. cai_cpu:=taicpu;
  273. cai_align:=tai_align;
  274. casmdata:=TAsmData;
  275. end.