aasmcpu.pas 8.3 KB

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