aasmcpu.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. typecode : string; // used for call_indirect
  36. constructor op_none(op : tasmop);
  37. constructor op_reg(op : tasmop;_op1 : tregister);
  38. constructor op_const(op : tasmop;_op1 : aint);
  39. constructor op_ref(op : tasmop;const _op1 : treference);
  40. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  41. constructor op_sym_const(op : tasmop;_op1 : tasmsymbol;_op2 : aint);
  42. constructor op_single(op : tasmop;_op1 : single);
  43. constructor op_double(op : tasmop;_op1 : double);
  44. constructor op_callindirect(const atypecode: string);
  45. //constructor op_string(op : tasmop;_op1len : aint;_op1 : pchar);
  46. //constructor op_wstring(op : tasmop;_op1 : pcompilerwidestring);
  47. procedure loadsingle(opidx:longint;f:single);
  48. procedure loaddouble(opidx:longint;d:double);
  49. //procedure loadstr(opidx:longint;vallen: aint;pc: pchar);
  50. //procedure loadpwstr(opidx:longint;pwstr:pcompilerwidestring);
  51. { register allocation }
  52. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  53. { register spilling code }
  54. function spilling_get_operation_type(opnr: longint): topertype;override;
  55. end;
  56. tai_align = class(tai_align_abstract)
  57. { nothing to add }
  58. end;
  59. TImpExpType= (
  60. ie_Func, // functions
  61. ie_Table, // tables (arrays of methods)
  62. ie_Memory, // memory reference
  63. ie_Global // global variables
  64. );
  65. // the actual use is defined by the assembly section used
  66. { timpexp_ai }
  67. tai_impexp = class(tai)
  68. extname : ansistring; // external name
  69. intname : ansistring; // internal name
  70. extmodule : ansistring; // external unit name
  71. symstype: TImpExpType;
  72. constructor create(const aextname, aintname: ansistring; asymtype: timpexptype); overload;
  73. constructor create(const aextmodule, aextname, aintname: ansistring; asymtype: timpexptype); overload;
  74. end;
  75. // local variable declaration
  76. { tai_local }
  77. tai_local = class(tai)
  78. bastyp: TWasmBasicType;
  79. name : string;
  80. constructor create(abasictype: TWasmBasicType; const aname: string = '');
  81. end;
  82. { tai_functype }
  83. tai_functype = class(tai)
  84. funcname: string;
  85. params: array of TWasmBasicType;
  86. results: array of TWasmBasicType;
  87. constructor create(const afuncname: string = '');
  88. procedure add_param(param: TWasmBasicType);
  89. procedure add_result(res: TWasmBasicType);
  90. end;
  91. procedure InitAsm;
  92. procedure DoneAsm;
  93. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  94. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  95. implementation
  96. { tai_functype }
  97. constructor tai_functype.create(const afuncname: string = '');
  98. begin
  99. inherited Create;
  100. typ:=ait_functype;
  101. funcname:=afuncname;
  102. end;
  103. procedure tai_functype.add_param(param: TWasmBasicType);
  104. begin
  105. SetLength(params,Length(params)+1);
  106. params[High(params)]:=param;
  107. end;
  108. procedure tai_functype.add_result(res: TWasmBasicType);
  109. begin
  110. SetLength(results,Length(results)+1);
  111. results[High(results)]:=res;
  112. end;
  113. { tai_local }
  114. constructor tai_local.create(abasictype: TWasmBasicType; const aname: string);
  115. begin
  116. inherited Create;
  117. bastyp := abasictype;
  118. typ := ait_local;
  119. name := aname;
  120. end;
  121. { timpexp_ai }
  122. constructor tai_impexp.create(const aextname, aintname: ansistring;
  123. asymtype: timpexptype);
  124. begin
  125. create('', aextname, aintname, asymtype);;
  126. end;
  127. constructor tai_impexp.create(const aextmodule, aextname, aintname: ansistring; asymtype: timpexptype);
  128. begin
  129. inherited create;
  130. typ := ait_importexport;
  131. extmodule := aextmodule;
  132. extname := aextname;
  133. intname := aintname;
  134. symstype:= asymtype;
  135. end;
  136. {*****************************************************************************
  137. taicpu Constructors
  138. *****************************************************************************}
  139. constructor taicpu.op_none(op : tasmop);
  140. begin
  141. inherited create(op);
  142. end;
  143. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  144. begin
  145. inherited create(op);
  146. ops:=1;
  147. loadreg(0,_op1);
  148. end;
  149. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  150. begin
  151. inherited create(op);
  152. ops:=1;
  153. loadref(0,_op1);
  154. end;
  155. constructor taicpu.op_const(op : tasmop;_op1 : aint);
  156. begin
  157. inherited create(op);
  158. ops:=1;
  159. loadconst(0,_op1);
  160. end;
  161. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  162. begin
  163. inherited create(op);
  164. ops:=1;
  165. //is_jmp:=op in [a_if_acmpeq, a_if_acmpne, a_if_icmpeq, a_if_icmpge, a_if_icmpgt,
  166. // a_if_icmple, a_if_icmplt, a_if_icmpne,
  167. // a_ifeq, a_ifge, a_ifgt, a_ifle, a_iflt, a_ifne, a_ifnonnull, a_ifnull, a_goto];
  168. loadsymbol(0,_op1,0);
  169. end;
  170. constructor taicpu.op_sym_const(op: tasmop; _op1: tasmsymbol; _op2: aint);
  171. begin
  172. inherited create(op);
  173. ops:=2;
  174. loadsymbol(0,_op1,0);
  175. loadconst(1,_op2);
  176. end;
  177. constructor taicpu.op_single(op: tasmop; _op1: single);
  178. begin
  179. inherited create(op);
  180. ops:=1;
  181. loadsingle(0,_op1);
  182. end;
  183. constructor taicpu.op_double(op: tasmop; _op1: double);
  184. begin
  185. inherited create(op);
  186. ops:=1;
  187. loaddouble(0,_op1);
  188. end;
  189. constructor taicpu.op_callindirect(const atypecode: string);
  190. begin
  191. typecode := atypecode;
  192. op_none(a_call_indirect);
  193. end;
  194. {constructor taicpu.op_string(op: tasmop; _op1len: aint; _op1: pchar);
  195. begin
  196. inherited create(op);
  197. ops:=1;
  198. loadstr(0,_op1len,_op1);
  199. end;
  200. constructor taicpu.op_wstring(op: tasmop; _op1: pcompilerwidestring);
  201. begin
  202. inherited create(op);
  203. ops:=1;
  204. loadpwstr(0,_op1);
  205. end;}
  206. procedure taicpu.loadsingle(opidx:longint;f:single);
  207. begin
  208. allocate_oper(opidx+1);
  209. with oper[opidx]^ do
  210. begin
  211. if typ<>top_single then
  212. clearop(opidx);
  213. sval:=f;
  214. typ:=top_single;
  215. end;
  216. end;
  217. procedure taicpu.loaddouble(opidx: longint; d: double);
  218. begin
  219. allocate_oper(opidx+1);
  220. with oper[opidx]^ do
  221. begin
  222. if typ<>top_double then
  223. clearop(opidx);
  224. dval:=d;
  225. typ:=top_double;
  226. end;
  227. end;
  228. {procedure taicpu.loadstr(opidx: longint; vallen: aint; pc: pchar);
  229. begin
  230. allocate_oper(opidx+1);
  231. with oper[opidx]^ do
  232. begin
  233. clearop(opidx);
  234. pcvallen:=vallen;
  235. getmem(pcval,vallen);
  236. move(pc^,pcval^,vallen);
  237. typ:=top_string;
  238. end;
  239. end;
  240. procedure taicpu.loadpwstr(opidx:longint;pwstr:pcompilerwidestring);
  241. begin
  242. allocate_oper(opidx+1);
  243. with oper[opidx]^ do
  244. begin
  245. clearop(opidx);
  246. initwidestring(pwstrval);
  247. copywidestring(pwstr,pwstrval);
  248. typ:=top_wstring;
  249. end;
  250. end;}
  251. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  252. begin
  253. result:=false;
  254. end;
  255. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  256. begin
  257. if opcode in AsmOp_Store then
  258. result:=operand_write
  259. else
  260. result:=operand_read;
  261. end;
  262. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  263. begin
  264. internalerror(2010122614);
  265. result:=nil;
  266. end;
  267. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  268. begin
  269. internalerror(2010122615);
  270. result:=nil;
  271. end;
  272. procedure InitAsm;
  273. begin
  274. end;
  275. procedure DoneAsm;
  276. begin
  277. end;
  278. initialization
  279. cai_cpu:=taicpu;
  280. cai_align:=tai_align;
  281. casmdata:=TAsmData;
  282. end.