mka64ins.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. {
  2. Copyright (c) 1998-2005 by Peter Vreman and Florian Klaempfl
  3. Convert i386ins.dat from Nasm 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 mkarmins;
  13. const
  14. Version = '0.9';
  15. var
  16. s : string;
  17. i : longint;
  18. function lower(const s : string) : string;
  19. {
  20. return lowercased string of s
  21. }
  22. var
  23. i : longint;
  24. begin
  25. for i:=1 to length(s) do
  26. if s[i] in ['A'..'Z'] then
  27. lower[i]:=char(byte(s[i])+32)
  28. else
  29. lower[i]:=s[i];
  30. lower[0]:=s[0];
  31. end;
  32. function Replace(var s:string;const s1,s2:string):boolean;
  33. var
  34. i : longint;
  35. begin
  36. i:=pos(s1,s);
  37. if i>0 then
  38. begin
  39. Delete(s,i,length(s1));
  40. Insert(s2,s,i);
  41. Replace:=true;
  42. end
  43. else
  44. Replace:=false;
  45. end;
  46. function formatop(s:string):string;
  47. const
  48. replaces=19;
  49. replacetab : array[1..replaces,1..2] of string[32]=(
  50. (':',' or ot_colon'),
  51. ('mem8','mem or ot_bits8'),
  52. ('mem16','mem or ot_bits16'),
  53. ('mem32','mem or ot_bits32'),
  54. ('mem64','mem or ot_bits64'),
  55. ('mem80','mem or ot_bits80'),
  56. ('mem','memory'),
  57. ('memory_offs','mem_offs'),
  58. ('imm8','imm or ot_bits8'),
  59. ('imm16','imm or ot_bits16'),
  60. ('imm32','imm or ot_bits32'),
  61. ('imm64','imm or ot_bits64'),
  62. ('imm80','imm or ot_bits80'),
  63. ('imm','immediate'),
  64. ('rm8','regmem or ot_bits8'),
  65. ('rm16','regmem or ot_bits16'),
  66. ('rm32','regmem or ot_bits32'),
  67. ('rm64','regmem or ot_bits64'),
  68. ('rm80','regmem or ot_bits80')
  69. );
  70. var
  71. i : longint;
  72. begin
  73. for i:=1to replaces do
  74. replace(s,replacetab[i,1],replacetab[i,2]);
  75. formatop:=s;
  76. end;
  77. function readnumber : longint;
  78. var
  79. base : longint;
  80. begin
  81. result:=0;
  82. if s[i]='\' then
  83. begin
  84. base:=8;
  85. inc(i);
  86. if s[i]='x' then
  87. begin
  88. base:=16;
  89. inc(i);
  90. end;
  91. end
  92. else
  93. base:=10;
  94. s[i]:=upcase(s[i]);
  95. while s[i] in ['0'..'9','A'..'F'] do
  96. begin
  97. case s[i] of
  98. '0'..'9':
  99. result:=result*base+ord(s[i])-ord('0');
  100. 'A'..'F':
  101. result:=result*base+ord(s[i])-ord('A')+10;
  102. end;
  103. inc(i);
  104. end;
  105. end;
  106. function tostr(l : longint) : string;
  107. var
  108. hs : string;
  109. begin
  110. str(l,hs);
  111. tostr:=hs;
  112. end;
  113. function readstr : string;
  114. begin
  115. result:='';
  116. while (s[i] in ['0'..'9','A'..'Z','a'..'z','_']) and (i<=length(s)) do
  117. begin
  118. result:=result+s[i];
  119. inc(i);
  120. end;
  121. end;
  122. procedure skipspace;
  123. begin
  124. while (s[i] in [' ',#9]) do
  125. inc(i);
  126. end;
  127. procedure openinc(out f:text;const fn:string);
  128. begin
  129. writeln('creating ',fn);
  130. assign(f,fn);
  131. rewrite(f);
  132. writeln(f,'{ don''t edit, this file is generated from armins.dat }');
  133. writeln(f,'(');
  134. end;
  135. procedure closeinc(var f:text);
  136. begin
  137. writeln(f);
  138. writeln(f,');');
  139. close(f);
  140. end;
  141. var
  142. attsuffix,
  143. hs : string;
  144. j : longint;
  145. firstopcode,
  146. first : boolean;
  147. maxinfolen,
  148. code : byte;
  149. insns : longint;
  150. attsuffile,{propfile,}opfile,
  151. nopfile,attfile,
  152. infile,insfile : text;
  153. { instruction fields }
  154. skip : boolean;
  155. {last,}
  156. ops : longint;
  157. attopcode,
  158. opcode,
  159. codes,
  160. flags : string;
  161. optypes : array[1..4] of string;
  162. begin
  163. writeln('FPC Instruction Table Converter Version ',Version);
  164. writeln('Based on Narm Instruction Table Converter ');
  165. insns:=0;
  166. maxinfolen:=0;
  167. { open dat file }
  168. assign(infile,'../aarch64/a64ins.dat');
  169. { create inc files }
  170. openinc(insfile,'a64tab.inc');
  171. openinc(opfile,'a64op.inc');
  172. assign(nopfile,'a64nop.inc');
  173. openinc(attfile,'a64att.inc');
  174. openinc(attsuffile,'a64atts.inc');
  175. rewrite(nopfile);
  176. writeln(nopfile,'{ don''t edit, this file is generated from a64ins.dat }');
  177. reset(infile);
  178. first:=true;
  179. opcode:='';
  180. firstopcode:=true;
  181. while not(eof(infile)) do
  182. begin
  183. { handle comment }
  184. readln(infile,s);
  185. while (s[1]=' ') do
  186. delete(s,1,1);
  187. if (s='') or (s[1]=';') then
  188. continue;
  189. if (s[1]='[') then
  190. begin
  191. i:=pos(',',s);
  192. j:=pos(']',s);
  193. if i=0 then
  194. begin
  195. opcode:='A_'+Copy(s,2,j-2);
  196. attopcode:=Copy(s,2,j-2);
  197. { Conditional }
  198. if (attopcode[length(attopcode)]='c') and
  199. (attopcode[length(attopcode)-1]='c') then
  200. begin
  201. dec(byte(attopcode[0]),2);
  202. dec(byte(opcode[0]),2);
  203. end;
  204. attsuffix:='attsufNONE';
  205. end
  206. else
  207. begin
  208. opcode:='A_'+Copy(s,2,i-2);
  209. { intel conditional }
  210. if (opcode[length(attopcode)]='c') and
  211. (opcode[length(attopcode)-1]='c') then
  212. dec(byte(opcode[0]),2);
  213. attopcode:=Copy(s,i+1,j-i-1);
  214. { att Suffix }
  215. case attopcode[length(attopcode)] of
  216. 'X' :
  217. begin
  218. dec(attopcode[0]);
  219. attsuffix:='attsufINT';
  220. end;
  221. 'F' :
  222. begin
  223. dec(attopcode[0]);
  224. attsuffix:='attsufFPU';
  225. end;
  226. 'R' :
  227. begin
  228. dec(attopcode[0]);
  229. attsuffix:='attsufFPUint';
  230. end;
  231. else
  232. attsuffix:='attsufNONE';
  233. end;
  234. { att Conditional }
  235. if (attopcode[length(attopcode)]='C') and
  236. (attopcode[length(attopcode)-1]='C') then
  237. dec(byte(attopcode[0]),2);
  238. end;
  239. attopcode:=Lower(attopcode);
  240. if firstopcode then
  241. firstopcode:=false
  242. else
  243. begin
  244. writeln(opfile,',');
  245. writeln(attfile,',');
  246. writeln(attsuffile,',');
  247. { writeln(propfile,','); }
  248. end;
  249. write(opfile,opcode);
  250. write(attfile,'''',attopcode,'''');
  251. write(attsuffile,attsuffix);
  252. { read the next line which contains the Change options }
  253. {
  254. repeat
  255. readln(infile,s);
  256. until eof(infile) or ((s<>'') and (s[1]<>';'));
  257. write(propfile,'(Ch: ',s,')');
  258. }
  259. continue;
  260. end;
  261. { we must have an opcode }
  262. if opcode='' then
  263. runerror(234);
  264. { clear }
  265. ops:=0;
  266. optypes[1]:='';
  267. optypes[2]:='';
  268. optypes[3]:='';
  269. optypes[4]:='';
  270. codes:='';
  271. flags:='';
  272. skip:=false;
  273. { ops and optypes }
  274. i:=1;
  275. repeat
  276. hs:=readstr;
  277. if (hs='void') or (hs='ignore') then
  278. break;
  279. inc(ops);
  280. optypes[ops]:=optypes[ops]+'ot_'+formatop(hs);
  281. { if s[i]=':' then
  282. begin
  283. inc(i);
  284. optypes[ops]:=optypes[ops]+' or ot_'+formatop(readstr);
  285. end;}
  286. while s[i]='|' do
  287. begin
  288. inc(i);
  289. optypes[ops]:=optypes[ops]+' or ot_'+formatop(readstr);
  290. end;
  291. if s[i] in [',',':'] then
  292. inc(i)
  293. else
  294. break;
  295. until false;
  296. for j:=1 to 4-ops do
  297. optypes[4-j+1]:='ot_none';
  298. { codes }
  299. skipspace;
  300. j:=0;
  301. (* last:=0;*)
  302. if s[i] in ['\','0'..'9'] then
  303. begin
  304. while not(s[i] in [' ',#9]) do
  305. begin
  306. code:=readnumber;
  307. (*
  308. { for some codes we want also to change the optypes, but not
  309. if the last byte was a 1 then this byte belongs to a direct
  310. copy }
  311. if last<>1 then
  312. begin
  313. case code of
  314. 12,13,14 :
  315. optypes[code-11]:=optypes[code-11]+' or ot_signed';
  316. end;
  317. end;
  318. *)
  319. codes:=codes+'#'+tostr(code);
  320. (* last:=code;*)
  321. inc(j);
  322. end;
  323. end
  324. else
  325. begin
  326. readstr;
  327. codes:='#0';
  328. end;
  329. if j>maxinfolen then
  330. maxinfolen:=j;
  331. { flags }
  332. skipspace;
  333. while not(s[i] in [' ',#9,#13,#10]) and (i<=length(s)) do
  334. begin
  335. hs:=readstr;
  336. if hs<>'ND' then
  337. begin
  338. if flags<>'' then
  339. flags:=flags+' or ';
  340. flags:=flags+'if_'+lower(hs);
  341. end;
  342. if (s[i]=',') and (i<=length(s)) then
  343. inc(i)
  344. else
  345. break;
  346. end;
  347. { write instruction }
  348. if not skip then
  349. begin
  350. if not(first) then
  351. writeln(insfile,',')
  352. else
  353. first:=false;
  354. writeln(insfile,' (');
  355. writeln(insfile,' opcode : ',opcode,';');
  356. writeln(insfile,' ops : ',ops,';');
  357. writeln(insfile,' optypes : (',optypes[1],',',optypes[2],',',optypes[3],',',optypes[4],');');
  358. writeln(insfile,' code : ',codes,';');
  359. writeln(insfile,' flags : ',flags);
  360. write(insfile,' )');
  361. inc(insns);
  362. end;
  363. end;
  364. close(infile);
  365. closeinc(insfile);
  366. closeinc(attfile);
  367. closeinc(attsuffile);
  368. closeinc(opfile);
  369. writeln(nopfile,insns,';');
  370. close(nopfile);
  371. { closeinc(propfile); }
  372. writeln(insns,' nodes processed (maxinfolen=',maxinfolen,')');
  373. end.