mkarmins.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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('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. {
  175. openinc(intfile,'i386int.inc');
  176. openinc(propfile,'i386prop.inc');
  177. }
  178. rewrite(nopfile);
  179. writeln(nopfile,'{ don''t edit, this file is generated from armins.dat }');
  180. reset(infile);
  181. first:=true;
  182. opcode:='';
  183. firstopcode:=true;
  184. while not(eof(infile)) do
  185. begin
  186. { handle comment }
  187. readln(infile,s);
  188. while (s[1]=' ') do
  189. delete(s,1,1);
  190. if (s='') or (s[1]=';') then
  191. continue;
  192. if (s[1]='[') then
  193. begin
  194. i:=pos(',',s);
  195. j:=pos(']',s);
  196. if i=0 then
  197. begin
  198. opcode:='A_'+Copy(s,2,j-2);
  199. attopcode:=Copy(s,2,j-2);
  200. { Conditional }
  201. if (attopcode[length(attopcode)]='c') and
  202. (attopcode[length(attopcode)-1]='c') then
  203. begin
  204. dec(byte(attopcode[0]),2);
  205. dec(byte(opcode[0]),2);
  206. end;
  207. attsuffix:='attsufNONE';
  208. end
  209. else
  210. begin
  211. opcode:='A_'+Copy(s,2,i-2);
  212. { intel conditional }
  213. if (opcode[length(attopcode)]='c') and
  214. (opcode[length(attopcode)-1]='c') then
  215. dec(byte(opcode[0]),2);
  216. attopcode:=Copy(s,i+1,j-i-1);
  217. { att Suffix }
  218. case attopcode[length(attopcode)] of
  219. 'X' :
  220. begin
  221. dec(attopcode[0]);
  222. attsuffix:='attsufINT';
  223. end;
  224. 'F' :
  225. begin
  226. dec(attopcode[0]);
  227. attsuffix:='attsufFPU';
  228. end;
  229. 'R' :
  230. begin
  231. dec(attopcode[0]);
  232. attsuffix:='attsufFPUint';
  233. end;
  234. else
  235. attsuffix:='attsufNONE';
  236. end;
  237. { att Conditional }
  238. if (attopcode[length(attopcode)]='C') and
  239. (attopcode[length(attopcode)-1]='C') then
  240. dec(byte(attopcode[0]),2);
  241. end;
  242. attopcode:=Lower(attopcode);
  243. if firstopcode then
  244. firstopcode:=false
  245. else
  246. begin
  247. writeln(opfile,',');
  248. writeln(attfile,',');
  249. writeln(attsuffile,',');
  250. { writeln(propfile,','); }
  251. end;
  252. write(opfile,opcode);
  253. write(attfile,'''',attopcode,'''');
  254. write(attsuffile,attsuffix);
  255. { read the next line which contains the Change options }
  256. {
  257. repeat
  258. readln(infile,s);
  259. until eof(infile) or ((s<>'') and (s[1]<>';'));
  260. write(propfile,'(Ch: ',s,')');
  261. }
  262. continue;
  263. end;
  264. { we must have an opcode }
  265. if opcode='' then
  266. runerror(234);
  267. { clear }
  268. ops:=0;
  269. optypes[1]:='';
  270. optypes[2]:='';
  271. optypes[3]:='';
  272. optypes[4]:='';
  273. codes:='';
  274. flags:='';
  275. skip:=false;
  276. { ops and optypes }
  277. i:=1;
  278. repeat
  279. hs:=readstr;
  280. if (hs='void') or (hs='ignore') then
  281. break;
  282. inc(ops);
  283. optypes[ops]:=optypes[ops]+'ot_'+formatop(hs);
  284. { if s[i]=':' then
  285. begin
  286. inc(i);
  287. optypes[ops]:=optypes[ops]+' or ot_'+formatop(readstr);
  288. end;}
  289. while s[i]='|' do
  290. begin
  291. inc(i);
  292. optypes[ops]:=optypes[ops]+' or ot_'+formatop(readstr);
  293. end;
  294. if s[i] in [',',':'] then
  295. inc(i)
  296. else
  297. break;
  298. until false;
  299. for j:=1 to 4-ops do
  300. optypes[4-j+1]:='ot_none';
  301. { codes }
  302. skipspace;
  303. j:=0;
  304. (* last:=0;*)
  305. if s[i] in ['\','0'..'9'] then
  306. begin
  307. while not(s[i] in [' ',#9]) do
  308. begin
  309. code:=readnumber;
  310. (*
  311. { for some codes we want also to change the optypes, but not
  312. if the last byte was a 1 then this byte belongs to a direct
  313. copy }
  314. if last<>1 then
  315. begin
  316. case code of
  317. 12,13,14 :
  318. optypes[code-11]:=optypes[code-11]+' or ot_signed';
  319. end;
  320. end;
  321. *)
  322. codes:=codes+'#'+tostr(code);
  323. (* last:=code;*)
  324. inc(j);
  325. end;
  326. end
  327. else
  328. begin
  329. readstr;
  330. codes:='#0';
  331. end;
  332. if j>maxinfolen then
  333. maxinfolen:=j;
  334. { flags }
  335. skipspace;
  336. while not(s[i] in [' ',#9,#13,#10]) and (i<=length(s)) do
  337. begin
  338. hs:=readstr;
  339. if hs<>'ND' then
  340. begin
  341. if flags<>'' then
  342. flags:=flags+' or ';
  343. flags:=flags+'if_'+lower(hs);
  344. end;
  345. if (s[i]=',') and (i<=length(s)) then
  346. inc(i)
  347. else
  348. break;
  349. end;
  350. { write instruction }
  351. if not skip then
  352. begin
  353. if not(first) then
  354. writeln(insfile,',')
  355. else
  356. first:=false;
  357. writeln(insfile,' (');
  358. writeln(insfile,' opcode : ',opcode,';');
  359. writeln(insfile,' ops : ',ops,';');
  360. writeln(insfile,' optypes : (',optypes[1],',',optypes[2],',',optypes[3],',',optypes[4],');');
  361. writeln(insfile,' code : ',codes,';');
  362. writeln(insfile,' flags : ',flags);
  363. write(insfile,' )');
  364. inc(insns);
  365. end;
  366. end;
  367. close(infile);
  368. closeinc(insfile);
  369. closeinc(attfile);
  370. closeinc(attsuffile);
  371. closeinc(opfile);
  372. writeln(nopfile,insns,';');
  373. close(nopfile);
  374. { closeinc(propfile); }
  375. writeln(insns,' nodes processed (maxinfolen=',maxinfolen,')');
  376. end.