mkarmins.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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:=1 to 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..6] of string;
  162. begin
  163. writeln('Narm Instruction Table Converter Version ',Version);
  164. insns:=0;
  165. maxinfolen:=0;
  166. { open dat file }
  167. assign(infile,'../arm/armins.dat');
  168. { create inc files }
  169. openinc(insfile,'armtab.inc');
  170. openinc(opfile,'armop.inc');
  171. assign(nopfile,'armnop.inc');
  172. openinc(attfile,'armatt.inc');
  173. openinc(attsuffile,'armatts.inc');
  174. rewrite(nopfile);
  175. writeln(nopfile,'{ don''t edit, this file is generated from armins.dat }');
  176. reset(infile);
  177. first:=true;
  178. opcode:='';
  179. firstopcode:=true;
  180. while not(eof(infile)) do
  181. begin
  182. { handle comment }
  183. readln(infile,s);
  184. while (s[1]=' ') do
  185. delete(s,1,1);
  186. if (s='') or (s[1]=';') then
  187. continue;
  188. if (s[1]='[') then
  189. begin
  190. i:=pos(',',s);
  191. j:=pos(']',s);
  192. if i=0 then
  193. begin
  194. opcode:='A_'+Copy(s,2,j-2);
  195. attopcode:=Copy(s,2,j-2);
  196. { Conditional }
  197. if (attopcode[length(attopcode)]='c') and
  198. (attopcode[length(attopcode)-1]='c') then
  199. begin
  200. dec(byte(attopcode[0]),2);
  201. dec(byte(opcode[0]),2);
  202. end;
  203. attsuffix:='attsufNONE';
  204. end
  205. else
  206. begin
  207. opcode:='A_'+Copy(s,2,i-2);
  208. { intel conditional }
  209. if (opcode[length(attopcode)]='c') and
  210. (opcode[length(attopcode)-1]='c') then
  211. dec(byte(opcode[0]),2);
  212. attopcode:=Copy(s,i+1,j-i-1);
  213. { att Suffix }
  214. case attopcode[length(attopcode)] of
  215. 'X' :
  216. begin
  217. dec(attopcode[0]);
  218. attsuffix:='attsufINT';
  219. end;
  220. 'F' :
  221. begin
  222. dec(attopcode[0]);
  223. attsuffix:='attsufFPU';
  224. end;
  225. 'R' :
  226. begin
  227. dec(attopcode[0]);
  228. attsuffix:='attsufFPUint';
  229. end;
  230. else
  231. attsuffix:='attsufNONE';
  232. end;
  233. { att Conditional }
  234. if (attopcode[length(attopcode)]='C') and
  235. (attopcode[length(attopcode)-1]='C') then
  236. dec(byte(attopcode[0]),2);
  237. end;
  238. attopcode:=Lower(attopcode);
  239. if firstopcode then
  240. firstopcode:=false
  241. else
  242. begin
  243. writeln(opfile,',');
  244. writeln(attfile,',');
  245. writeln(attsuffile,',');
  246. end;
  247. write(opfile,opcode);
  248. write(attfile,'''',attopcode,'''');
  249. write(attsuffile,attsuffix);
  250. { read the next line which contains the Change options }
  251. {
  252. repeat
  253. readln(infile,s);
  254. until eof(infile) or ((s<>'') and (s[1]<>';'));
  255. write(propfile,'(Ch: ',s,')');
  256. }
  257. continue;
  258. end;
  259. { we must have an opcode }
  260. if opcode='' then
  261. runerror(234);
  262. { clear }
  263. ops:=0;
  264. optypes[1]:='';
  265. optypes[2]:='';
  266. optypes[3]:='';
  267. optypes[4]:='';
  268. optypes[5]:='';
  269. optypes[6]:='';
  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 6-ops do
  297. optypes[6-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],',',optypes[5],',',optypes[6],');');
  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. writeln(insns,' nodes processed (maxinfolen=',maxinfolen,')');
  372. end.