2
0

aasmcpu.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. first: boolean;
  81. last: boolean;
  82. constructor create(abasictype: TWasmBasicType; const aname: string = '');
  83. end;
  84. { tai_functype }
  85. tai_functype = class(tai)
  86. funcname: string;
  87. functype: TWasmFuncType;
  88. constructor create(const afuncname: string = '');
  89. destructor destroy;override;
  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. functype:=TWasmFuncType.Create;
  103. end;
  104. destructor tai_functype.destroy;
  105. begin
  106. functype.free;
  107. inherited;
  108. end;
  109. { tai_local }
  110. constructor tai_local.create(abasictype: TWasmBasicType; const aname: string);
  111. begin
  112. inherited Create;
  113. bastyp := abasictype;
  114. typ := ait_local;
  115. name := aname;
  116. end;
  117. { timpexp_ai }
  118. constructor tai_impexp.create(const aextname, aintname: ansistring;
  119. asymtype: timpexptype);
  120. begin
  121. create('', aextname, aintname, asymtype);;
  122. end;
  123. constructor tai_impexp.create(const aextmodule, aextname, aintname: ansistring; asymtype: timpexptype);
  124. begin
  125. inherited create;
  126. typ := ait_importexport;
  127. extmodule := aextmodule;
  128. extname := aextname;
  129. intname := aintname;
  130. symstype:= asymtype;
  131. end;
  132. {*****************************************************************************
  133. taicpu Constructors
  134. *****************************************************************************}
  135. constructor taicpu.op_none(op : tasmop);
  136. begin
  137. inherited create(op);
  138. end;
  139. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  140. begin
  141. inherited create(op);
  142. ops:=1;
  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_get_local,a_set_local,a_tee_local] 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(2021010201);
  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. //is_jmp:=op in [a_if_acmpeq, a_if_acmpne, a_if_icmpeq, a_if_icmpge, a_if_icmpgt,
  172. // a_if_icmple, a_if_icmplt, a_if_icmpne,
  173. // a_ifeq, a_ifge, a_ifgt, a_ifle, a_iflt, a_ifne, a_ifnonnull, a_ifnull, a_goto];
  174. loadsymbol(0,_op1,0);
  175. end;
  176. constructor taicpu.op_sym_const(op: tasmop; _op1: tasmsymbol; _op2: aint);
  177. begin
  178. inherited create(op);
  179. ops:=2;
  180. loadsymbol(0,_op1,0);
  181. loadconst(1,_op2);
  182. end;
  183. constructor taicpu.op_single(op: tasmop; _op1: single);
  184. begin
  185. inherited create(op);
  186. ops:=1;
  187. loadsingle(0,_op1);
  188. end;
  189. constructor taicpu.op_double(op: tasmop; _op1: double);
  190. begin
  191. inherited create(op);
  192. ops:=1;
  193. loaddouble(0,_op1);
  194. end;
  195. constructor taicpu.op_callindirect(const atypecode: string);
  196. begin
  197. typecode := atypecode;
  198. op_none(a_call_indirect);
  199. end;
  200. {constructor taicpu.op_string(op: tasmop; _op1len: aint; _op1: pchar);
  201. begin
  202. inherited create(op);
  203. ops:=1;
  204. loadstr(0,_op1len,_op1);
  205. end;
  206. constructor taicpu.op_wstring(op: tasmop; _op1: pcompilerwidestring);
  207. begin
  208. inherited create(op);
  209. ops:=1;
  210. loadpwstr(0,_op1);
  211. end;}
  212. procedure taicpu.loadsingle(opidx:longint;f:single);
  213. begin
  214. allocate_oper(opidx+1);
  215. with oper[opidx]^ do
  216. begin
  217. if typ<>top_single then
  218. clearop(opidx);
  219. sval:=f;
  220. typ:=top_single;
  221. end;
  222. end;
  223. procedure taicpu.loaddouble(opidx: longint; d: double);
  224. begin
  225. allocate_oper(opidx+1);
  226. with oper[opidx]^ do
  227. begin
  228. if typ<>top_double then
  229. clearop(opidx);
  230. dval:=d;
  231. typ:=top_double;
  232. end;
  233. end;
  234. {procedure taicpu.loadstr(opidx: longint; vallen: aint; pc: pchar);
  235. begin
  236. allocate_oper(opidx+1);
  237. with oper[opidx]^ do
  238. begin
  239. clearop(opidx);
  240. pcvallen:=vallen;
  241. getmem(pcval,vallen);
  242. move(pc^,pcval^,vallen);
  243. typ:=top_string;
  244. end;
  245. end;
  246. procedure taicpu.loadpwstr(opidx:longint;pwstr:pcompilerwidestring);
  247. begin
  248. allocate_oper(opidx+1);
  249. with oper[opidx]^ do
  250. begin
  251. clearop(opidx);
  252. initwidestring(pwstrval);
  253. copywidestring(pwstr,pwstrval);
  254. typ:=top_wstring;
  255. end;
  256. end;}
  257. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  258. begin
  259. result:=false;
  260. end;
  261. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  262. begin
  263. if opcode in AsmOp_Store then
  264. result:=operand_write
  265. else
  266. result:=operand_read;
  267. end;
  268. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  269. begin
  270. internalerror(2010122614);
  271. result:=nil;
  272. end;
  273. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  274. begin
  275. internalerror(2010122615);
  276. result:=nil;
  277. end;
  278. procedure InitAsm;
  279. begin
  280. end;
  281. procedure DoneAsm;
  282. begin
  283. end;
  284. initialization
  285. cai_cpu:=taicpu;
  286. cai_align:=tai_align;
  287. casmdata:=TAsmData;
  288. end.