mkarmins.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. program mkarmins;
  12. const
  13. Version = '0.9';
  14. var
  15. s : string;
  16. i : longint;
  17. x86_64 : boolean;
  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. result : longint;
  81. begin
  82. result:=0;
  83. if s[i]='\' then
  84. begin
  85. base:=8;
  86. inc(i);
  87. if s[i]='x' then
  88. begin
  89. base:=16;
  90. inc(i);
  91. end;
  92. end
  93. else
  94. base:=10;
  95. s[i]:=upcase(s[i]);
  96. while s[i] in ['0'..'9','A'..'F'] do
  97. begin
  98. case s[i] of
  99. '0'..'9':
  100. result:=result*base+ord(s[i])-ord('0');
  101. 'A'..'F':
  102. result:=result*base+ord(s[i])-ord('A')+10;
  103. end;
  104. inc(i);
  105. end;
  106. readnumber:=result;
  107. end;
  108. function tostr(l : longint) : string;
  109. var
  110. hs : string;
  111. begin
  112. str(l,hs);
  113. tostr:=hs;
  114. end;
  115. function readstr : string;
  116. var
  117. result : string;
  118. begin
  119. result:='';
  120. while (s[i] in ['0'..'9','A'..'Z','a'..'z','_']) and (i<=length(s)) do
  121. begin
  122. result:=result+s[i];
  123. inc(i);
  124. end;
  125. readstr:=result;
  126. end;
  127. procedure skipspace;
  128. begin
  129. while (s[i] in [' ',#9]) do
  130. inc(i);
  131. end;
  132. procedure openinc(var f:text;const fn:string);
  133. begin
  134. writeln('creating ',fn);
  135. assign(f,fn);
  136. rewrite(f);
  137. writeln(f,'{ don''t edit, this file is generated from armins.dat }');
  138. writeln(f,'(');
  139. end;
  140. procedure closeinc(var f:text);
  141. begin
  142. writeln(f);
  143. writeln(f,');');
  144. close(f);
  145. end;
  146. var
  147. attsuffix,
  148. hs : string;
  149. j : longint;
  150. firstopcode,
  151. first : boolean;
  152. maxinfolen,
  153. code : byte;
  154. insns : longint;
  155. attsuffile,propfile,opfile,
  156. nopfile,attfile,
  157. infile,insfile : text;
  158. { instruction fields }
  159. skip : boolean;
  160. last,
  161. ops : longint;
  162. attopcode,
  163. opcode,
  164. codes,
  165. flags : string;
  166. optypes : array[1..4] of string;
  167. begin
  168. writeln('Narm Instruction Table Converter Version ',Version);
  169. insns:=0;
  170. maxinfolen:=0;
  171. { open dat file }
  172. assign(infile,'../arm/armins.dat');
  173. { create inc files }
  174. openinc(insfile,'armtab.inc');
  175. openinc(opfile,'armop.inc');
  176. assign(nopfile,'armnop.inc');
  177. openinc(attfile,'armatt.inc');
  178. openinc(attsuffile,'armatts.inc');
  179. {
  180. openinc(intfile,'i386int.inc');
  181. openinc(propfile,'i386prop.inc');
  182. }
  183. rewrite(nopfile);
  184. writeln(nopfile,'{ don''t edit, this file is generated from armins.dat }');
  185. reset(infile);
  186. first:=true;
  187. opcode:='';
  188. firstopcode:=true;
  189. while not(eof(infile)) do
  190. begin
  191. { handle comment }
  192. readln(infile,s);
  193. while (s[1]=' ') do
  194. delete(s,1,1);
  195. if (s='') or (s[1]=';') then
  196. continue;
  197. if (s[1]='[') then
  198. begin
  199. i:=pos(',',s);
  200. j:=pos(']',s);
  201. if i=0 then
  202. begin
  203. opcode:='A_'+Copy(s,2,j-2);
  204. attopcode:=Copy(s,2,j-2);
  205. { Conditional }
  206. if (attopcode[length(attopcode)]='c') and
  207. (attopcode[length(attopcode)-1]='c') then
  208. begin
  209. dec(byte(attopcode[0]),2);
  210. dec(byte(opcode[0]),2);
  211. end;
  212. attsuffix:='attsufNONE';
  213. end
  214. else
  215. begin
  216. opcode:='A_'+Copy(s,2,i-2);
  217. { intel conditional }
  218. if (opcode[length(attopcode)]='c') and
  219. (opcode[length(attopcode)-1]='c') then
  220. dec(byte(opcode[0]),2);
  221. attopcode:=Copy(s,i+1,j-i-1);
  222. { att Suffix }
  223. case attopcode[length(attopcode)] of
  224. 'X' :
  225. begin
  226. dec(attopcode[0]);
  227. attsuffix:='attsufINT';
  228. end;
  229. 'F' :
  230. begin
  231. dec(attopcode[0]);
  232. attsuffix:='attsufFPU';
  233. end;
  234. 'R' :
  235. begin
  236. dec(attopcode[0]);
  237. attsuffix:='attsufFPUint';
  238. end;
  239. else
  240. attsuffix:='attsufNONE';
  241. end;
  242. { att Conditional }
  243. if (attopcode[length(attopcode)]='C') and
  244. (attopcode[length(attopcode)-1]='C') then
  245. dec(byte(attopcode[0]),2);
  246. end;
  247. attopcode:=Lower(attopcode);
  248. if firstopcode then
  249. firstopcode:=false
  250. else
  251. begin
  252. writeln(opfile,',');
  253. writeln(attfile,',');
  254. writeln(attsuffile,',');
  255. { writeln(propfile,','); }
  256. end;
  257. write(opfile,opcode);
  258. write(attfile,'''',attopcode,'''');
  259. write(attsuffile,attsuffix);
  260. { read the next line which contains the Change options }
  261. {
  262. repeat
  263. readln(infile,s);
  264. until eof(infile) or ((s<>'') and (s[1]<>';'));
  265. write(propfile,'(Ch: ',s,')');
  266. }
  267. continue;
  268. end;
  269. { we must have an opcode }
  270. if opcode='' then
  271. runerror(234);
  272. { clear }
  273. ops:=0;
  274. optypes[1]:='';
  275. optypes[2]:='';
  276. optypes[3]:='';
  277. optypes[4]:='';
  278. codes:='';
  279. flags:='';
  280. skip:=false;
  281. { ops and optypes }
  282. i:=1;
  283. repeat
  284. hs:=readstr;
  285. if (hs='void') or (hs='ignore') then
  286. break;
  287. inc(ops);
  288. optypes[ops]:=optypes[ops]+'ot_'+formatop(hs);
  289. { if s[i]=':' then
  290. begin
  291. inc(i);
  292. optypes[ops]:=optypes[ops]+' or ot_'+formatop(readstr);
  293. end;}
  294. while s[i]='|' do
  295. begin
  296. inc(i);
  297. optypes[ops]:=optypes[ops]+' or ot_'+formatop(readstr);
  298. end;
  299. if s[i] in [',',':'] then
  300. inc(i)
  301. else
  302. break;
  303. until false;
  304. for j:=1 to 4-ops do
  305. optypes[4-j+1]:='ot_none';
  306. { codes }
  307. skipspace;
  308. j:=0;
  309. last:=0;
  310. if s[i] in ['\','0'..'9'] then
  311. begin
  312. while not(s[i] in [' ',#9]) do
  313. begin
  314. code:=readnumber;
  315. (*
  316. { for some codes we want also to change the optypes, but not
  317. if the last byte was a 1 then this byte belongs to a direct
  318. copy }
  319. if last<>1 then
  320. begin
  321. case code of
  322. 12,13,14 :
  323. optypes[code-11]:=optypes[code-11]+' or ot_signed';
  324. end;
  325. end;
  326. *)
  327. codes:=codes+'#'+tostr(code);
  328. last:=code;
  329. inc(j);
  330. end;
  331. end
  332. else
  333. begin
  334. readstr;
  335. codes:='#0';
  336. end;
  337. if j>maxinfolen then
  338. maxinfolen:=j;
  339. { flags }
  340. skipspace;
  341. while not(s[i] in [' ',#9,#13,#10]) and (i<=length(s)) do
  342. begin
  343. hs:=readstr;
  344. if hs<>'ND' then
  345. begin
  346. if flags<>'' then
  347. flags:=flags+' or ';
  348. flags:=flags+'if_'+lower(hs);
  349. end;
  350. if (s[i]=',') and (i<=length(s)) then
  351. inc(i)
  352. else
  353. break;
  354. end;
  355. { write instruction }
  356. if not skip then
  357. begin
  358. if not(first) then
  359. writeln(insfile,',')
  360. else
  361. first:=false;
  362. writeln(insfile,' (');
  363. writeln(insfile,' opcode : ',opcode,';');
  364. writeln(insfile,' ops : ',ops,';');
  365. writeln(insfile,' optypes : (',optypes[1],',',optypes[2],',',optypes[3],',',optypes[4],');');
  366. writeln(insfile,' code : ',codes,';');
  367. writeln(insfile,' flags : ',flags);
  368. write(insfile,' )');
  369. inc(insns);
  370. end;
  371. end;
  372. close(infile);
  373. closeinc(insfile);
  374. closeinc(attfile);
  375. closeinc(attsuffile);
  376. closeinc(opfile);
  377. writeln(nopfile,insns,';');
  378. close(nopfile);
  379. { closeinc(propfile); }
  380. writeln(insns,' nodes procesed (maxinfolen=',maxinfolen,')');
  381. end.