nasmconv.pp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. {
  2. $Id$
  3. Copyright (c) 1999 by Peter Vreman and Florian Klaempfl
  4. Convert insns.dat from Nasm to a .inc file for usage with
  5. the Free pascal compiler
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. program msg2inc;
  13. program nasmconv;
  14. var
  15. infile,outfile : text;
  16. s : string;
  17. i : longint;
  18. function Replace(var s:string;const s1,s2:string):boolean;
  19. var
  20. i : longint;
  21. begin
  22. i:=pos(s1,s);
  23. if i>0 then
  24. begin
  25. Delete(s,i,length(s1));
  26. Insert(s2,s,i);
  27. Replace:=true;
  28. end
  29. else
  30. Replace:=false;
  31. end;
  32. function formatop(s:string):string;
  33. const
  34. replaces=19;
  35. replacetab : array[1..replaces,1..2] of string[32]=(
  36. (':',' or ot_colon'),
  37. ('mem8','mem or ot_bits8'),
  38. ('mem16','mem or ot_bits16'),
  39. ('mem32','mem or ot_bits32'),
  40. ('mem64','mem or ot_bits64'),
  41. ('mem80','mem or ot_bits80'),
  42. ('mem','memory'),
  43. ('memory_offs','mem_offs'),
  44. ('imm8','imm or ot_bits8'),
  45. ('imm16','imm or ot_bits16'),
  46. ('imm32','imm or ot_bits32'),
  47. ('imm64','imm or ot_bits64'),
  48. ('imm80','imm or ot_bits80'),
  49. ('imm','immediate'),
  50. ('rm8','regmem or ot_bits8'),
  51. ('rm16','regmem or ot_bits16'),
  52. ('rm32','regmem or ot_bits32'),
  53. ('rm64','regmem or ot_bits64'),
  54. ('rm80','regmem or ot_bits80')
  55. );
  56. var
  57. i : longint;
  58. begin
  59. for i:=1to replaces do
  60. replace(s,replacetab[i,1],replacetab[i,2]);
  61. formatop:=s;
  62. end;
  63. procedure maybe_newline;
  64. begin
  65. if s[i]=#10 then
  66. begin
  67. readln(infile,s);
  68. i:=1;
  69. end;
  70. while s[1]=';' do
  71. begin
  72. readln(infile,s);
  73. i:=1;
  74. end;
  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. var
  132. hs : string;
  133. j : longint;
  134. first : boolean;
  135. maxinfolen,
  136. code : byte;
  137. insns : longint;
  138. { instruction fields }
  139. last,
  140. ops : longint;
  141. opcode,
  142. codes,
  143. flags : string;
  144. optypes : array[1..3] of string;
  145. begin
  146. writeln('Nasm Instruction Table Converter Version 0.99.11');
  147. insns:=0;
  148. assign(infile,'insns.dat');
  149. reset(infile);
  150. assign(outfile,'i386tab.inc');
  151. rewrite(outfile);
  152. writeln(outfile,'(');
  153. first:=true;
  154. while not(eof(infile)) do
  155. begin
  156. { handle comment }
  157. readln(infile,s);
  158. if s[1]=';' then
  159. continue;
  160. { clear }
  161. opcode:='';
  162. ops:=0;
  163. optypes[1]:='';
  164. optypes[2]:='';
  165. optypes[3]:='';
  166. codes:='';
  167. flags:='';
  168. { opcode }
  169. opcode:='A_';
  170. i:=1;
  171. while not(s[i] in [' ',#9]) do
  172. begin
  173. opcode:=opcode+s[i];
  174. inc(i);
  175. end;
  176. skipspace;
  177. { ops and optypes }
  178. repeat
  179. hs:=readstr;
  180. if (hs='void') or (hs='ignore') then
  181. break;
  182. inc(ops);
  183. optypes[ops]:=optypes[ops]+'ot_'+formatop(hs);
  184. if s[i]=':' then
  185. begin
  186. inc(i);
  187. optypes[ops]:=optypes[ops]+' or ot_'+formatop(readstr);
  188. end;
  189. while s[i]='|' do
  190. begin
  191. inc(i);
  192. optypes[ops]:=optypes[ops]+' or ot_'+formatop(readstr);
  193. end;
  194. if s[i]=',' then
  195. inc(i)
  196. else
  197. break;
  198. until false;
  199. for j:=1 to 3-ops do
  200. optypes[3-j+1]:='ot_none';
  201. { codes }
  202. skipspace;
  203. j:=0;
  204. last:=0;
  205. if s[i] in ['\','0'..'9'] then
  206. begin
  207. while not(s[i] in [' ',#9]) do
  208. begin
  209. code:=readnumber;
  210. { for some codes we want also to change the optypes, but not
  211. if the last byte was a 1 then this byte belongs to a direct
  212. copy }
  213. if last<>1 then
  214. begin
  215. case code of
  216. 12,13,14 :
  217. optypes[code-11]:=optypes[code-11]+' or ot_signed';
  218. end;
  219. end;
  220. codes:=codes+'#'+tostr(code);
  221. last:=code;
  222. inc(j);
  223. end;
  224. end
  225. else
  226. codes:='#0';
  227. if j>maxinfolen then
  228. maxinfolen:=j;
  229. { flags }
  230. skipspace;
  231. while not(s[i] in [' ',#9,#13,#10]) and (i<=length(s)) do
  232. begin
  233. hs:=readstr;
  234. if hs='ignore' then
  235. begin
  236. flags:='0';
  237. break;
  238. end;
  239. if hs<>'ND' then
  240. begin
  241. if flags<>'' then
  242. flags:=flags+' or ';
  243. flags:=flags+'if_'+hs;
  244. end;
  245. if (s[i]=',') and (i<=length(s)) then
  246. inc(i)
  247. else
  248. break;
  249. end;
  250. { write instruction }
  251. if not(first) then
  252. writeln(outfile,',')
  253. else
  254. first:=false;
  255. writeln(outfile,' (');
  256. writeln(outfile,' opcode : ',opcode,';');
  257. writeln(outfile,' ops : ',ops,';');
  258. writeln(outfile,' optypes : (',optypes[1],',',optypes[2],',',optypes[3],');');
  259. writeln(outfile,' code : ',codes,';');
  260. writeln(outfile,' flags : ',flags);
  261. write(outfile,' )');
  262. maybe_newline;
  263. inc(insns);
  264. end;
  265. writeln(outfile);
  266. writeln(outfile,');');
  267. close(infile);
  268. close(outfile);
  269. writeln(insns,' nodes procesed (maxinfolen=',maxinfolen,')');
  270. end.
  271. {
  272. $Log$
  273. Revision 1.1 1999-05-12 16:17:10 peter
  274. * init
  275. Revision 1.1 1999/05/12 16:08:27 peter
  276. + moved compiler utils
  277. }