ra386att.pas 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  1. {
  2. $Id$
  3. Copyright (c) 1997-99 by Carl Eric Codere and Peter Vreman
  4. Does the parsing for the AT&T styled inline assembler.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$ifdef TP}
  19. {$N+,E+}
  20. {$endif TP}
  21. Unit Ra386att;
  22. Interface
  23. uses
  24. tree;
  25. function assemble: ptree;
  26. Implementation
  27. Uses
  28. globtype,
  29. strings,cobjects,systems,verbose,globals,
  30. files,aasm,types,symtable,scanner,hcodegen
  31. ,i386base
  32. ,rautils,ra386;
  33. type
  34. tasmtoken = (
  35. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  36. AS_REALNUM,AS_COMMA,AS_LPAREN,
  37. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  38. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,AS_DOLLAR,
  39. {------------------ Assembler directives --------------------}
  40. AS_DB,AS_DW,AS_DD,AS_DQ,AS_GLOBAL,AS_ALIGN,AS_ASCII,
  41. AS_ASCIIZ,AS_LCOMM,AS_COMM,AS_SINGLE,AS_DOUBLE,AS_EXTENDED,
  42. AS_DATA,AS_TEXT,AS_END,
  43. {------------------ Assembler Operators --------------------}
  44. AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR,AS_NOR);
  45. tasmkeyword = string[8];
  46. const
  47. { These tokens should be modified accordingly to the modifications }
  48. { in the different enumerations. }
  49. firstdirective = AS_DB;
  50. lastdirective = AS_END;
  51. _count_asmprefixes = 5;
  52. _count_asmspecialops = 25;
  53. _count_asmoverrides = 3;
  54. token2str : array[tasmtoken] of string[10]=(
  55. '','Label','LLabel','string','integer',
  56. 'float',',','(',
  57. ')',':','.','+','-','*',
  58. ';','identifier','register','opcode','/','$',
  59. '.byte','.word','.long','.quad','.globl','.align','.ascii',
  60. '.asciz','.lcomm','.comm','.single','.double','.tfloat',
  61. '.data','.text','END',
  62. '%','<<','>>','!','&','|','^','~');
  63. const
  64. newline = #10;
  65. firsttoken : boolean = TRUE;
  66. charcount : byte = 0;
  67. var
  68. _asmsorted,
  69. inexpression : boolean;
  70. curlist : paasmoutput;
  71. c : char;
  72. actasmtoken : tasmtoken;
  73. prevasmtoken : tasmtoken;
  74. actasmpattern : string;
  75. actopcode : tasmop;
  76. actasmregister : tregister;
  77. actopsize : topsize;
  78. actcondition : tasmcond;
  79. iasmops : ^op2strtable;
  80. iasmregs : ^reg2strtable;
  81. Procedure SetupTables;
  82. { creates uppercased symbol tables for speed access }
  83. var
  84. i : tasmop;
  85. j : tregister;
  86. Begin
  87. { opcodes }
  88. new(iasmops);
  89. for i:=firstop to lastop do
  90. iasmops^[i] := upper(att_op2str[i]);
  91. { registers }
  92. new(iasmregs);
  93. for j:=firstreg to lastreg do
  94. iasmregs^[j] := upper(att_reg2str[j]);
  95. end;
  96. {---------------------------------------------------------------------}
  97. { Routines for the tokenizing }
  98. {---------------------------------------------------------------------}
  99. function is_asmopcode(const s: string):boolean;
  100. const
  101. att_sizesuffixstr : array[0..8] of string[2] = (
  102. '','B','W','L','BW','BL','WL','Q','T'
  103. );
  104. att_sizesuffix : array[0..8] of topsize = (
  105. S_NO,S_B,S_W,S_L,S_BW,S_BL,S_WL,S_IQ,S_FX
  106. );
  107. var
  108. i : tasmop;
  109. cond : string[4];
  110. cnd : tasmcond;
  111. len,
  112. j,
  113. sufidx : longint;
  114. hid : string;
  115. Begin
  116. is_asmopcode:=FALSE;
  117. actopcode:=A_None;
  118. actcondition:=C_None;
  119. actopsize:=S_NO;
  120. { search for all possible suffixes }
  121. for sufidx:=low(att_sizesuffixstr) to high(att_sizesuffixstr) do
  122. begin
  123. len:=length(s)-length(att_sizesuffixstr[sufidx]);
  124. if copy(s,len+1,length(att_sizesuffixstr[sufidx]))=att_sizesuffixstr[sufidx] then
  125. begin
  126. { here we search the entire table... }
  127. hid:=copy(s,1,len);
  128. for i:=firstop to lastop do
  129. if (length(hid) > 0) and (hid=iasmops^[i]) then
  130. begin
  131. actopsize:=att_sizesuffix[sufidx];
  132. actopcode:=i;
  133. actasmtoken:=AS_OPCODE;
  134. is_asmopcode:=TRUE;
  135. exit;
  136. end;
  137. { not found, check condition opcodes }
  138. j:=0;
  139. while (j<CondAsmOps) do
  140. begin
  141. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  142. begin
  143. cond:=Copy(s,Length(CondAsmOpStr[j])+1,len-Length(CondAsmOpStr[j]));
  144. if cond<>'' then
  145. begin
  146. for cnd:=low(TasmCond) to high(TasmCond) do
  147. if Cond=Upper(cond2str[cnd]) then
  148. begin
  149. actopcode:=CondASmOp[j];
  150. actopsize:=att_sizesuffix[sufidx];
  151. actcondition:=cnd;
  152. actasmtoken:=AS_OPCODE;
  153. is_asmopcode:=TRUE;
  154. exit;
  155. end;
  156. end;
  157. end;
  158. inc(j);
  159. end;
  160. end;
  161. end;
  162. end;
  163. Function is_asmdirective(const s: string):boolean;
  164. var
  165. i : tasmtoken;
  166. hs : string;
  167. Begin
  168. { GNU as is also not casesensitive with this }
  169. hs:=lower(s);
  170. for i:=firstdirective to lastdirective do
  171. if hs=token2str[i] then
  172. begin
  173. actasmtoken:=i;
  174. is_asmdirective:=true;
  175. exit;
  176. end;
  177. is_asmdirective:=false;
  178. end;
  179. Function is_register(const s: string):boolean;
  180. Var
  181. i : tregister;
  182. Begin
  183. actasmregister:=R_NO;
  184. for i:=firstreg to lastreg do
  185. if s=iasmregs^[i] then
  186. begin
  187. actasmtoken:=AS_REGISTER;
  188. actasmregister:=i;
  189. is_register:=true;
  190. exit;
  191. end;
  192. is_register:=false;
  193. end;
  194. Function is_locallabel(const s: string):boolean;
  195. begin
  196. is_locallabel:=(length(s)>=2) and (s[1]='.') and (s[2]='L');
  197. end;
  198. Procedure GetToken;
  199. var
  200. forcelabel: boolean;
  201. errorflag : boolean;
  202. len : longint;
  203. begin
  204. { save old token and reset new token }
  205. prevasmtoken:=actasmtoken;
  206. actasmtoken:=AS_NONE;
  207. { reset }
  208. errorflag:=FALSE;
  209. forcelabel:=FALSE;
  210. actasmpattern:='';
  211. { while space and tab , continue scan... }
  212. while c in [' ',#9] do
  213. c:=current_scanner^.asmgetchar;
  214. { get token pos }
  215. if not (c in [newline,#13,'{',';']) then
  216. current_scanner^.gettokenpos;
  217. { Local Label, Label, Directive, Prefix or Opcode }
  218. if firsttoken and not(c in [newline,#13,'{',';']) then
  219. begin
  220. firsttoken:=FALSE;
  221. len:=0;
  222. { directive or local label }
  223. if c = '.' then
  224. begin
  225. inc(len);
  226. actasmpattern[len]:=c;
  227. { Let us point to the next character }
  228. c:=current_scanner^.asmgetchar;
  229. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  230. begin
  231. inc(len);
  232. actasmpattern[len]:=c;
  233. c:=current_scanner^.asmgetchar;
  234. end;
  235. actasmpattern[0]:=chr(len);
  236. { this is a local label... }
  237. if (c=':') and is_locallabel(actasmpattern) then
  238. Begin
  239. { local variables are case sensitive }
  240. actasmtoken:=AS_LLABEL;
  241. c:=current_scanner^.asmgetchar;
  242. exit;
  243. end
  244. { must be a directive }
  245. else
  246. Begin
  247. { directives are case sensitive!! }
  248. if is_asmdirective(actasmpattern) then
  249. exit;
  250. Message1(asmr_e_not_directive_or_local_symbol,actasmpattern);
  251. end;
  252. end;
  253. { only opcodes and global labels are allowed now. }
  254. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  255. begin
  256. inc(len);
  257. actasmpattern[len]:=c;
  258. c:=current_scanner^.asmgetchar;
  259. end;
  260. actasmpattern[0]:=chr(len);
  261. { Label ? }
  262. if c = ':' then
  263. begin
  264. actasmtoken:=AS_LABEL;
  265. { let us point to the next character }
  266. c:=current_scanner^.asmgetchar;
  267. exit;
  268. end;
  269. { Opcode ? }
  270. If is_asmopcode(upper(actasmpattern)) then
  271. Begin
  272. uppervar(actasmpattern);
  273. exit;
  274. end;
  275. { End of assemblerblock ? }
  276. if upper(actasmpattern) = 'END' then
  277. begin
  278. actasmtoken:=AS_END;
  279. exit;
  280. end;
  281. actasmtoken:=AS_NONE;
  282. end
  283. else { else firsttoken }
  284. { Here we must handle all possible cases }
  285. begin
  286. case c of
  287. '.' : { possiblities : - local label reference , such as in jmp @local1 }
  288. { - field of object/record }
  289. { - directive. }
  290. begin
  291. if (prevasmtoken=AS_ID) then
  292. begin
  293. c:=current_scanner^.asmgetchar;
  294. actasmtoken:=AS_DOT;
  295. exit;
  296. end;
  297. actasmpattern:=c;
  298. c:=current_scanner^.asmgetchar;
  299. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  300. begin
  301. actasmpattern:=actasmpattern + c;
  302. c:=current_scanner^.asmgetchar;
  303. end;
  304. if is_asmdirective(actasmpattern) then
  305. exit;
  306. { local label references and directives }
  307. { are case sensitive }
  308. actasmtoken:=AS_ID;
  309. exit;
  310. end;
  311. { identifier, register, prefix or directive }
  312. '_','A'..'Z','a'..'z':
  313. begin
  314. len:=0;
  315. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  316. begin
  317. inc(len);
  318. actasmpattern[len]:=c;
  319. c:=current_scanner^.asmgetchar;
  320. end;
  321. actasmpattern[0]:=chr(len);
  322. uppervar(actasmpattern);
  323. { Opcode, can only be when the previous was a prefix }
  324. If is_prefix(actopcode) and is_asmopcode(upper(actasmpattern)) then
  325. Begin
  326. uppervar(actasmpattern);
  327. exit;
  328. end;
  329. { check for end which is a reserved word unlike the opcodes }
  330. if actasmpattern = 'END' then
  331. Begin
  332. actasmtoken:=AS_END;
  333. exit;
  334. end;
  335. actasmtoken:=AS_ID;
  336. exit;
  337. end;
  338. '%' : { register or modulo }
  339. begin
  340. len:=1;
  341. actasmpattern[len]:='%';
  342. c:=current_scanner^.asmgetchar;
  343. while c in ['a'..'z','A'..'Z','0'..'9'] do
  344. Begin
  345. inc(len);
  346. actasmpattern[len]:=c;
  347. c:=current_scanner^.asmgetchar;
  348. end;
  349. actasmpattern[0]:=chr(len);
  350. uppervar(actasmpattern);
  351. if (actasmpattern = '%ST') and (c='(') then
  352. Begin
  353. actasmpattern:=actasmpattern+c;
  354. c:=current_scanner^.asmgetchar;
  355. if c in ['0'..'9'] then
  356. actasmpattern:=actasmpattern + c
  357. else
  358. Message(asmr_e_invalid_fpu_register);
  359. c:=current_scanner^.asmgetchar;
  360. if c <> ')' then
  361. Message(asmr_e_invalid_fpu_register)
  362. else
  363. Begin
  364. actasmpattern:=actasmpattern + c;
  365. c:=current_scanner^.asmgetchar; { let us point to next character. }
  366. end;
  367. end;
  368. if is_register(actasmpattern) then
  369. exit;
  370. Message(asmr_w_modulo_not_supported);
  371. end;
  372. '1'..'9': { integer number }
  373. begin
  374. len:=0;
  375. while c in ['0'..'9'] do
  376. Begin
  377. inc(len);
  378. actasmpattern[len]:=c;
  379. c:=current_scanner^.asmgetchar;
  380. end;
  381. actasmpattern[0]:=chr(len);
  382. actasmpattern:=tostr(ValDecimal(actasmpattern));
  383. actasmtoken:=AS_INTNUM;
  384. exit;
  385. end;
  386. '0' : { octal,hexa,real or binary number. }
  387. begin
  388. actasmpattern:=c;
  389. c:=current_scanner^.asmgetchar;
  390. case upcase(c) of
  391. 'B': { binary }
  392. Begin
  393. c:=current_scanner^.asmgetchar;
  394. while c in ['0','1'] do
  395. Begin
  396. actasmpattern:=actasmpattern + c;
  397. c:=current_scanner^.asmgetchar;
  398. end;
  399. actasmpattern:=tostr(ValBinary(actasmpattern));
  400. actasmtoken:=AS_INTNUM;
  401. exit;
  402. end;
  403. 'D': { real }
  404. Begin
  405. c:=current_scanner^.asmgetchar;
  406. { get ridd of the 0d }
  407. if (c in ['+','-']) then
  408. begin
  409. actasmpattern:=c;
  410. c:=current_scanner^.asmgetchar;
  411. end
  412. else
  413. actasmpattern:='';
  414. while c in ['0'..'9'] do
  415. Begin
  416. actasmpattern:=actasmpattern + c;
  417. c:=current_scanner^.asmgetchar;
  418. end;
  419. if c='.' then
  420. begin
  421. actasmpattern:=actasmpattern + c;
  422. c:=current_scanner^.asmgetchar;
  423. while c in ['0'..'9'] do
  424. Begin
  425. actasmpattern:=actasmpattern + c;
  426. c:=current_scanner^.asmgetchar;
  427. end;
  428. if upcase(c) = 'E' then
  429. begin
  430. actasmpattern:=actasmpattern + c;
  431. c:=current_scanner^.asmgetchar;
  432. if (c in ['+','-']) then
  433. begin
  434. actasmpattern:=actasmpattern + c;
  435. c:=current_scanner^.asmgetchar;
  436. end;
  437. while c in ['0'..'9'] do
  438. Begin
  439. actasmpattern:=actasmpattern + c;
  440. c:=current_scanner^.asmgetchar;
  441. end;
  442. end;
  443. actasmtoken:=AS_REALNUM;
  444. exit;
  445. end
  446. else
  447. begin
  448. Message1(asmr_e_invalid_float_const,actasmpattern+c);
  449. actasmtoken:=AS_NONE;
  450. end;
  451. end;
  452. 'X': { hexadecimal }
  453. Begin
  454. c:=current_scanner^.asmgetchar;
  455. while c in ['0'..'9','a'..'f','A'..'F'] do
  456. Begin
  457. actasmpattern:=actasmpattern + c;
  458. c:=current_scanner^.asmgetchar;
  459. end;
  460. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  461. actasmtoken:=AS_INTNUM;
  462. exit;
  463. end;
  464. '1'..'7': { octal }
  465. begin
  466. actasmpattern:=actasmpattern + c;
  467. while c in ['0'..'7'] do
  468. Begin
  469. actasmpattern:=actasmpattern + c;
  470. c:=current_scanner^.asmgetchar;
  471. end;
  472. actasmpattern:=tostr(ValOctal(actasmpattern));
  473. actasmtoken:=AS_INTNUM;
  474. exit;
  475. end;
  476. else { octal number zero value...}
  477. Begin
  478. actasmpattern:=tostr(ValOctal(actasmpattern));
  479. actasmtoken:=AS_INTNUM;
  480. exit;
  481. end;
  482. end; { end case }
  483. end;
  484. '&' :
  485. begin
  486. c:=current_scanner^.asmgetchar;
  487. actasmtoken:=AS_AND;
  488. end;
  489. '''' : { char }
  490. begin
  491. actasmpattern:='';
  492. repeat
  493. c:=current_scanner^.asmgetchar;
  494. case c of
  495. '\' :
  496. begin
  497. { copy also the next char so \" is parsed correctly }
  498. c:=current_scanner^.asmgetchar;
  499. actasmpattern:=actasmpattern+c;
  500. end;
  501. '''' :
  502. begin
  503. c:=current_scanner^.asmgetchar;
  504. break;
  505. end;
  506. newline:
  507. Message(scan_f_string_exceeds_line);
  508. else
  509. actasmpattern:=actasmpattern+c;
  510. end;
  511. until false;
  512. actasmpattern:=EscapeToPascal(actasmpattern);
  513. actasmtoken:=AS_STRING;
  514. exit;
  515. end;
  516. '"' : { string }
  517. begin
  518. actasmpattern:='';
  519. repeat
  520. c:=current_scanner^.asmgetchar;
  521. case c of
  522. '\' :
  523. begin
  524. { copy also the next char so \" is parsed correctly }
  525. c:=current_scanner^.asmgetchar;
  526. actasmpattern:=actasmpattern+c;
  527. end;
  528. '"' :
  529. begin
  530. c:=current_scanner^.asmgetchar;
  531. break;
  532. end;
  533. newline:
  534. Message(scan_f_string_exceeds_line);
  535. else
  536. actasmpattern:=actasmpattern+c;
  537. end;
  538. until false;
  539. actasmpattern:=EscapeToPascal(actasmpattern);
  540. actasmtoken:=AS_STRING;
  541. exit;
  542. end;
  543. '$' :
  544. begin
  545. actasmtoken:=AS_DOLLAR;
  546. c:=current_scanner^.asmgetchar;
  547. exit;
  548. end;
  549. ',' :
  550. begin
  551. actasmtoken:=AS_COMMA;
  552. c:=current_scanner^.asmgetchar;
  553. exit;
  554. end;
  555. '<' :
  556. begin
  557. actasmtoken:=AS_SHL;
  558. c:=current_scanner^.asmgetchar;
  559. if c = '<' then
  560. c:=current_scanner^.asmgetchar;
  561. exit;
  562. end;
  563. '>' :
  564. begin
  565. actasmtoken:=AS_SHL;
  566. c:=current_scanner^.asmgetchar;
  567. if c = '>' then
  568. c:=current_scanner^.asmgetchar;
  569. exit;
  570. end;
  571. '|' :
  572. begin
  573. actasmtoken:=AS_OR;
  574. c:=current_scanner^.asmgetchar;
  575. exit;
  576. end;
  577. '^' :
  578. begin
  579. actasmtoken:=AS_XOR;
  580. c:=current_scanner^.asmgetchar;
  581. exit;
  582. end;
  583. '!' :
  584. begin
  585. Message(asmr_e_nor_not_supported);
  586. c:=current_scanner^.asmgetchar;
  587. actasmtoken:=AS_NONE;
  588. exit;
  589. end;
  590. '(' :
  591. begin
  592. actasmtoken:=AS_LPAREN;
  593. c:=current_scanner^.asmgetchar;
  594. exit;
  595. end;
  596. ')' :
  597. begin
  598. actasmtoken:=AS_RPAREN;
  599. c:=current_scanner^.asmgetchar;
  600. exit;
  601. end;
  602. ':' :
  603. begin
  604. actasmtoken:=AS_COLON;
  605. c:=current_scanner^.asmgetchar;
  606. exit;
  607. end;
  608. '+' :
  609. begin
  610. actasmtoken:=AS_PLUS;
  611. c:=current_scanner^.asmgetchar;
  612. exit;
  613. end;
  614. '-' :
  615. begin
  616. actasmtoken:=AS_MINUS;
  617. c:=current_scanner^.asmgetchar;
  618. exit;
  619. end;
  620. '*' :
  621. begin
  622. actasmtoken:=AS_STAR;
  623. c:=current_scanner^.asmgetchar;
  624. exit;
  625. end;
  626. '/' :
  627. begin
  628. c:=current_scanner^.asmgetchar;
  629. actasmtoken:=AS_SLASH;
  630. exit;
  631. end;
  632. '{',#13,newline,';' :
  633. begin
  634. { the comment is read by asmgetchar }
  635. c:=current_scanner^.asmgetchar;
  636. firsttoken:=TRUE;
  637. actasmtoken:=AS_SEPARATOR;
  638. exit;
  639. end;
  640. else
  641. Begin
  642. Message(scan_f_illegal_char);
  643. end;
  644. end;
  645. end;
  646. end;
  647. function consume(t : tasmtoken):boolean;
  648. begin
  649. Consume:=true;
  650. if t<>actasmtoken then
  651. begin
  652. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  653. Consume:=false;
  654. end;
  655. repeat
  656. gettoken;
  657. until actasmtoken<>AS_NONE;
  658. end;
  659. procedure RecoverConsume(allowcomma:boolean);
  660. begin
  661. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  662. begin
  663. if allowcomma and (actasmtoken=AS_COMMA) then
  664. break;
  665. Consume(actasmtoken);
  666. end;
  667. end;
  668. {*****************************************************************************
  669. Parsing Helpers
  670. *****************************************************************************}
  671. Procedure BuildRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  672. { Description: This routine builds up a record offset after a AS_DOT }
  673. { token is encountered. }
  674. { On entry actasmtoken should be equal to AS_DOT }
  675. var
  676. s : string;
  677. Begin
  678. offset:=0;
  679. size:=0;
  680. s:=expr;
  681. while (actasmtoken=AS_DOT) do
  682. begin
  683. Consume(AS_DOT);
  684. if actasmtoken=AS_ID then
  685. s:=s+'.'+actasmpattern;
  686. if not Consume(AS_ID) then
  687. begin
  688. RecoverConsume(true);
  689. break;
  690. end;
  691. end;
  692. if not GetRecordOffsetSize(s,offset,size) then
  693. Message(asmr_e_building_record_offset);
  694. end;
  695. Procedure BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:longint;var asmsym:string);
  696. var
  697. hs,tempstr,expr : string;
  698. parenlevel,l,k : longint;
  699. errorflag : boolean;
  700. prevtok : tasmtoken;
  701. sym : psym;
  702. hl : PAsmLabel;
  703. Begin
  704. asmsym:='';
  705. value:=0;
  706. errorflag:=FALSE;
  707. tempstr:='';
  708. expr:='';
  709. inexpression:=TRUE;
  710. parenlevel:=0;
  711. Repeat
  712. Case actasmtoken of
  713. AS_LPAREN:
  714. Begin
  715. { Exit if ref? }
  716. if allowref and (prevasmtoken in [AS_INTNUM,AS_ID]) then
  717. break;
  718. Consume(AS_LPAREN);
  719. expr:=expr + '(';
  720. inc(parenlevel);
  721. end;
  722. AS_RPAREN:
  723. Begin
  724. { end of ref ? }
  725. if (parenlevel=0) and betweenbracket then
  726. break;
  727. Consume(AS_RPAREN);
  728. expr:=expr + ')';
  729. dec(parenlevel);
  730. end;
  731. AS_SHL:
  732. Begin
  733. Consume(AS_SHL);
  734. expr:=expr + '<';
  735. end;
  736. AS_SHR:
  737. Begin
  738. Consume(AS_SHR);
  739. expr:=expr + '>';
  740. end;
  741. AS_SLASH:
  742. Begin
  743. Consume(AS_SLASH);
  744. expr:=expr + '/';
  745. end;
  746. AS_MOD:
  747. Begin
  748. Consume(AS_MOD);
  749. expr:=expr + '%';
  750. end;
  751. AS_STAR:
  752. Begin
  753. Consume(AS_STAR);
  754. expr:=expr + '*';
  755. end;
  756. AS_PLUS:
  757. Begin
  758. Consume(AS_PLUS);
  759. expr:=expr + '+';
  760. end;
  761. AS_MINUS:
  762. Begin
  763. Consume(AS_MINUS);
  764. expr:=expr + '-';
  765. end;
  766. AS_AND:
  767. Begin
  768. Consume(AS_AND);
  769. expr:=expr + '&';
  770. end;
  771. AS_NOT:
  772. Begin
  773. Consume(AS_NOT);
  774. expr:=expr + '~';
  775. end;
  776. AS_XOR:
  777. Begin
  778. Consume(AS_XOR);
  779. expr:=expr + '^';
  780. end;
  781. AS_OR:
  782. Begin
  783. Consume(AS_OR);
  784. expr:=expr + '|';
  785. end;
  786. AS_INTNUM:
  787. Begin
  788. expr:=expr + actasmpattern;
  789. Consume(AS_INTNUM);
  790. end;
  791. AS_DOLLAR:
  792. begin
  793. Consume(AS_DOLLAR);
  794. if actasmtoken<>AS_ID then
  795. Comment(V_Error,'assem_e_dollar_without_identifier');
  796. end;
  797. AS_ID:
  798. Begin
  799. tempstr:=actasmpattern;
  800. prevtok:=prevasmtoken;
  801. consume(AS_ID);
  802. if actasmtoken=AS_DOT then
  803. begin
  804. BuildRecordOffsetSize(tempstr,l,k);
  805. str(l, tempstr);
  806. expr:=expr + tempstr;
  807. end
  808. else
  809. if SearchIConstant(tempstr,l) then
  810. begin
  811. str(l, tempstr);
  812. expr:=expr + tempstr;
  813. end
  814. else
  815. begin
  816. hs:='';
  817. if is_locallabel(tempstr) then
  818. begin
  819. CreateLocalLabel(tempstr,hl,false);
  820. hs:=hl^.name
  821. end
  822. else
  823. if SearchLabel(tempstr,hl,false) then
  824. hs:=hl^.name
  825. else
  826. begin
  827. getsym(tempstr,false);
  828. sym:=srsym;
  829. if assigned(sym) then
  830. begin
  831. if sym^.owner^.symtabletype in [localsymtable,parasymtable] then
  832. Message(asmr_e_no_local_or_para_allowed);
  833. case srsym^.typ of
  834. varsym :
  835. hs:=pvarsym(srsym)^.mangledname;
  836. typedconstsym :
  837. hs:=ptypedconstsym(srsym)^.mangledname;
  838. procsym :
  839. hs:=pprocsym(srsym)^.mangledname;
  840. else
  841. Message(asmr_e_wrong_sym_type);
  842. end;
  843. end
  844. else
  845. Message1(sym_e_unknown_id,tempstr);
  846. end;
  847. { symbol found? }
  848. if hs<>'' then
  849. begin
  850. if needofs and (prevtok<>AS_DOLLAR) then
  851. Message(asmr_e_need_offset);
  852. if asmsym='' then
  853. asmsym:=hs
  854. else
  855. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  856. if (expr='') or (expr[length(expr)]='+') then
  857. begin
  858. delete(expr,length(expr),1);
  859. if not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END]) then
  860. Message(asmr_e_only_add_relocatable_symbol);
  861. end
  862. else
  863. Message(asmr_e_only_add_relocatable_symbol);
  864. end
  865. else
  866. begin
  867. { Error recovery }
  868. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  869. delete(expr,length(expr),1);
  870. end;
  871. end;
  872. end;
  873. AS_END,
  874. AS_SEPARATOR,
  875. AS_COMMA:
  876. Begin
  877. break;
  878. end;
  879. else
  880. Begin
  881. { write error only once. }
  882. if not errorflag then
  883. Message(asmr_e_invalid_constant_expression);
  884. { consume tokens until we find COMMA or SEPARATOR }
  885. Consume(actasmtoken);
  886. errorflag:=TRUE;
  887. end;
  888. end;
  889. Until false;
  890. { calculate expression }
  891. if not ErrorFlag then
  892. value:=CalculateExpression(expr)
  893. else
  894. value:=0;
  895. { no longer in an expression }
  896. inexpression:=FALSE;
  897. end;
  898. Function BuildConstExpression(allowref,betweenbracket:boolean): longint;
  899. var
  900. l : longint;
  901. hs : string;
  902. begin
  903. BuildConstSymbolExpression(allowref,betweenbracket,false,l,hs);
  904. if hs<>'' then
  905. Message(asmr_e_relocatable_symbol_not_allowed);
  906. BuildConstExpression:=l;
  907. end;
  908. {****************************************************************************
  909. T386ATTOperand
  910. ****************************************************************************}
  911. type
  912. P386ATTOperand=^T386ATTOperand;
  913. T386ATTOperand=object(T386Operand)
  914. Procedure BuildOperand;virtual;
  915. private
  916. Procedure BuildReference;
  917. Procedure BuildConstant;
  918. end;
  919. Procedure T386ATTOperand.BuildReference;
  920. procedure Consume_RParen;
  921. begin
  922. if actasmtoken <> AS_RPAREN then
  923. Begin
  924. Message(asmr_e_invalid_reference_syntax);
  925. RecoverConsume(true);
  926. end
  927. else
  928. begin
  929. Consume(AS_RPAREN);
  930. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR,AS_END]) then
  931. Begin
  932. Message(asmr_e_invalid_reference_syntax);
  933. RecoverConsume(true);
  934. end;
  935. end;
  936. end;
  937. procedure Consume_Scale;
  938. var
  939. l : longint;
  940. begin
  941. { we have to process the scaling }
  942. l:=BuildConstExpression(false,true);
  943. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) then
  944. opr.ref.scalefactor:=l
  945. else
  946. Begin
  947. Message(asmr_e_wrong_scale_factor);
  948. opr.ref.scalefactor:=0;
  949. end;
  950. end;
  951. Begin
  952. Consume(AS_LPAREN);
  953. Case actasmtoken of
  954. AS_INTNUM,
  955. AS_MINUS,
  956. AS_PLUS: { absolute offset, such as fs:(0x046c) }
  957. Begin
  958. { offset(offset) is invalid }
  959. If opr.Ref.Offset <> 0 Then
  960. Begin
  961. Message(asmr_e_invalid_reference_syntax);
  962. RecoverConsume(true);
  963. End
  964. Else
  965. Begin
  966. opr.Ref.Offset:=BuildConstExpression(false,true);
  967. Consume_RParen;
  968. end;
  969. exit;
  970. End;
  971. AS_REGISTER: { (reg ... }
  972. Begin
  973. { Check if there is already a base (mostly ebp,esp) than this is
  974. not allowed,becuase it will give crashing code }
  975. if opr.ref.base<>R_NO then
  976. Message(asmr_e_cannot_index_relative_var);
  977. opr.ref.base:=actasmregister;
  978. Consume(AS_REGISTER);
  979. { can either be a register or a right parenthesis }
  980. { (reg) }
  981. if actasmtoken=AS_RPAREN then
  982. Begin
  983. Consume_RParen;
  984. exit;
  985. end;
  986. { (reg,reg .. }
  987. Consume(AS_COMMA);
  988. if actasmtoken=AS_REGISTER then
  989. Begin
  990. opr.ref.index:=actasmregister;
  991. Consume(AS_REGISTER);
  992. { check for scaling ... }
  993. case actasmtoken of
  994. AS_RPAREN:
  995. Begin
  996. Consume_RParen;
  997. exit;
  998. end;
  999. AS_COMMA:
  1000. Begin
  1001. Consume(AS_COMMA);
  1002. Consume_Scale;
  1003. Consume_RParen;
  1004. end;
  1005. else
  1006. Begin
  1007. Message(asmr_e_invalid_reference_syntax);
  1008. RecoverConsume(false);
  1009. end;
  1010. end; { end case }
  1011. end
  1012. else
  1013. Begin
  1014. Message(asmr_e_invalid_reference_syntax);
  1015. RecoverConsume(false);
  1016. end;
  1017. end; {end case }
  1018. AS_COMMA: { (, ... can either be scaling, or index }
  1019. Begin
  1020. Consume(AS_COMMA);
  1021. { Index }
  1022. if (actasmtoken=AS_REGISTER) then
  1023. Begin
  1024. opr.ref.index:=actasmregister;
  1025. Consume(AS_REGISTER);
  1026. { check for scaling ... }
  1027. case actasmtoken of
  1028. AS_RPAREN:
  1029. Begin
  1030. Consume_RParen;
  1031. exit;
  1032. end;
  1033. AS_COMMA:
  1034. Begin
  1035. Consume(AS_COMMA);
  1036. Consume_Scale;
  1037. Consume_RParen;
  1038. end;
  1039. else
  1040. Begin
  1041. Message(asmr_e_invalid_reference_syntax);
  1042. RecoverConsume(false);
  1043. end;
  1044. end; {end case }
  1045. end
  1046. { Scaling }
  1047. else
  1048. Begin
  1049. Consume_Scale;
  1050. Consume_RParen;
  1051. exit;
  1052. end;
  1053. end;
  1054. else
  1055. Begin
  1056. Message(asmr_e_invalid_reference_syntax);
  1057. RecoverConsume(false);
  1058. end;
  1059. end;
  1060. end;
  1061. Procedure T386ATTOperand.BuildConstant;
  1062. var
  1063. l : longint;
  1064. tempstr : string;
  1065. begin
  1066. BuildConstSymbolExpression(false,false,true,l,tempstr);
  1067. if tempstr<>'' then
  1068. begin
  1069. opr.typ:=OPR_SYMBOL;
  1070. opr.symofs:=l;
  1071. opr.symbol:=newasmsymbol(tempstr);
  1072. end
  1073. else
  1074. begin
  1075. opr.typ:=OPR_CONSTANT;
  1076. opr.val:=l;
  1077. end;
  1078. end;
  1079. Procedure T386ATTOperand.BuildOperand;
  1080. procedure AddLabelOperand(hl:pasmlabel);
  1081. begin
  1082. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  1083. is_calljmp(actopcode) then
  1084. begin
  1085. opr.typ:=OPR_SYMBOL;
  1086. opr.symbol:=hl;
  1087. end
  1088. else
  1089. begin
  1090. InitRef;
  1091. opr.ref.symbol:=hl;
  1092. end;
  1093. end;
  1094. function MaybeBuildReference:boolean;
  1095. { Try to create a reference, if not a reference is found then false
  1096. is returned }
  1097. begin
  1098. MaybeBuildReference:=true;
  1099. case actasmtoken of
  1100. AS_INTNUM,
  1101. AS_MINUS,
  1102. AS_PLUS:
  1103. Begin
  1104. opr.ref.offset:=BuildConstExpression(True,False);
  1105. if actasmtoken<>AS_LPAREN then
  1106. Message(asmr_e_invalid_reference_syntax)
  1107. else
  1108. BuildReference;
  1109. end;
  1110. AS_LPAREN:
  1111. BuildReference;
  1112. AS_ID: { only a variable is allowed ... }
  1113. Begin
  1114. if not SetupVar(actasmpattern) then
  1115. Message(asmr_e_invalid_reference_syntax);
  1116. Consume(actasmtoken);
  1117. case actasmtoken of
  1118. AS_END,
  1119. AS_SEPARATOR,
  1120. AS_COMMA: ;
  1121. AS_LPAREN: BuildReference;
  1122. else
  1123. Begin
  1124. Message(asmr_e_invalid_reference_syntax);
  1125. Consume(actasmtoken);
  1126. end;
  1127. end; {end case }
  1128. end;
  1129. else
  1130. MaybeBuildReference:=false;
  1131. end; { end case }
  1132. end;
  1133. var
  1134. expr,
  1135. tempstr : string;
  1136. tempreg : tregister;
  1137. hl : PAsmLabel;
  1138. tsize,l,
  1139. toffset : longint;
  1140. Begin
  1141. tempstr:='';
  1142. expr:='';
  1143. case actasmtoken of
  1144. AS_LPAREN: { Memory reference or constant expression }
  1145. Begin
  1146. InitRef;
  1147. BuildReference;
  1148. end;
  1149. AS_DOLLAR: { Constant expression }
  1150. Begin
  1151. Consume(AS_DOLLAR);
  1152. BuildConstant;
  1153. end;
  1154. AS_INTNUM,
  1155. AS_MINUS,
  1156. AS_PLUS:
  1157. Begin
  1158. { Constant memory offset }
  1159. { This must absolutely be followed by ( }
  1160. InitRef;
  1161. opr.ref.offset:=BuildConstExpression(True,False);
  1162. if actasmtoken<>AS_LPAREN then
  1163. Message(asmr_e_invalid_reference_syntax)
  1164. else
  1165. BuildReference;
  1166. end;
  1167. AS_STAR: { Call from memory address }
  1168. Begin
  1169. Consume(AS_STAR);
  1170. if actasmtoken=AS_REGISTER then
  1171. begin
  1172. opr.typ:=OPR_REGISTER;
  1173. opr.reg:=actasmregister;
  1174. size:=reg_2_opsize[actasmregister];
  1175. Consume(AS_REGISTER);
  1176. end
  1177. else
  1178. begin
  1179. InitRef;
  1180. if not MaybeBuildReference then
  1181. Message(asmr_e_syn_operand);
  1182. end;
  1183. { this is only allowed for call's and jmp's }
  1184. if not is_calljmp(actopcode) then
  1185. Message(asmr_e_syn_operand);
  1186. end;
  1187. AS_ID: { A constant expression, or a Variable ref. }
  1188. Begin
  1189. { Local Label ? }
  1190. if is_locallabel(actasmpattern) then
  1191. begin
  1192. CreateLocalLabel(actasmpattern,hl,false);
  1193. Consume(AS_ID);
  1194. AddLabelOperand(hl);
  1195. end
  1196. else
  1197. { Check for label }
  1198. if SearchLabel(actasmpattern,hl,false) then
  1199. begin
  1200. Consume(AS_ID);
  1201. AddLabelOperand(hl);
  1202. end
  1203. else
  1204. { probably a variable or normal expression }
  1205. { or a procedure (such as in CALL ID) }
  1206. Begin
  1207. InitRef;
  1208. if not SetupVar(actasmpattern) then
  1209. Begin
  1210. { look for special symbols ... }
  1211. if actasmpattern = '__RESULT' then
  1212. SetUpResult
  1213. else
  1214. if actasmpattern = '__SELF' then
  1215. SetupSelf
  1216. else
  1217. if actasmpattern = '__OLDEBP' then
  1218. SetupOldEBP
  1219. else
  1220. { check for direct symbolic names }
  1221. { only if compiling the system unit }
  1222. if (cs_compilesystem in aktmoduleswitches) then
  1223. begin
  1224. if not SetupDirectVar(actasmpattern) then
  1225. Begin
  1226. { not found, finally ... add it anyways ... }
  1227. Message1(asmr_w_id_supposed_external,actasmpattern);
  1228. opr.ref.symbol:=newasmsymbol(actasmpattern);
  1229. end;
  1230. end
  1231. else
  1232. Message1(sym_e_unknown_id,actasmpattern);
  1233. end;
  1234. { constant expression? }
  1235. if (opr.typ=OPR_CONSTANT) then
  1236. begin
  1237. l:=BuildConstExpression(true,false);
  1238. { indexing? }
  1239. if actasmtoken=AS_LPAREN then
  1240. begin
  1241. opr.typ:=OPR_REFERENCE;
  1242. reset_reference(opr.Ref);
  1243. opr.Ref.Offset:=l;
  1244. BuildReference;
  1245. end
  1246. else
  1247. opr.Val:=l;
  1248. end
  1249. else
  1250. begin
  1251. expr:=actasmpattern;
  1252. Consume(AS_ID);
  1253. if actasmtoken=AS_DOT then
  1254. begin
  1255. BuildRecordOffsetSize(expr,toffset,tsize);
  1256. inc(opr.ref.offset,toffset);
  1257. SetSize(tsize);
  1258. end;
  1259. end;
  1260. end;
  1261. if opr.typ=OPR_REFERENCE then
  1262. begin
  1263. { Do we have a +[constant] ? }
  1264. if actasmtoken in [AS_PLUS,AS_MINUS] then
  1265. inc(opr.ref.offset,BuildConstExpression(true,false));
  1266. { Do we have a indexing reference, then parse it also }
  1267. if actasmtoken=AS_LPAREN then
  1268. BuildReference;
  1269. end;
  1270. end;
  1271. AS_REGISTER: { Register, a variable reference or a constant reference }
  1272. Begin
  1273. { save the type of register used. }
  1274. tempreg:=actasmregister;
  1275. Consume(AS_REGISTER);
  1276. if actasmtoken = AS_COLON then
  1277. Begin
  1278. Consume(AS_COLON);
  1279. InitRef;
  1280. opr.ref.segment:=tempreg;
  1281. { This must absolutely be followed by a reference }
  1282. if not MaybeBuildReference then
  1283. Begin
  1284. Message(asmr_e_invalid_seg_override);
  1285. Consume(actasmtoken);
  1286. end;
  1287. end
  1288. { Simple register }
  1289. else if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1290. Begin
  1291. if not (opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1292. Message(asmr_e_invalid_operand_type);
  1293. opr.typ:=OPR_REGISTER;
  1294. opr.reg:=tempreg;
  1295. size:=reg_2_opsize[tempreg];
  1296. end
  1297. else
  1298. Message(asmr_e_syn_operand);
  1299. end;
  1300. AS_END,
  1301. AS_SEPARATOR,
  1302. AS_COMMA: ;
  1303. else
  1304. Begin
  1305. Message(asmr_e_syn_operand);
  1306. Consume(actasmtoken);
  1307. end;
  1308. end; { end case }
  1309. end;
  1310. {*****************************************************************************
  1311. T386ATTInstruction
  1312. *****************************************************************************}
  1313. type
  1314. P386AttInstruction=^T386AttInstruction;
  1315. T386AttInstruction=object(T386Instruction)
  1316. procedure InitOperands;virtual;
  1317. procedure BuildOpcode;virtual;
  1318. end;
  1319. procedure T386AttInstruction.InitOperands;
  1320. var
  1321. i : longint;
  1322. begin
  1323. for i:=1to 3 do
  1324. Operands[i]:=new(P386AttOperand,Init);
  1325. end;
  1326. Procedure T386AttInstruction.BuildOpCode;
  1327. var
  1328. operandnum : longint;
  1329. PrefixOp,OverrideOp: tasmop;
  1330. expr : string;
  1331. Begin
  1332. expr:='';
  1333. PrefixOp:=A_None;
  1334. OverrideOp:=A_None;
  1335. { prefix seg opcode / prefix opcode }
  1336. repeat
  1337. if is_prefix(actopcode) then
  1338. begin
  1339. PrefixOp:=ActOpcode;
  1340. opcode:=ActOpcode;
  1341. condition:=ActCondition;
  1342. opsize:=ActOpsize;
  1343. ConcatInstruction(curlist);
  1344. Consume(AS_OPCODE);
  1345. end
  1346. else
  1347. if is_override(actopcode) then
  1348. begin
  1349. OverrideOp:=ActOpcode;
  1350. opcode:=ActOpcode;
  1351. condition:=ActCondition;
  1352. opsize:=ActOpsize;
  1353. ConcatInstruction(curlist);
  1354. Consume(AS_OPCODE);
  1355. end
  1356. else
  1357. break;
  1358. { allow for newline as in gas styled syntax }
  1359. while actasmtoken=AS_SEPARATOR do
  1360. Consume(AS_SEPARATOR);
  1361. until (actasmtoken<>AS_OPCODE);
  1362. { opcode }
  1363. if (actasmtoken <> AS_OPCODE) then
  1364. Begin
  1365. Message(asmr_e_invalid_or_missing_opcode);
  1366. RecoverConsume(true);
  1367. exit;
  1368. end;
  1369. { Fill the instr object with the current state }
  1370. Opcode:=ActOpcode;
  1371. condition:=ActCondition;
  1372. opsize:=ActOpsize;
  1373. { Valid combination of prefix/override and instruction ? }
  1374. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1375. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1376. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1377. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1378. { We are reading operands, so opcode will be an AS_ID }
  1379. operandnum:=1;
  1380. Consume(AS_OPCODE);
  1381. { Zero operand opcode ? }
  1382. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1383. begin
  1384. operandnum:=0;
  1385. exit;
  1386. end;
  1387. { Read the operands }
  1388. repeat
  1389. case actasmtoken of
  1390. AS_COMMA: { Operand delimiter }
  1391. Begin
  1392. if operandnum > MaxOperands then
  1393. Message(asmr_e_too_many_operands)
  1394. else
  1395. Inc(operandnum);
  1396. Consume(AS_COMMA);
  1397. end;
  1398. AS_SEPARATOR,
  1399. AS_END : { End of asm operands for this opcode }
  1400. begin
  1401. break;
  1402. end;
  1403. else
  1404. Operands[operandnum]^.BuildOperand;
  1405. end; { end case }
  1406. until false;
  1407. Ops:=operandnum;
  1408. end;
  1409. Procedure BuildConstant(maxvalue: longint);
  1410. var
  1411. strlength: byte;
  1412. asmsym,
  1413. expr: string;
  1414. value : longint;
  1415. Begin
  1416. Repeat
  1417. Case actasmtoken of
  1418. AS_STRING:
  1419. Begin
  1420. if maxvalue = $ff then
  1421. strlength:=1
  1422. else
  1423. Message(asmr_e_string_not_allowed_as_const);
  1424. expr:=actasmpattern;
  1425. if length(expr) > 1 then
  1426. Message(asmr_e_string_not_allowed_as_const);
  1427. Consume(AS_STRING);
  1428. Case actasmtoken of
  1429. AS_COMMA: Consume(AS_COMMA);
  1430. AS_END,
  1431. AS_SEPARATOR: ;
  1432. else
  1433. Message(asmr_e_invalid_string_expression);
  1434. end; { end case }
  1435. ConcatString(curlist,expr);
  1436. end;
  1437. AS_INTNUM,
  1438. AS_PLUS,
  1439. AS_MINUS,
  1440. AS_LPAREN,
  1441. AS_NOT,
  1442. AS_ID :
  1443. Begin
  1444. BuildConstSymbolExpression(false,false,false,value,asmsym);
  1445. if asmsym<>'' then
  1446. begin
  1447. if maxvalue<>$ffffffff then
  1448. Comment(V_Warning,'32bit constant created for address');
  1449. ConcatConstSymbol(curlist,asmsym,value)
  1450. end
  1451. else
  1452. ConcatConstant(curlist,value,maxvalue);
  1453. end;
  1454. AS_COMMA:
  1455. Consume(AS_COMMA);
  1456. AS_END,
  1457. AS_SEPARATOR:
  1458. break;
  1459. else
  1460. begin
  1461. Message(asmr_e_syn_constant);
  1462. RecoverConsume(false);
  1463. end
  1464. end; { end case }
  1465. Until false;
  1466. end;
  1467. Procedure BuildRealConstant(typ : tfloattype);
  1468. var
  1469. expr : string;
  1470. r : bestreal;
  1471. code : integer;
  1472. negativ : boolean;
  1473. errorflag: boolean;
  1474. Begin
  1475. errorflag:=FALSE;
  1476. Repeat
  1477. negativ:=false;
  1478. expr:='';
  1479. if actasmtoken=AS_PLUS then
  1480. Consume(AS_PLUS)
  1481. else
  1482. if actasmtoken=AS_MINUS then
  1483. begin
  1484. negativ:=true;
  1485. consume(AS_MINUS);
  1486. end;
  1487. Case actasmtoken of
  1488. AS_INTNUM:
  1489. Begin
  1490. expr:=actasmpattern;
  1491. Consume(AS_INTNUM);
  1492. if negativ then
  1493. expr:='-'+expr;
  1494. val(expr,r,code);
  1495. if code<>0 then
  1496. Begin
  1497. r:=0;
  1498. Message(asmr_e_invalid_float_expr);
  1499. End;
  1500. ConcatRealConstant(curlist,r,typ);
  1501. end;
  1502. AS_REALNUM:
  1503. Begin
  1504. expr:=actasmpattern;
  1505. Consume(AS_REALNUM);
  1506. { in ATT syntax you have 0d in front of the real }
  1507. { should this be forced ? yes i think so, as to }
  1508. { conform to gas as much as possible. }
  1509. if (expr[1]='0') and (upper(expr[2])='D') then
  1510. Delete(expr,1,2);
  1511. if negativ then
  1512. expr:='-'+expr;
  1513. val(expr,r,code);
  1514. if code<>0 then
  1515. Begin
  1516. r:=0;
  1517. Message(asmr_e_invalid_float_expr);
  1518. End;
  1519. ConcatRealConstant(curlist,r,typ);
  1520. end;
  1521. AS_COMMA:
  1522. begin
  1523. Consume(AS_COMMA);
  1524. end;
  1525. AS_END,
  1526. AS_SEPARATOR:
  1527. begin
  1528. break;
  1529. end;
  1530. else
  1531. Begin
  1532. Consume(actasmtoken);
  1533. if not errorflag then
  1534. Message(asmr_e_invalid_float_expr);
  1535. errorflag:=TRUE;
  1536. end;
  1537. end;
  1538. Until false;
  1539. end;
  1540. Procedure BuildStringConstant(asciiz: boolean);
  1541. var
  1542. expr: string;
  1543. errorflag : boolean;
  1544. Begin
  1545. errorflag:=FALSE;
  1546. Repeat
  1547. Case actasmtoken of
  1548. AS_STRING:
  1549. Begin
  1550. expr:=actasmpattern;
  1551. if asciiz then
  1552. expr:=expr+#0;
  1553. ConcatPasString(curlist,expr);
  1554. Consume(AS_STRING);
  1555. end;
  1556. AS_COMMA:
  1557. begin
  1558. Consume(AS_COMMA);
  1559. end;
  1560. AS_END,
  1561. AS_SEPARATOR:
  1562. begin
  1563. break;
  1564. end;
  1565. else
  1566. Begin
  1567. Consume(actasmtoken);
  1568. if not errorflag then
  1569. Message(asmr_e_invalid_string_expression);
  1570. errorflag:=TRUE;
  1571. end;
  1572. end;
  1573. Until false;
  1574. end;
  1575. Function Assemble: Ptree;
  1576. Var
  1577. hl : PAsmLabel;
  1578. commname : string;
  1579. lastsec : tsection;
  1580. instr : T386ATTInstruction;
  1581. Begin
  1582. Message1(asmr_d_start_reading,'AT&T');
  1583. firsttoken:=TRUE;
  1584. if assigned(procinfo.retdef) and
  1585. (is_fpu(procinfo.retdef) or
  1586. ret_in_acc(procinfo.retdef)) then
  1587. procinfo.funcret_is_valid:=true;
  1588. { sets up all opcode and register tables in uppercase }
  1589. if not _asmsorted then
  1590. Begin
  1591. SetupTables;
  1592. _asmsorted:=TRUE;
  1593. end;
  1594. curlist:=new(paasmoutput,init);
  1595. lastsec:=sec_code;
  1596. { setup label linked list }
  1597. new(LocalLabelList,Init);
  1598. { start tokenizer }
  1599. c:=current_scanner^.asmgetchar;
  1600. gettoken;
  1601. { main loop }
  1602. repeat
  1603. case actasmtoken of
  1604. AS_LLABEL:
  1605. Begin
  1606. if CreateLocalLabel(actasmpattern,hl,true) then
  1607. ConcatLabel(curlist,hl);
  1608. Consume(AS_LLABEL);
  1609. end;
  1610. AS_LABEL:
  1611. Begin
  1612. if SearchLabel(upper(actasmpattern),hl,true) then
  1613. ConcatLabel(curlist,hl)
  1614. else
  1615. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1616. Consume(AS_LABEL);
  1617. end;
  1618. AS_DW:
  1619. Begin
  1620. Consume(AS_DW);
  1621. BuildConstant($ffff);
  1622. end;
  1623. AS_DATA:
  1624. Begin
  1625. curlist^.Concat(new(pai_section,init(sec_data)));
  1626. lastsec:=sec_data;
  1627. Consume(AS_DATA);
  1628. end;
  1629. AS_TEXT:
  1630. Begin
  1631. curlist^.Concat(new(pai_section,init(sec_code)));
  1632. lastsec:=sec_code;
  1633. Consume(AS_TEXT);
  1634. end;
  1635. AS_DB:
  1636. Begin
  1637. Consume(AS_DB);
  1638. BuildConstant($ff);
  1639. end;
  1640. AS_DD:
  1641. Begin
  1642. Consume(AS_DD);
  1643. BuildConstant($ffffffff);
  1644. end;
  1645. AS_DQ:
  1646. Begin
  1647. Consume(AS_DQ);
  1648. BuildRealConstant(s64comp);
  1649. end;
  1650. AS_SINGLE:
  1651. Begin
  1652. Consume(AS_SINGLE);
  1653. BuildRealConstant(s32real);
  1654. end;
  1655. AS_DOUBLE:
  1656. Begin
  1657. Consume(AS_DOUBLE);
  1658. BuildRealConstant(s64real);
  1659. end;
  1660. AS_EXTENDED:
  1661. Begin
  1662. Consume(AS_EXTENDED);
  1663. BuildRealConstant(s80real);
  1664. end;
  1665. AS_GLOBAL:
  1666. Begin
  1667. Consume(AS_GLOBAL);
  1668. if actasmtoken=AS_ID then
  1669. ConcatPublic(curlist,actasmpattern);
  1670. Consume(AS_ID);
  1671. if actasmtoken<>AS_SEPARATOR then
  1672. Consume(AS_SEPARATOR);
  1673. end;
  1674. AS_ALIGN:
  1675. Begin
  1676. Consume(AS_ALIGN);
  1677. ConcatAlign(curlist,BuildConstExpression(false,false));
  1678. if actasmtoken<>AS_SEPARATOR then
  1679. Consume(AS_SEPARATOR);
  1680. end;
  1681. AS_ASCIIZ:
  1682. Begin
  1683. Consume(AS_ASCIIZ);
  1684. BuildStringConstant(TRUE);
  1685. end;
  1686. AS_ASCII:
  1687. Begin
  1688. Consume(AS_ASCII);
  1689. BuildStringConstant(FALSE);
  1690. end;
  1691. AS_LCOMM:
  1692. Begin
  1693. Consume(AS_LCOMM);
  1694. commname:=actasmpattern;
  1695. Consume(AS_ID);
  1696. Consume(AS_COMMA);
  1697. ConcatLocalBss(commname,BuildConstExpression(false,false));
  1698. if actasmtoken<>AS_SEPARATOR then
  1699. Consume(AS_SEPARATOR);
  1700. end;
  1701. AS_COMM:
  1702. Begin
  1703. Consume(AS_COMM);
  1704. commname:=actasmpattern;
  1705. Consume(AS_ID);
  1706. Consume(AS_COMMA);
  1707. ConcatGlobalBss(commname,BuildConstExpression(false,false));
  1708. if actasmtoken<>AS_SEPARATOR then
  1709. Consume(AS_SEPARATOR);
  1710. end;
  1711. AS_OPCODE:
  1712. Begin
  1713. instr.init;
  1714. instr.BuildOpcode;
  1715. instr.AddReferenceSizes;
  1716. instr.SetInstructionOpsize;
  1717. instr.CheckOperandSizes;
  1718. instr.ConcatInstruction(curlist);
  1719. instr.done;
  1720. end;
  1721. AS_SEPARATOR:
  1722. Begin
  1723. Consume(AS_SEPARATOR);
  1724. end;
  1725. AS_END:
  1726. begin
  1727. break; { end assembly block }
  1728. end;
  1729. else
  1730. Begin
  1731. Message(asmr_e_syntax_error);
  1732. RecoverConsume(false);
  1733. end;
  1734. end;
  1735. until false;
  1736. { Check LocalLabelList }
  1737. LocalLabelList^.CheckEmitted;
  1738. dispose(LocalLabelList,Done);
  1739. { are we back in the code section? }
  1740. if lastsec<>sec_code then
  1741. begin
  1742. Message(asmr_w_assembler_code_not_returned_to_text);
  1743. curlist^.Concat(new(pai_section,init(sec_code)));
  1744. end;
  1745. { Return the list in an asmnode }
  1746. assemble:=genasmnode(curlist);
  1747. Message1(asmr_d_finish_reading,'AT&T');
  1748. end;
  1749. {*****************************************************************************
  1750. Initialize
  1751. *****************************************************************************}
  1752. var
  1753. old_exit : pointer;
  1754. procedure ra386att_exit;{$ifndef FPC}far;{$endif}
  1755. begin
  1756. if assigned(iasmops) then
  1757. dispose(iasmops);
  1758. if assigned(iasmregs) then
  1759. dispose(iasmregs);
  1760. exitproc:=old_exit;
  1761. end;
  1762. begin
  1763. old_exit:=exitproc;
  1764. exitproc:=@ra386att_exit;
  1765. end.
  1766. {
  1767. $Log$
  1768. Revision 1.50 1999-06-08 11:51:58 peter
  1769. * fixed some intel bugs with scale parsing
  1770. * end is now also a separator in many more cases
  1771. Revision 1.49 1999/06/03 16:28:03 pierre
  1772. * typo corrected
  1773. Revision 1.48 1999/05/27 19:44:56 peter
  1774. * removed oldasm
  1775. * plabel -> pasmlabel
  1776. * -a switches to source writing automaticly
  1777. * assembler readers OOPed
  1778. * asmsymbol automaticly external
  1779. * jumptables and other label fixes for asm readers
  1780. Revision 1.47 1999/05/21 13:55:13 peter
  1781. * NEWLAB for label as symbol
  1782. Revision 1.46 1999/05/12 00:19:56 peter
  1783. * removed R_DEFAULT_SEG
  1784. * uniform float names
  1785. Revision 1.45 1999/05/06 09:05:25 peter
  1786. * generic write_float and str_float
  1787. * fixed constant float conversions
  1788. Revision 1.44 1999/05/05 22:22:00 peter
  1789. * updated messages
  1790. Revision 1.43 1999/05/04 21:45:01 florian
  1791. * changes to compile it with Delphi 4.0
  1792. Revision 1.42 1999/05/02 14:25:07 peter
  1793. * only allow *<reg/ref> when call/jmp is used
  1794. Revision 1.41 1999/05/01 13:48:39 peter
  1795. * merged nasm compiler
  1796. Revision 1.7 1999/04/29 09:37:47 peter
  1797. * fixed var+const support
  1798. Revision 1.6 1999/04/26 23:26:17 peter
  1799. * redesigned record offset parsing to support nested records
  1800. * normal compiler uses the redesigned createvarinstr()
  1801. Revision 1.5 1999/04/20 11:01:23 peter
  1802. * better tokenpos info
  1803. Revision 1.4 1999/04/14 09:07:45 peter
  1804. * asm reader improvements
  1805. Revision 1.3 1999/03/06 17:24:26 peter
  1806. * rewritten intel parser a lot, especially reference reading
  1807. * size checking added for asm parsers
  1808. Revision 1.2 1999/03/02 02:56:30 peter
  1809. + stabs support for binary writers
  1810. * more fixes and missing updates from the previous commit :(
  1811. Revision 1.1 1999/03/01 15:46:26 peter
  1812. * ag386bin finally make cycles correct
  1813. * prefixes are now also normal opcodes
  1814. }