nasmconv.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. program nasmconv;
  2. var
  3. infile,outfile : text;
  4. s : string;
  5. i : longint;
  6. function Replace(var s:string;const s1,s2:string):boolean;
  7. var
  8. i : longint;
  9. begin
  10. i:=pos(s1,s);
  11. if i>0 then
  12. begin
  13. Delete(s,i,length(s1));
  14. Insert(s2,s,i);
  15. Replace:=true;
  16. end
  17. else
  18. Replace:=false;
  19. end;
  20. function formatop(s:string):string;
  21. const
  22. replaces=19;
  23. replacetab : array[1..replaces,1..2] of string[32]=(
  24. (':',' or ot_colon'),
  25. ('mem8','mem or ot_bits8'),
  26. ('mem16','mem or ot_bits16'),
  27. ('mem32','mem or ot_bits32'),
  28. ('mem64','mem or ot_bits64'),
  29. ('mem80','mem or ot_bits80'),
  30. ('mem','memory'),
  31. ('memory_offs','mem_offs'),
  32. ('imm8','imm or ot_bits8'),
  33. ('imm16','imm or ot_bits16'),
  34. ('imm32','imm or ot_bits32'),
  35. ('imm64','imm or ot_bits64'),
  36. ('imm80','imm or ot_bits80'),
  37. ('imm','immediate'),
  38. ('rm8','regmem or ot_bits8'),
  39. ('rm16','regmem or ot_bits16'),
  40. ('rm32','regmem or ot_bits32'),
  41. ('rm64','regmem or ot_bits64'),
  42. ('rm80','regmem or ot_bits80')
  43. );
  44. var
  45. i : longint;
  46. begin
  47. for i:=1to replaces do
  48. replace(s,replacetab[i,1],replacetab[i,2]);
  49. formatop:=s;
  50. end;
  51. procedure maybe_newline;
  52. begin
  53. if s[i]=#10 then
  54. begin
  55. readln(infile,s);
  56. i:=1;
  57. end;
  58. while s[1]=';' do
  59. begin
  60. readln(infile,s);
  61. i:=1;
  62. end;
  63. end;
  64. function readnumber : longint;
  65. var
  66. base : longint;
  67. result : longint;
  68. begin
  69. result:=0;
  70. if s[i]='\' then
  71. begin
  72. base:=8;
  73. inc(i);
  74. if s[i]='x' then
  75. begin
  76. base:=16;
  77. inc(i);
  78. end;
  79. end
  80. else
  81. base:=10;
  82. s[i]:=upcase(s[i]);
  83. while s[i] in ['0'..'9','A'..'F'] do
  84. begin
  85. case s[i] of
  86. '0'..'9':
  87. result:=result*base+ord(s[i])-ord('0');
  88. 'A'..'F':
  89. result:=result*base+ord(s[i])-ord('A')+10;
  90. end;
  91. inc(i);
  92. end;
  93. readnumber:=result;
  94. end;
  95. function tostr(l : longint) : string;
  96. var
  97. hs : string;
  98. begin
  99. str(l,hs);
  100. tostr:=hs;
  101. end;
  102. function readstr : string;
  103. var
  104. result : string;
  105. begin
  106. result:='';
  107. while (s[i] in ['0'..'9','A'..'Z','a'..'z','_']) and (i<=length(s)) do
  108. begin
  109. result:=result+s[i];
  110. inc(i);
  111. end;
  112. readstr:=result;
  113. end;
  114. procedure skipspace;
  115. begin
  116. while (s[i] in [' ',#9]) do
  117. inc(i);
  118. end;
  119. var
  120. hs : string;
  121. j : longint;
  122. first : boolean;
  123. maxinfolen,
  124. code : byte;
  125. insns : longint;
  126. { instruction fields }
  127. last,
  128. ops : longint;
  129. opcode,
  130. codes,
  131. flags : string;
  132. optypes : array[1..3] of string;
  133. begin
  134. writeln('Nasm Instruction Table Converter Version 0.99.11');
  135. insns:=0;
  136. assign(infile,'insns.dat');
  137. reset(infile);
  138. assign(outfile,'i386tab.inc');
  139. rewrite(outfile);
  140. writeln(outfile,'(');
  141. first:=true;
  142. while not(eof(infile)) do
  143. begin
  144. { handle comment }
  145. readln(infile,s);
  146. if s[1]=';' then
  147. continue;
  148. { clear }
  149. opcode:='';
  150. ops:=0;
  151. optypes[1]:='';
  152. optypes[2]:='';
  153. optypes[3]:='';
  154. codes:='';
  155. flags:='';
  156. { opcode }
  157. opcode:='A_';
  158. i:=1;
  159. while not(s[i] in [' ',#9]) do
  160. begin
  161. opcode:=opcode+s[i];
  162. inc(i);
  163. end;
  164. skipspace;
  165. { ops and optypes }
  166. repeat
  167. hs:=readstr;
  168. if (hs='void') or (hs='ignore') then
  169. break;
  170. inc(ops);
  171. optypes[ops]:=optypes[ops]+'ot_'+formatop(hs);
  172. if s[i]=':' then
  173. begin
  174. inc(i);
  175. optypes[ops]:=optypes[ops]+' or ot_'+formatop(readstr);
  176. end;
  177. while s[i]='|' do
  178. begin
  179. inc(i);
  180. optypes[ops]:=optypes[ops]+' or ot_'+formatop(readstr);
  181. end;
  182. if s[i]=',' then
  183. inc(i)
  184. else
  185. break;
  186. until false;
  187. for j:=1 to 3-ops do
  188. optypes[3-j+1]:='ot_none';
  189. { codes }
  190. skipspace;
  191. j:=0;
  192. last:=0;
  193. if s[i] in ['\','0'..'9'] then
  194. begin
  195. while not(s[i] in [' ',#9]) do
  196. begin
  197. code:=readnumber;
  198. { for some codes we want also to change the optypes, but not
  199. if the last byte was a 1 then this byte belongs to a direct
  200. copy }
  201. if last<>1 then
  202. begin
  203. case code of
  204. 12,13,14 :
  205. optypes[code-11]:=optypes[code-11]+' or ot_signed';
  206. end;
  207. end;
  208. codes:=codes+'#'+tostr(code);
  209. last:=code;
  210. inc(j);
  211. end;
  212. end
  213. else
  214. codes:='#0';
  215. if j>maxinfolen then
  216. maxinfolen:=j;
  217. { flags }
  218. skipspace;
  219. while not(s[i] in [' ',#9,#13,#10]) and (i<=length(s)) do
  220. begin
  221. hs:=readstr;
  222. if hs='ignore' then
  223. begin
  224. flags:='0';
  225. break;
  226. end;
  227. if hs<>'ND' then
  228. begin
  229. if flags<>'' then
  230. flags:=flags+' or ';
  231. flags:=flags+'if_'+hs;
  232. end;
  233. if (s[i]=',') and (i<=length(s)) then
  234. inc(i)
  235. else
  236. break;
  237. end;
  238. { write instruction }
  239. if not(first) then
  240. writeln(outfile,',')
  241. else
  242. first:=false;
  243. writeln(outfile,' (');
  244. writeln(outfile,' opcode : ',opcode,';');
  245. writeln(outfile,' ops : ',ops,';');
  246. writeln(outfile,' optypes : (',optypes[1],',',optypes[2],',',optypes[3],');');
  247. writeln(outfile,' code : ',codes,';');
  248. writeln(outfile,' flags : ',flags);
  249. write(outfile,' )');
  250. maybe_newline;
  251. inc(insns);
  252. end;
  253. writeln(outfile);
  254. writeln(outfile,');');
  255. close(infile);
  256. close(outfile);
  257. writeln(insns,' nodes procesed (maxinfolen=',maxinfolen,')');
  258. end.