mkarmins.pp 10 KB

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