mkarmins.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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..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. {
  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. optypes[5]:='';
  274. optypes[6]:='';
  275. codes:='';
  276. flags:='';
  277. skip:=false;
  278. { ops and optypes }
  279. i:=1;
  280. repeat
  281. hs:=readstr;
  282. if (hs='void') or (hs='ignore') then
  283. break;
  284. inc(ops);
  285. optypes[ops]:=optypes[ops]+'ot_'+formatop(hs);
  286. { if s[i]=':' then
  287. begin
  288. inc(i);
  289. optypes[ops]:=optypes[ops]+' or ot_'+formatop(readstr);
  290. end;}
  291. while s[i]='|' do
  292. begin
  293. inc(i);
  294. optypes[ops]:=optypes[ops]+' or ot_'+formatop(readstr);
  295. end;
  296. if s[i] in [',',':'] then
  297. inc(i)
  298. else
  299. break;
  300. until false;
  301. for j:=1 to 6-ops do
  302. optypes[6-j+1]:='ot_none';
  303. { codes }
  304. skipspace;
  305. j:=0;
  306. (* last:=0;*)
  307. if s[i] in ['\','0'..'9'] then
  308. begin
  309. while not(s[i] in [' ',#9]) do
  310. begin
  311. code:=readnumber;
  312. (*
  313. { for some codes we want also to change the optypes, but not
  314. if the last byte was a 1 then this byte belongs to a direct
  315. copy }
  316. if last<>1 then
  317. begin
  318. case code of
  319. 12,13,14 :
  320. optypes[code-11]:=optypes[code-11]+' or ot_signed';
  321. end;
  322. end;
  323. *)
  324. codes:=codes+'#'+tostr(code);
  325. (* last:=code;*)
  326. inc(j);
  327. end;
  328. end
  329. else
  330. begin
  331. readstr;
  332. codes:='#0';
  333. end;
  334. if j>maxinfolen then
  335. maxinfolen:=j;
  336. { flags }
  337. skipspace;
  338. while not(s[i] in [' ',#9,#13,#10]) and (i<=length(s)) do
  339. begin
  340. hs:=readstr;
  341. if hs<>'ND' then
  342. begin
  343. if flags<>'' then
  344. flags:=flags+' or ';
  345. flags:=flags+'if_'+lower(hs);
  346. end;
  347. if (s[i]=',') and (i<=length(s)) then
  348. inc(i)
  349. else
  350. break;
  351. end;
  352. { write instruction }
  353. if not skip then
  354. begin
  355. if not(first) then
  356. writeln(insfile,',')
  357. else
  358. first:=false;
  359. writeln(insfile,' (');
  360. writeln(insfile,' opcode : ',opcode,';');
  361. writeln(insfile,' ops : ',ops,';');
  362. writeln(insfile,' optypes : (',optypes[1],',',optypes[2],',',optypes[3],',',optypes[4],',',optypes[5],',',optypes[6],');');
  363. writeln(insfile,' code : ',codes,';');
  364. writeln(insfile,' flags : ',flags);
  365. write(insfile,' )');
  366. inc(insns);
  367. end;
  368. end;
  369. close(infile);
  370. closeinc(insfile);
  371. closeinc(attfile);
  372. closeinc(attsuffile);
  373. closeinc(opfile);
  374. writeln(nopfile,insns,';');
  375. close(nopfile);
  376. { closeinc(propfile); }
  377. writeln(insns,' nodes processed (maxinfolen=',maxinfolen,')');
  378. end.