mkloongarch64reg.pp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. {
  2. Copyright (C) 2022 Loongson Technology Corporation Limited.
  3. Convert loongarchreg.dat to a .inc file for usage with
  4. the Free pascal compiler
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}
  12. program mkloongarchreg;
  13. const
  14. Version = '1.0';
  15. max_regcount = 512;
  16. var
  17. nr_regs : word;
  18. srcfile : text;
  19. names,abinames,stdnames : array[0..max_regcount] of string;
  20. regtypes,subtypes,values,stabs,dwarf,std_regname_index,regnumber_index : array[0..max_regcount] of word;
  21. numbers : array[0..max_regcount] of longint;
  22. procedure bug(errormsg : string);
  23. begin
  24. writeln(errormsg);
  25. close(srcfile);
  26. halt;
  27. end;
  28. function str2word(s : string) : word;
  29. var
  30. v,errcode : integer;
  31. begin
  32. val(s,v,errcode);
  33. if (errcode<>0) or (v>65535) or (v<0) then
  34. str2word:=0
  35. else
  36. str2word:=word(v);
  37. end;
  38. procedure readdatfile;
  39. var
  40. i,last,idx : longint;
  41. s,subs : string;
  42. begin
  43. assign(srcfile, '../loongarch64/loongarchreg.dat');
  44. reset(srcfile);
  45. nr_regs:=0;
  46. while not(eof(srcfile)) do
  47. begin
  48. readln(srcfile,s);
  49. if (s='') or (s[1]=';') then
  50. continue;
  51. { <name>,<type>,<subtype>,<value>,<abiname>,<stdname>,<stab idx>,<dwarf idx> }
  52. last:=length(s)+1;
  53. idx:=1;
  54. for i:=length(s) downto 0 do
  55. begin
  56. if (i=0) then
  57. begin
  58. if (idx<>8) then
  59. bug('Incomplete tables');
  60. names[nr_regs]:=copy(s,1,last-1);
  61. continue;
  62. end;
  63. if (s[i]<>',') then
  64. continue;
  65. subs:=copy(s,i+1,last-i-1);
  66. case (idx) of
  67. 1: dwarf[nr_regs]:=str2word(subs);
  68. 2: stabs[nr_regs]:=str2word(subs);
  69. 3: stdnames[nr_regs]:=subs;
  70. 4: abinames[nr_regs]:=subs;
  71. 5: values[nr_regs]:=str2word(subs);
  72. 6: subtypes[nr_regs]:=str2word(subs);
  73. 7: regtypes[nr_regs]:=str2word(subs);
  74. 8: bug('Overflow tables');
  75. end;
  76. idx:=idx+1;
  77. last:=i;
  78. end;
  79. nr_regs:=nr_regs+1;
  80. end;
  81. end;
  82. type
  83. SWAP_FUNC=procedure (i,j : longint; p : pointer);
  84. CMPR_FUNC=function (i,j : longint; p :pointer) : boolean;
  85. procedure qsort(l,r : longint; p : pointer; s : SWAP_FUNC; c : CMPR_FUNC);
  86. var
  87. i,j : longint;
  88. begin
  89. if l>=r then
  90. exit;
  91. i:=l;
  92. j:=r;
  93. while i<j do
  94. begin
  95. while (i<j) and c(i,j,p) do
  96. j:=j-1;
  97. if i<j then
  98. begin
  99. s(i,j,p);
  100. i:=i+1;
  101. end;
  102. while (i<j) and c(i,j,p) do
  103. i:=i+1;
  104. if i<j then
  105. begin
  106. s(i,j,p);
  107. j:=j-1;
  108. end;
  109. end;
  110. qsort(l,i-1,p,s,c);
  111. qsort(i+1,r,p,s,c);
  112. end;
  113. procedure swap_rni(i,j : longint; p : pointer);
  114. var
  115. t : word;
  116. begin
  117. t:=regnumber_index[i];
  118. regnumber_index[i]:=regnumber_index[j];
  119. regnumber_index[j]:=t;
  120. end;
  121. function cmpr_rn(i,j : longint; p :pointer) : boolean;
  122. begin
  123. cmpr_rn:=numbers[regnumber_index[i]]<numbers[regnumber_index[j]];
  124. end;
  125. procedure swap_sri(i,j : longint; p : pointer);
  126. var
  127. t : word;
  128. begin
  129. t:=std_regname_index[i];
  130. std_regname_index[i]:=std_regname_index[j];
  131. std_regname_index[j]:=t;
  132. end;
  133. function cmpr_sr(i,j : longint; p :pointer) : boolean;
  134. begin
  135. cmpr_sr:=stdnames[std_regname_index[i]]<stdnames[std_regname_index[j]];
  136. end;
  137. procedure build_regnum_index;
  138. var
  139. i :longint;
  140. s : SWAP_FUNC;
  141. c : CMPR_FUNC;
  142. begin
  143. for i:=0 to nr_regs-1 do
  144. regnumber_index[i]:=i;
  145. s:=@swap_rni;
  146. c:=@cmpr_rn;
  147. qsort(0,nr_regs-1,nil,s,c);
  148. end;
  149. procedure build_std_regname_index;
  150. var
  151. i : longint;
  152. s : SWAP_FUNC;
  153. c : CMPR_FUNC;
  154. begin
  155. for i:=0 to nr_regs-1 do
  156. std_regname_index[i]:=i;
  157. s:=@swap_sri;
  158. c:=@cmpr_sr;
  159. qsort(0,nr_regs-1,nil,s,c);
  160. end;
  161. procedure setarrays;
  162. var
  163. i : longint;
  164. begin
  165. for i:=0 to nr_regs-1 do
  166. numbers[i]:=(regtypes[i] shl 24) or (subtypes[i] shl 16) or values[i];
  167. build_regnum_index;
  168. build_std_regname_index;
  169. end;
  170. procedure openinc(out f:text;const fn:string);
  171. begin
  172. writeln('creating ',fn);
  173. assign(f,fn);
  174. rewrite(f);
  175. writeln(f,'{ don''t edit, this file is generated from loongarchreg.dat }');
  176. end;
  177. procedure closeinc(var f:text);
  178. begin
  179. writeln(f);
  180. close(f);
  181. end;
  182. procedure write_inc_files;
  183. var
  184. first : boolean;
  185. numfile,stdfile,stabfile,dwarffile,rnifile,srifile,supfile,norfile,confile,abinamefile : text;
  186. i : longint;
  187. begin
  188. { create inc files }
  189. openinc(confile,'../loongarch64/rloongarch64con.inc');
  190. openinc(supfile,'../loongarch64/rloongarch64sup.inc');
  191. openinc(numfile,'../loongarch64/rloongarch64num.inc');
  192. openinc(stdfile,'../loongarch64/rloongarch64std.inc');
  193. openinc(abinamefile,'../loongarch64/rloongarch64abi.inc');
  194. openinc(stabfile,'../loongarch64/rloongarch64sta.inc');
  195. openinc(dwarffile,'../loongarch64/rloongarch64dwa.inc');
  196. openinc(rnifile,'../loongarch64/rloongarch64rni.inc');
  197. openinc(srifile,'../loongarch64/rloongarch64sri.inc');
  198. first:=true;
  199. for i:=0 to nr_regs-1 do
  200. begin
  201. if not first then
  202. begin
  203. writeln(numfile,',');
  204. writeln(stdfile,',');
  205. writeln(abinamefile,',');
  206. writeln(stabfile,',');
  207. writeln(dwarffile,',');
  208. writeln(rnifile,',');
  209. writeln(srifile,',');
  210. end
  211. else
  212. first:=false;
  213. writeln(supfile,'RS_',names[i],' = ',values[i],';');
  214. writeln(confile,'NR_'+names[i],' = ','tregister(',numbers[i],')',';');
  215. write(numfile,'tregister(',numbers[i],')');
  216. write(stdfile,'''',stdnames[i],'''');
  217. write(abinamefile,'''',abinames[i],'''');
  218. write(stabfile,stabs[i]);
  219. write(dwarffile,dwarf[i]);
  220. write(rnifile,regnumber_index[i]);
  221. write(srifile,std_regname_index[i]);
  222. end;
  223. openinc(norfile,'../loongarch64/rloongarch64nor.inc');
  224. write(norfile,nr_regs);
  225. closeinc(norfile);
  226. close(confile);
  227. close(supfile);
  228. closeinc(numfile);
  229. closeinc(stdfile);
  230. closeinc(abinamefile);
  231. closeinc(stabfile);
  232. closeinc(dwarffile);
  233. closeinc(rnifile);
  234. closeinc(srifile);
  235. writeln('Done!');
  236. writeln(nr_regs,' registers processed');
  237. end;
  238. begin
  239. writeln('Register Table Converter Version ',Version);
  240. readdatfile;
  241. setarrays;
  242. write_inc_files;
  243. end.