ra386att.pas 56 KB

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