aasmcpu.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. {
  2. Copyright (c) 1999-2002 by Jonas Maebe and Thomas Schatzl
  3. Contains the assembler object for the Risc-V 32 and Risc-V 64
  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. globtype,verbose,
  22. aasmbase,aasmtai,aasmdata,aasmsym,
  23. cpubase,cgbase,cgutils;
  24. const
  25. { "mov reg,reg" source operand number }
  26. O_MOV_SOURCE = 1;
  27. { "mov reg,reg" source operand number }
  28. O_MOV_DEST = 0;
  29. type
  30. taicpu = class(tai_cpu_abstract_sym)
  31. constructor op_none(op : tasmop);
  32. constructor op_reg(op : tasmop;_op1 : tregister);
  33. constructor op_const(op : tasmop;_op1 : aint);
  34. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  35. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  36. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  37. constructor op_const_reg(op:tasmop; _op1: aint; _op2: tregister);
  38. constructor op_const_const(op : tasmop;_op1,_op2 : aint);
  39. constructor op_reg_reg_const_const(op: tasmop; _op1, _op2: tregister; _op3, _op4: aint);
  40. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  41. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  42. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: aint);
  43. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  44. constructor op_const_reg_reg(op : tasmop;_op1 : aint;_op2, _op3 : tregister);
  45. constructor op_const_reg_const(op : tasmop;_op1 : aint;_op2 : tregister;_op3 : aint);
  46. constructor op_const_const_const(op : tasmop;_op1 : aint;_op2 : aint;_op3 : aint);
  47. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  48. constructor op_reg_bool_reg_reg(op : tasmop;_op1: tregister;_op2:boolean;_op3,_op4:tregister);
  49. constructor op_reg_bool_reg_const(op : tasmop;_op1: tregister;_op2:boolean;_op3:tregister;_op4: aint);
  50. constructor op_reg_reg_reg_const(op : tasmop; _op1, _op2, _op3 : tregister; _op4 : aint);
  51. constructor op_reg_reg_reg_const_const(op : tasmop;_op1,_op2,_op3 : tregister;_op4,_op5 : aint);
  52. constructor op_reg_reg_const_const_const(op : tasmop;_op1,_op2 : tregister;_op3,_op4,_op5 : aint);
  53. { this is for Jmp instructions }
  54. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  55. constructor op_const_const_sym(op : tasmop;_op1,_op2 : aint;_op3: tasmsymbol);
  56. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  57. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:aint);
  58. constructor op_reg_sym(op : tasmop;_op1 : tregister;_op2:tasmsymbol);
  59. constructor op_reg_const_sym(op : tasmop;_op1 : tregister;_op2:aint;_op3:tasmsymbol);
  60. constructor op_reg_reg_sym(op : tasmop;_op1, _op2 : tregister;_op3:tasmsymbol);
  61. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : aint);
  62. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:aint;const _op2 : treference);
  63. procedure loadbool(opidx:aint;_b:boolean);
  64. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  65. { register spilling code }
  66. function spilling_get_operation_type(opnr: longint): topertype;override;
  67. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;override;
  68. end;
  69. tai_align = class(tai_align_abstract)
  70. { nothing to add }
  71. end;
  72. procedure InitAsm;
  73. procedure DoneAsm;
  74. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  75. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  76. implementation
  77. uses cutils, cclasses;
  78. {*****************************************************************************
  79. taicpu Constructors
  80. *****************************************************************************}
  81. procedure taicpu.loadbool(opidx:aint;_b:boolean);
  82. begin
  83. if opidx>=ops then
  84. ops:=opidx+1;
  85. with oper[opidx]^ do
  86. begin
  87. if typ=top_ref then
  88. dispose(ref);
  89. b:=_b;
  90. typ:=top_bool;
  91. end;
  92. end;
  93. constructor taicpu.op_none(op : tasmop);
  94. begin
  95. inherited create(op);
  96. end;
  97. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  98. begin
  99. inherited create(op);
  100. ops:=1;
  101. loadreg(0,_op1);
  102. end;
  103. constructor taicpu.op_const(op : tasmop;_op1 : aint);
  104. begin
  105. inherited create(op);
  106. ops:=1;
  107. loadconst(0,_op1);
  108. end;
  109. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  110. begin
  111. inherited create(op);
  112. ops:=2;
  113. loadreg(0,_op1);
  114. loadreg(1,_op2);
  115. end;
  116. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  117. begin
  118. inherited create(op);
  119. ops:=2;
  120. loadreg(0,_op1);
  121. loadconst(1,_op2);
  122. end;
  123. constructor taicpu.op_const_reg(op:tasmop; _op1: aint; _op2: tregister);
  124. begin
  125. inherited create(op);
  126. ops:=2;
  127. loadconst(0,_op1);
  128. loadreg(1,_op2);
  129. end;
  130. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  131. begin
  132. inherited create(op);
  133. ops:=2;
  134. loadreg(0,_op1);
  135. loadref(1,_op2);
  136. end;
  137. constructor taicpu.op_const_const(op : tasmop;_op1,_op2 : aint);
  138. begin
  139. inherited create(op);
  140. ops:=2;
  141. loadconst(0,_op1);
  142. loadconst(1,_op2);
  143. end;
  144. constructor taicpu.op_reg_reg_const_const(op: tasmop; _op1, _op2: tregister; _op3, _op4: aint);
  145. begin
  146. inherited create(op);
  147. ops := 4;
  148. loadreg(0, _op1);
  149. loadreg(1, _op2);
  150. loadconst(2, _op3);
  151. loadconst(3, _op4);
  152. end;
  153. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  154. begin
  155. inherited create(op);
  156. ops:=3;
  157. loadreg(0,_op1);
  158. loadreg(1,_op2);
  159. loadreg(2,_op3);
  160. end;
  161. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  162. begin
  163. inherited create(op);
  164. ops:=3;
  165. loadreg(0,_op1);
  166. loadreg(1,_op2);
  167. loadconst(2,_op3);
  168. end;
  169. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: aint);
  170. begin
  171. inherited create(op);
  172. ops:=3;
  173. loadreg(0,_op1);
  174. loadreg(1,_op2);
  175. loadsymbol(2,_op3,_op3ofs);
  176. end;
  177. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  178. begin
  179. inherited create(op);
  180. ops:=3;
  181. loadreg(0,_op1);
  182. loadreg(1,_op2);
  183. loadref(2,_op3);
  184. end;
  185. constructor taicpu.op_const_reg_reg(op : tasmop;_op1 : aint;_op2, _op3 : tregister);
  186. begin
  187. inherited create(op);
  188. ops:=3;
  189. loadconst(0,_op1);
  190. loadreg(1,_op2);
  191. loadreg(2,_op3);
  192. end;
  193. constructor taicpu.op_const_reg_const(op : tasmop;_op1 : aint;_op2 : tregister;_op3 : aint);
  194. begin
  195. inherited create(op);
  196. ops:=3;
  197. loadconst(0,_op1);
  198. loadreg(1,_op2);
  199. loadconst(2,_op3);
  200. end;
  201. constructor taicpu.op_const_const_const(op : tasmop;_op1 : aint;_op2 : aint;_op3 : aint);
  202. begin
  203. inherited create(op);
  204. ops:=3;
  205. loadconst(0,_op1);
  206. loadconst(1,_op2);
  207. loadconst(2,_op3);
  208. end;
  209. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  210. begin
  211. inherited create(op);
  212. ops:=4;
  213. loadreg(0,_op1);
  214. loadreg(1,_op2);
  215. loadreg(2,_op3);
  216. loadreg(3,_op4);
  217. end;
  218. constructor taicpu.op_reg_bool_reg_reg(op : tasmop;_op1: tregister;_op2:boolean;_op3,_op4:tregister);
  219. begin
  220. inherited create(op);
  221. ops:=4;
  222. loadreg(0,_op1);
  223. loadbool(1,_op2);
  224. loadreg(2,_op3);
  225. loadreg(3,_op4);
  226. end;
  227. constructor taicpu.op_reg_bool_reg_const(op : tasmop;_op1: tregister;_op2:boolean;_op3:tregister;_op4: aint);
  228. begin
  229. inherited create(op);
  230. ops:=4;
  231. loadreg(0,_op1);
  232. loadbool(0,_op2);
  233. loadreg(0,_op3);
  234. loadconst(0,cardinal(_op4));
  235. end;
  236. constructor taicpu.op_reg_reg_reg_const(op : tasmop; _op1, _op2, _op3 : tregister; _op4 : aint);
  237. begin
  238. inherited create(op);
  239. ops := 4;
  240. loadreg(0, _op1);
  241. loadreg(1, _op2);
  242. loadreg(2, _op3);
  243. loadconst(3, cardinal(_op4));
  244. end;
  245. constructor taicpu.op_reg_reg_reg_const_const(op : tasmop;_op1,_op2,_op3 : tregister;_op4,_op5 : aint);
  246. begin
  247. inherited create(op);
  248. ops:=5;
  249. loadreg(0,_op1);
  250. loadreg(1,_op2);
  251. loadreg(2,_op3);
  252. loadconst(3,cardinal(_op4));
  253. loadconst(4,cardinal(_op5));
  254. end;
  255. constructor taicpu.op_reg_reg_const_const_const(op : tasmop;_op1,_op2 : tregister;_op3,_op4,_op5 : aint);
  256. begin
  257. inherited create(op);
  258. ops:=5;
  259. loadreg(0,_op1);
  260. loadreg(1,_op2);
  261. loadconst(2,_op3);
  262. loadconst(3,_op4);
  263. loadconst(4,_op5);
  264. end;
  265. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  266. begin
  267. inherited create(op);
  268. condition:=cond;
  269. ops:=1;
  270. loadsymbol(0,_op1,0);
  271. end;
  272. constructor taicpu.op_const_const_sym(op : tasmop;_op1,_op2 : aint; _op3: tasmsymbol);
  273. begin
  274. inherited create(op);
  275. ops:=3;
  276. loadconst(0,_op1);
  277. loadconst(1,_op2);
  278. loadsymbol(2,_op3,0);
  279. end;
  280. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  281. begin
  282. inherited create(op);
  283. ops:=1;
  284. loadsymbol(0,_op1,0);
  285. end;
  286. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:aint);
  287. begin
  288. inherited create(op);
  289. ops:=1;
  290. loadsymbol(0,_op1,_op1ofs);
  291. end;
  292. constructor taicpu.op_reg_sym(op: tasmop; _op1: tregister; _op2: tasmsymbol);
  293. begin
  294. inherited create(op);
  295. ops:=2;
  296. loadreg(0,_op1);
  297. loadsymbol(1,_op2,0);
  298. end;
  299. constructor taicpu.op_reg_const_sym(op: tasmop; _op1: tregister; _op2: aint; _op3: tasmsymbol);
  300. begin
  301. inherited create(op);
  302. ops:=3;
  303. loadreg(0,_op1);
  304. loadconst(1,_op2);
  305. loadsymbol(2,_op3,0);
  306. end;
  307. constructor taicpu.op_reg_reg_sym(op: tasmop; _op1, _op2: tregister; _op3: tasmsymbol);
  308. begin
  309. inherited create(op);
  310. ops:=3;
  311. loadreg(0,_op1);
  312. loadreg(1,_op2);
  313. loadsymbol(2,_op3,0);
  314. end;
  315. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : aint);
  316. begin
  317. inherited create(op);
  318. ops:=2;
  319. loadreg(0,_op1);
  320. loadsymbol(1,_op2,_op2ofs);
  321. end;
  322. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:aint;const _op2 : treference);
  323. begin
  324. inherited create(op);
  325. ops:=2;
  326. loadsymbol(0,_op1,_op1ofs);
  327. loadref(1,_op2);
  328. end;
  329. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  330. begin
  331. case regtype of
  332. R_INTREGISTER:
  333. result:=
  334. (opcode=A_MOV) and
  335. (oper[0]^.reg=oper[1]^.reg);
  336. R_FPUREGISTER:
  337. result:=
  338. (opcode=A_MOV_S) and
  339. (oper[0]^.reg=oper[1]^.reg) and
  340. (oper[0]^.reg=oper[2]^.reg);
  341. else
  342. result:=false;
  343. end;
  344. end;
  345. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  346. begin
  347. result := operand_read;
  348. case opcode of
  349. A_MOV:
  350. if opnr=0 then
  351. result:=operand_write
  352. else
  353. result:=operand_read;
  354. else
  355. ;
  356. end;
  357. end;
  358. function taicpu.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  359. begin
  360. result := operand_read;
  361. end;
  362. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  363. begin
  364. case getregtype(r) of
  365. R_INTREGISTER:
  366. result:=taicpu.op_reg_reg_const(A_L32I,r,ref.base,ref.offset);
  367. R_FPUREGISTER:
  368. result:=taicpu.op_reg_reg_const(A_LSI,r,ref.base,ref.offset);
  369. else
  370. internalerror(2020030701);
  371. end;
  372. end;
  373. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  374. begin
  375. case getregtype(r) of
  376. R_INTREGISTER:
  377. result:=taicpu.op_reg_reg_const(A_S32I,r,ref.base,ref.offset);
  378. R_FPUREGISTER:
  379. result:=taicpu.op_reg_reg_const(A_SSI,r,ref.base,ref.offset);
  380. else
  381. internalerror(2020030701);
  382. end;
  383. end;
  384. procedure InitAsm;
  385. begin
  386. end;
  387. procedure DoneAsm;
  388. begin
  389. end;
  390. begin
  391. cai_align:=tai_align;
  392. cai_cpu:=taicpu;
  393. end.