raatt.pas 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. Does the parsing for the GAS styled inline assembler.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit raatt;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,cclasses,
  23. { global }
  24. globtype,
  25. { aasm }
  26. cpubase,cpuinfo,aasmbase,aasmtai,aasmdata,aasmcpu,
  27. { assembler reader }
  28. rabase,
  29. rasm,
  30. rautils,
  31. { symtable }
  32. symconst,
  33. { cg }
  34. cgbase;
  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. AS_HASH,AS_LSBRACKET,AS_RSBRACKET,AS_LBRACKET,AS_RBRACKET,
  42. AS_EQUAL,
  43. {------------------ Assembler directives --------------------}
  44. AS_DB,AS_DW,AS_DD,AS_DQ,AS_GLOBAL,
  45. AS_ALIGN,AS_BALIGN,AS_P2ALIGN,AS_ASCII,
  46. AS_ASCIIZ,AS_LCOMM,AS_COMM,AS_SINGLE,AS_DOUBLE,AS_EXTENDED,AS_CEXTENDED,
  47. AS_DATA,AS_TEXT,AS_INIT,AS_FINI,AS_RVA,
  48. AS_SET,AS_WEAK,AS_SECTION,AS_END,
  49. {------------------ Assembler Operators --------------------}
  50. AS_TYPE,AS_SIZEOF,AS_VMTOFFSET,AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR,AS_NOR,AS_AT,
  51. AS_RELTYPE, // common token for relocation types
  52. {------------------ Target-specific directive ---------------}
  53. AS_TARGET_DIRECTIVE
  54. );
  55. tasmkeyword = string[10];
  56. const
  57. { These tokens should be modified accordingly to the modifications }
  58. { in the different enumerations. }
  59. firstdirective = AS_DB;
  60. lastdirective = AS_END;
  61. token2str : array[tasmtoken] of tasmkeyword=(
  62. '','Label','LLabel','string','integer',
  63. 'float',',','(',
  64. ')',':','.','+','-','*',
  65. ';','identifier','register','opcode','/','$',
  66. '#','{','}','[',']',
  67. '=',
  68. '.byte','.word','.long','.quad','.globl',
  69. '.align','.balign','.p2align','.ascii',
  70. '.asciz','.lcomm','.comm','.single','.double','.tfloat','.tcfloat',
  71. '.data','.text','.init','.fini','.rva',
  72. '.set','.weak','.section','END',
  73. 'TYPE','SIZEOF','VMTOFFSET','%','<<','>>','!','&','|','^','~','@','reltype',
  74. 'directive');
  75. type
  76. tattreader = class(tasmreader)
  77. actasmtoken : tasmtoken;
  78. prevasmtoken : tasmtoken;
  79. procedure SetupTables;
  80. procedure BuildConstant(constsize: byte);
  81. procedure BuildConstantOperand(oper : toperand);
  82. procedure BuildRealConstant(typ : tfloattype);
  83. procedure BuildStringConstant(asciiz: boolean);
  84. procedure BuildRva;
  85. procedure BuildRecordOffsetSize(const expr: string;var offset:tcgint;var size:tcgint; var mangledname: string; needvmtofs: boolean);
  86. procedure BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:tcgint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  87. function BuildConstExpression(allowref,betweenbracket:boolean): tcgint;
  88. function Assemble: tlinkedlist;override;
  89. procedure handleopcode;virtual;abstract;
  90. function is_asmopcode(const s: string) : boolean;virtual;abstract;
  91. Function is_asmdirective(const s: string):boolean;
  92. function is_register(const s:string):boolean;virtual;
  93. function is_locallabel(const s: string):boolean;
  94. function is_targetdirective(const s: string): boolean;virtual;
  95. procedure GetToken;
  96. function consume(t : tasmtoken):boolean;
  97. procedure RecoverConsume(allowcomma:boolean);
  98. procedure handlepercent;virtual;
  99. procedure handledollar;virtual;
  100. procedure HandleTargetDirective;virtual;
  101. end;
  102. implementation
  103. uses
  104. { globals }
  105. verbose,systems,
  106. { input }
  107. scanner, pbase,
  108. { symtable }
  109. symbase,symtype,symsym,symdef,symtable,
  110. {$ifdef x86}
  111. rax86,
  112. {$endif x86}
  113. itcpugas,
  114. procinfo;
  115. procedure tattreader.SetupTables;
  116. var
  117. i : tasmop;
  118. Begin
  119. iasmops:=TFPHashList.create;
  120. for i:=firstop to lastop do
  121. iasmops.Add(upper(gas_op2str[i]),Pointer(PtrInt(i)));
  122. end;
  123. function tattreader.is_asmdirective(const s: string):boolean;
  124. var
  125. i : tasmtoken;
  126. hs : string;
  127. Begin
  128. { GNU as is also not casesensitive with this }
  129. hs:=lower(s);
  130. for i:=firstdirective to lastdirective do
  131. if hs=token2str[i] then
  132. begin
  133. actasmtoken:=i;
  134. is_asmdirective:=true;
  135. exit;
  136. end;
  137. is_asmdirective:=false;
  138. end;
  139. function tattreader.is_register(const s:string):boolean;
  140. begin
  141. is_register:=false;
  142. actasmregister:=gas_regnum_search(lower(s));
  143. if actasmregister<>NR_NO then
  144. begin
  145. is_register:=true;
  146. actasmtoken:=AS_REGISTER;
  147. end;
  148. end;
  149. function tattreader.is_locallabel(const s: string):boolean;
  150. begin
  151. is_locallabel:=(length(s)>=2) and (s[1]='.') and (s[2]='L');
  152. end;
  153. procedure tattreader.handledollar;
  154. begin
  155. c:=current_scanner.asmgetchar;
  156. actasmtoken:=AS_DOLLAR;
  157. end;
  158. procedure tattreader.handlepercent;
  159. begin
  160. c:=current_scanner.asmgetchar;
  161. actasmtoken:=AS_MOD;
  162. end;
  163. function tattreader.is_targetdirective(const s: string): boolean;
  164. begin
  165. result:=false;
  166. end;
  167. procedure tattreader.handletargetdirective;
  168. begin
  169. end;
  170. procedure tattreader.GetToken;
  171. var
  172. len : longint;
  173. srsym : tsym;
  174. srsymtable : TSymtable;
  175. begin
  176. c:=scanner.c;
  177. { save old token and reset new token }
  178. prevasmtoken:=actasmtoken;
  179. actasmtoken:=AS_NONE;
  180. { reset }
  181. actasmpattern:='';
  182. { while space and tab , continue scan... }
  183. while c in [' ',#9] do
  184. c:=current_scanner.asmgetchar;
  185. { get token pos }
  186. if not (c in [#10,#13,'{',';','/','(']) then
  187. current_scanner.gettokenpos;
  188. { Local Label, Label, Directive, Prefix or Opcode }
  189. if firsttoken and not(c in [#10,#13,'{',';','/','(']) then
  190. begin
  191. firsttoken:=FALSE;
  192. len:=0;
  193. { directive or local label }
  194. if c = '.' then
  195. begin
  196. inc(len);
  197. actasmpattern[len]:=c;
  198. { Let us point to the next character }
  199. c:=current_scanner.asmgetchar;
  200. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  201. begin
  202. inc(len);
  203. actasmpattern[len]:=c;
  204. c:=current_scanner.asmgetchar;
  205. end;
  206. actasmpattern[0]:=chr(len);
  207. { this is a local label... }
  208. if (c=':') and is_locallabel(actasmpattern) then
  209. Begin
  210. { local variables are case sensitive }
  211. actasmtoken:=AS_LLABEL;
  212. c:=current_scanner.asmgetchar;
  213. firsttoken:=true;
  214. exit;
  215. end
  216. { must be a directive }
  217. else
  218. Begin
  219. { directives are case sensitive!! }
  220. if is_asmdirective(actasmpattern) then
  221. exit;
  222. if is_targetdirective(actasmpattern) then
  223. begin
  224. actasmtoken:=AS_TARGET_DIRECTIVE;
  225. exit;
  226. end;
  227. Message1(asmr_e_not_directive_or_local_symbol,actasmpattern);
  228. end;
  229. end;
  230. { only opcodes and global labels are allowed now. }
  231. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  232. begin
  233. inc(len);
  234. actasmpattern[len]:=c;
  235. c:=current_scanner.asmgetchar;
  236. end;
  237. actasmpattern[0]:=chr(len);
  238. { Label ? }
  239. if c = ':' then
  240. begin
  241. actasmtoken:=AS_LABEL;
  242. { let us point to the next character }
  243. c:=current_scanner.asmgetchar;
  244. firsttoken:=true;
  245. exit;
  246. end;
  247. {$if defined(POWERPC) or defined(POWERPC64)}
  248. { some PowerPC instructions can have the postfix -, + or .
  249. this code could be moved to is_asmopcode but I think
  250. it's better to ifdef it here (FK)
  251. }
  252. case c of
  253. '.', '-', '+':
  254. begin
  255. actasmpattern:=actasmpattern+c;
  256. c:=current_scanner.asmgetchar;
  257. end
  258. end;
  259. {$endif POWERPC}
  260. {$if defined(ARM)}
  261. {
  262. Thumb-2 instructions can have a .W postfix to indicate 32bit instructions,
  263. Also in unified syntax sizes and types are indicated with something like a .<dt> prefix for example
  264. }
  265. case c of
  266. '.':
  267. begin
  268. if len>1 then
  269. begin
  270. while c in ['A'..'Z','a'..'z','0'..'9','_','.'] do
  271. begin
  272. inc(len);
  273. actasmpattern[len]:=c;
  274. c:=current_scanner.asmgetchar;
  275. end;
  276. actasmpattern[0]:=chr(len);
  277. end;
  278. {actasmpattern:=actasmpattern+c;
  279. c:=current_scanner.asmgetchar;
  280. if upcase(c) = 'W' then
  281. begin
  282. actasmpattern:=actasmpattern+c;
  283. c:=current_scanner.asmgetchar;
  284. end
  285. else
  286. internalerror(2010122301);}
  287. end
  288. end;
  289. {$endif ARM}
  290. {$ifdef aarch64}
  291. { b.cond }
  292. case c of
  293. '.':
  294. begin
  295. repeat
  296. actasmpattern:=actasmpattern+c;
  297. c:=current_scanner.asmgetchar;
  298. until not(c in ['a'..'z','A'..'Z']);
  299. end;
  300. end;
  301. {$endif aarch64}
  302. {$if defined(riscv32) or defined(riscv64)}
  303. {
  304. amo* instructions contain a postfix with size, and optionally memory ordering
  305. fence* can contain memory type identifier
  306. floating point instructions contain size, and optionally rounding mode
  307. }
  308. case c of
  309. '.':
  310. begin
  311. repeat
  312. actasmpattern:=actasmpattern+c;
  313. c:=current_scanner.asmgetchar;
  314. until not(c in ['a'..'z','A'..'Z','.']);
  315. end;
  316. end;
  317. {$endif riscv}
  318. { Opcode ? }
  319. If is_asmopcode(upper(actasmpattern)) then
  320. Begin
  321. uppervar(actasmpattern);
  322. exit;
  323. end;
  324. { End of assemblerblock ? }
  325. if upper(actasmpattern) = 'END' then
  326. begin
  327. actasmtoken:=AS_END;
  328. exit;
  329. end;
  330. message1(asmr_e_unknown_opcode,actasmpattern);
  331. actasmtoken:=AS_NONE;
  332. end
  333. else { else firsttoken }
  334. { Here we must handle all possible cases }
  335. begin
  336. case c of
  337. '.' : { possiblities : - local label reference , such as in jmp @local1 }
  338. { - field of object/record }
  339. { - directive. }
  340. begin
  341. if (prevasmtoken in [AS_ID,AS_RPAREN]) then
  342. begin
  343. c:=current_scanner.asmgetchar;
  344. actasmtoken:=AS_DOT;
  345. exit;
  346. end;
  347. actasmpattern:=c;
  348. c:=current_scanner.asmgetchar;
  349. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  350. begin
  351. actasmpattern:=actasmpattern + c;
  352. c:=current_scanner.asmgetchar;
  353. end;
  354. if is_asmdirective(actasmpattern) then
  355. exit;
  356. if is_targetdirective(actasmpattern) then
  357. begin
  358. actasmtoken:=AS_TARGET_DIRECTIVE;
  359. exit;
  360. end;
  361. { local label references and directives }
  362. { are case sensitive }
  363. actasmtoken:=AS_ID;
  364. exit;
  365. end;
  366. { identifier, register, prefix or directive }
  367. '_','A'..'Z','a'..'z':
  368. begin
  369. len:=0;
  370. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  371. begin
  372. inc(len);
  373. actasmpattern[len]:=c;
  374. c:=current_scanner.asmgetchar;
  375. end;
  376. actasmpattern[0]:=chr(len);
  377. uppervar(actasmpattern);
  378. {$ifdef x86}
  379. { only x86 architectures have instruction prefixes }
  380. { Opcode, can only be when the previous was a prefix }
  381. If is_prefix(actopcode) and is_asmopcode(actasmpattern) then
  382. Begin
  383. uppervar(actasmpattern);
  384. exit;
  385. end;
  386. {$endif x86}
  387. { check for end which is a reserved word unlike the opcodes }
  388. if actasmpattern = 'END' then
  389. Begin
  390. actasmtoken:=AS_END;
  391. exit;
  392. end;
  393. if actasmpattern = 'TYPE' then
  394. Begin
  395. actasmtoken:=AS_TYPE;
  396. exit;
  397. end;
  398. if actasmpattern = 'SIZEOF' then
  399. Begin
  400. actasmtoken:=AS_SIZEOF;
  401. exit;
  402. end;
  403. if actasmpattern = 'VMTOFFSET' then
  404. Begin
  405. actasmtoken:=AS_VMTOFFSET;
  406. exit;
  407. end;
  408. if is_register(actasmpattern) then
  409. begin
  410. actasmtoken:=AS_REGISTER;
  411. exit;
  412. end;
  413. { if next is a '.' and this is a unitsym then we also need to
  414. parse the identifier }
  415. if (c='.') then
  416. begin
  417. searchsym(actasmpattern,srsym,srsymtable);
  418. if assigned(srsym) and
  419. (srsym.typ=unitsym) and
  420. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  421. srsym.owner.iscurrentunit then
  422. begin
  423. actasmpattern:=actasmpattern+c;
  424. c:=current_scanner.asmgetchar;
  425. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  426. begin
  427. actasmpattern:=actasmpattern + upcase(c);
  428. c:=current_scanner.asmgetchar;
  429. end;
  430. end;
  431. end;
  432. actasmtoken:=AS_ID;
  433. exit;
  434. end;
  435. '%' : { register or modulo }
  436. handlepercent;
  437. '1'..'9': { integer number }
  438. begin
  439. len:=0;
  440. while c in ['0'..'9'] do
  441. Begin
  442. inc(len);
  443. actasmpattern[len]:=c;
  444. c:=current_scanner.asmgetchar;
  445. end;
  446. actasmpattern[0]:=chr(len);
  447. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  448. actasmtoken:=AS_INTNUM;
  449. exit;
  450. end;
  451. '0' : { octal,hexa,real or binary number. }
  452. begin
  453. actasmpattern:=c;
  454. c:=current_scanner.asmgetchar;
  455. case upcase(c) of
  456. 'B': { binary }
  457. Begin
  458. c:=current_scanner.asmgetchar;
  459. while c in ['0','1'] do
  460. Begin
  461. actasmpattern:=actasmpattern + c;
  462. c:=current_scanner.asmgetchar;
  463. end;
  464. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  465. actasmtoken:=AS_INTNUM;
  466. exit;
  467. end;
  468. 'D': { real }
  469. Begin
  470. c:=current_scanner.asmgetchar;
  471. { get ridd of the 0d }
  472. if (c in ['+','-']) then
  473. begin
  474. actasmpattern:=c;
  475. c:=current_scanner.asmgetchar;
  476. end
  477. else
  478. actasmpattern:='';
  479. while c in ['0'..'9'] do
  480. Begin
  481. actasmpattern:=actasmpattern + c;
  482. c:=current_scanner.asmgetchar;
  483. end;
  484. if c='.' then
  485. begin
  486. actasmpattern:=actasmpattern + c;
  487. c:=current_scanner.asmgetchar;
  488. while c in ['0'..'9'] do
  489. Begin
  490. actasmpattern:=actasmpattern + c;
  491. c:=current_scanner.asmgetchar;
  492. end;
  493. if upcase(c) = 'E' then
  494. begin
  495. actasmpattern:=actasmpattern + c;
  496. c:=current_scanner.asmgetchar;
  497. if (c in ['+','-']) then
  498. begin
  499. actasmpattern:=actasmpattern + c;
  500. c:=current_scanner.asmgetchar;
  501. end;
  502. while c in ['0'..'9'] do
  503. Begin
  504. actasmpattern:=actasmpattern + c;
  505. c:=current_scanner.asmgetchar;
  506. end;
  507. end;
  508. actasmtoken:=AS_REALNUM;
  509. exit;
  510. end
  511. else
  512. begin
  513. Message1(asmr_e_invalid_float_const,actasmpattern+c);
  514. actasmtoken:=AS_NONE;
  515. end;
  516. end;
  517. 'X': { hexadecimal }
  518. Begin
  519. c:=current_scanner.asmgetchar;
  520. while c in ['0'..'9','a'..'f','A'..'F'] do
  521. Begin
  522. actasmpattern:=actasmpattern + c;
  523. c:=current_scanner.asmgetchar;
  524. end;
  525. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  526. actasmtoken:=AS_INTNUM;
  527. exit;
  528. end;
  529. '1'..'7': { octal }
  530. begin
  531. actasmpattern:=actasmpattern + c;
  532. while c in ['0'..'7'] do
  533. Begin
  534. actasmpattern:=actasmpattern + c;
  535. c:=current_scanner.asmgetchar;
  536. end;
  537. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  538. actasmtoken:=AS_INTNUM;
  539. exit;
  540. end;
  541. else { octal number zero value...}
  542. Begin
  543. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  544. actasmtoken:=AS_INTNUM;
  545. exit;
  546. end;
  547. end; { end case }
  548. end;
  549. '&' :
  550. begin
  551. c:=current_scanner.asmgetchar;
  552. actasmtoken:=AS_AND;
  553. end;
  554. '''' : { char }
  555. begin
  556. actasmpattern:='';
  557. repeat
  558. c:=current_scanner.asmgetchar;
  559. case c of
  560. '\' :
  561. begin
  562. { copy also the next char so \" is parsed correctly }
  563. actasmpattern:=actasmpattern+c;
  564. c:=current_scanner.asmgetchar;
  565. actasmpattern:=actasmpattern+c;
  566. end;
  567. '''' :
  568. begin
  569. c:=current_scanner.asmgetchar;
  570. break;
  571. end;
  572. #10,#13:
  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. '"' : { string }
  583. begin
  584. actasmpattern:='';
  585. repeat
  586. c:=current_scanner.asmgetchar;
  587. case c of
  588. '\' :
  589. begin
  590. { copy also the next char so \" is parsed correctly }
  591. actasmpattern:=actasmpattern+c;
  592. c:=current_scanner.asmgetchar;
  593. actasmpattern:=actasmpattern+c;
  594. end;
  595. '"' :
  596. begin
  597. c:=current_scanner.asmgetchar;
  598. break;
  599. end;
  600. #10,#13:
  601. Message(scan_f_string_exceeds_line);
  602. else
  603. actasmpattern:=actasmpattern+c;
  604. end;
  605. until false;
  606. actasmpattern:=EscapeToPascal(actasmpattern);
  607. actasmtoken:=AS_STRING;
  608. exit;
  609. end;
  610. '$' :
  611. begin
  612. handledollar;
  613. exit;
  614. end;
  615. '#' :
  616. begin
  617. actasmtoken:=AS_HASH;
  618. c:=current_scanner.asmgetchar;
  619. exit;
  620. end;
  621. '[' :
  622. begin
  623. actasmtoken:=AS_LBRACKET;
  624. c:=current_scanner.asmgetchar;
  625. exit;
  626. end;
  627. ']' :
  628. begin
  629. actasmtoken:=AS_RBRACKET;
  630. c:=current_scanner.asmgetchar;
  631. exit;
  632. end;
  633. '{' :
  634. begin
  635. {$ifdef arm}
  636. // the arm assembler uses { ... } for register sets
  637. // but compiler directives {$... } are still allowed
  638. c:=current_scanner.asmgetchar;
  639. if c<>'$' then
  640. actasmtoken:=AS_LSBRACKET
  641. else
  642. begin
  643. current_scanner.skipcomment(false);
  644. GetToken;
  645. end;
  646. {$else arm}
  647. current_scanner.skipcomment(true);
  648. GetToken;
  649. {$endif arm}
  650. exit;
  651. end;
  652. {$ifdef arm}
  653. '}' :
  654. begin
  655. actasmtoken:=AS_RSBRACKET;
  656. c:=current_scanner.asmgetchar;
  657. exit;
  658. end;
  659. '=' :
  660. begin
  661. actasmtoken:=AS_EQUAL;
  662. c:=current_scanner.asmgetchar;
  663. exit;
  664. end;
  665. {$endif arm}
  666. ',' :
  667. begin
  668. actasmtoken:=AS_COMMA;
  669. c:=current_scanner.asmgetchar;
  670. exit;
  671. end;
  672. '<' :
  673. begin
  674. actasmtoken:=AS_SHL;
  675. c:=current_scanner.asmgetchar;
  676. if c = '<' then
  677. c:=current_scanner.asmgetchar;
  678. exit;
  679. end;
  680. '>' :
  681. begin
  682. actasmtoken:=AS_SHL;
  683. c:=current_scanner.asmgetchar;
  684. if c = '>' then
  685. c:=current_scanner.asmgetchar;
  686. exit;
  687. end;
  688. '|' :
  689. begin
  690. actasmtoken:=AS_OR;
  691. c:=current_scanner.asmgetchar;
  692. exit;
  693. end;
  694. '^' :
  695. begin
  696. actasmtoken:=AS_XOR;
  697. c:=current_scanner.asmgetchar;
  698. exit;
  699. end;
  700. '(' :
  701. begin
  702. c:=current_scanner.asmgetchar;
  703. if c='*' then
  704. begin
  705. current_scanner.skipoldtpcomment(true);
  706. GetToken;
  707. end
  708. else
  709. actasmtoken:=AS_LPAREN;
  710. exit;
  711. end;
  712. ')' :
  713. begin
  714. actasmtoken:=AS_RPAREN;
  715. c:=current_scanner.asmgetchar;
  716. exit;
  717. end;
  718. ':' :
  719. begin
  720. actasmtoken:=AS_COLON;
  721. c:=current_scanner.asmgetchar;
  722. exit;
  723. end;
  724. '+' :
  725. begin
  726. actasmtoken:=AS_PLUS;
  727. c:=current_scanner.asmgetchar;
  728. exit;
  729. end;
  730. '-' :
  731. begin
  732. actasmtoken:=AS_MINUS;
  733. c:=current_scanner.asmgetchar;
  734. exit;
  735. end;
  736. '*' :
  737. begin
  738. actasmtoken:=AS_STAR;
  739. c:=current_scanner.asmgetchar;
  740. exit;
  741. end;
  742. '/' :
  743. begin
  744. c:=current_scanner.asmgetchar;
  745. if c='/' then
  746. begin
  747. current_scanner.skipdelphicomment;
  748. GetToken;
  749. end
  750. else
  751. actasmtoken:=AS_SLASH;
  752. exit;
  753. end;
  754. '!', '~' :
  755. begin
  756. actasmtoken:=AS_NOT;
  757. c:=current_scanner.asmgetchar;
  758. exit;
  759. end;
  760. '@' :
  761. begin
  762. actasmtoken:=AS_AT;
  763. c:=current_scanner.asmgetchar;
  764. exit;
  765. end;
  766. #13,#10:
  767. begin
  768. current_scanner.linebreak;
  769. c:=current_scanner.asmgetchar;
  770. firsttoken:=TRUE;
  771. actasmtoken:=AS_SEPARATOR;
  772. exit;
  773. end;
  774. ';' :
  775. begin
  776. c:=current_scanner.asmgetchar;
  777. firsttoken:=TRUE;
  778. actasmtoken:=AS_SEPARATOR;
  779. exit;
  780. end;
  781. else
  782. current_scanner.illegal_char(c);
  783. end;
  784. end;
  785. end;
  786. function tattreader.consume(t : tasmtoken):boolean;
  787. begin
  788. Consume:=true;
  789. if t<>actasmtoken then
  790. begin
  791. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  792. Consume:=false;
  793. end;
  794. repeat
  795. gettoken;
  796. until actasmtoken<>AS_NONE;
  797. end;
  798. procedure tattreader.RecoverConsume(allowcomma:boolean);
  799. begin
  800. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  801. begin
  802. if allowcomma and (actasmtoken=AS_COMMA) then
  803. break;
  804. Consume(actasmtoken);
  805. end;
  806. end;
  807. Procedure tattreader.BuildConstant(constsize: byte);
  808. var
  809. asmsymtyp : TAsmSymType;
  810. asmsym,
  811. expr: string;
  812. value : tcgint;
  813. Begin
  814. Repeat
  815. Case actasmtoken of
  816. AS_STRING:
  817. Begin
  818. expr:=actasmpattern;
  819. if length(expr) > 1 then
  820. Message(asmr_e_string_not_allowed_as_const);
  821. Consume(AS_STRING);
  822. Case actasmtoken of
  823. AS_COMMA: Consume(AS_COMMA);
  824. AS_END,
  825. AS_SEPARATOR: ;
  826. else
  827. Message(asmr_e_invalid_string_expression);
  828. end; { end case }
  829. ConcatString(curlist,expr);
  830. end;
  831. AS_INTNUM,
  832. AS_PLUS,
  833. AS_MINUS,
  834. AS_LPAREN,
  835. AS_TYPE,
  836. AS_SIZEOF,
  837. AS_NOT,
  838. AS_VMTOFFSET,
  839. AS_ID :
  840. Begin
  841. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  842. if asmsym<>'' then
  843. begin
  844. if constsize<>sizeof(pint) then
  845. Message(asmr_w_32bit_const_for_address);
  846. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value,constsize,true)
  847. end
  848. else
  849. ConcatConstant(curlist,value,constsize);
  850. end;
  851. AS_COMMA:
  852. Consume(AS_COMMA);
  853. AS_END,
  854. AS_SEPARATOR:
  855. break;
  856. else
  857. begin
  858. Message(asmr_e_syn_constant);
  859. RecoverConsume(false);
  860. end
  861. end; { end case }
  862. Until false;
  863. end;
  864. Procedure tattreader.BuildRealConstant(typ : tfloattype);
  865. var
  866. expr : string;
  867. r : bestreal;
  868. code : integer;
  869. negativ : boolean;
  870. errorflag: boolean;
  871. Begin
  872. errorflag:=FALSE;
  873. Repeat
  874. negativ:=false;
  875. expr:='';
  876. if actasmtoken=AS_PLUS then
  877. Consume(AS_PLUS)
  878. else
  879. if actasmtoken=AS_MINUS then
  880. begin
  881. negativ:=true;
  882. consume(AS_MINUS);
  883. end;
  884. Case actasmtoken of
  885. AS_INTNUM:
  886. Begin
  887. expr:=actasmpattern;
  888. Consume(AS_INTNUM);
  889. if negativ then
  890. expr:='-'+expr;
  891. val(expr,r,code);
  892. if code<>0 then
  893. Begin
  894. r:=0;
  895. Message(asmr_e_invalid_float_expr);
  896. End;
  897. ConcatRealConstant(curlist,r,typ);
  898. end;
  899. AS_REALNUM:
  900. Begin
  901. expr:=actasmpattern;
  902. Consume(AS_REALNUM);
  903. { in ATT syntax you have 0d in front of the real }
  904. { should this be forced ? yes i think so, as to }
  905. { conform to gas as much as possible. }
  906. if (expr[1]='0') and (upper(expr[2])='D') then
  907. Delete(expr,1,2);
  908. if negativ then
  909. expr:='-'+expr;
  910. val(expr,r,code);
  911. if code<>0 then
  912. Begin
  913. r:=0;
  914. Message(asmr_e_invalid_float_expr);
  915. End;
  916. ConcatRealConstant(curlist,r,typ);
  917. end;
  918. AS_COMMA:
  919. begin
  920. Consume(AS_COMMA);
  921. end;
  922. AS_END,
  923. AS_SEPARATOR:
  924. begin
  925. break;
  926. end;
  927. else
  928. Begin
  929. Consume(actasmtoken);
  930. if not errorflag then
  931. Message(asmr_e_invalid_float_expr);
  932. errorflag:=TRUE;
  933. end;
  934. end;
  935. Until false;
  936. end;
  937. Procedure tattreader.BuildStringConstant(asciiz: boolean);
  938. var
  939. expr: string;
  940. errorflag : boolean;
  941. Begin
  942. errorflag:=FALSE;
  943. Repeat
  944. Case actasmtoken of
  945. AS_STRING:
  946. Begin
  947. expr:=actasmpattern;
  948. if asciiz then
  949. expr:=expr+#0;
  950. ConcatString(curlist,expr);
  951. Consume(AS_STRING);
  952. end;
  953. AS_COMMA:
  954. begin
  955. Consume(AS_COMMA);
  956. end;
  957. AS_END,
  958. AS_SEPARATOR:
  959. begin
  960. break;
  961. end;
  962. else
  963. Begin
  964. Consume(actasmtoken);
  965. if not errorflag then
  966. Message(asmr_e_invalid_string_expression);
  967. errorflag:=TRUE;
  968. end;
  969. end;
  970. Until false;
  971. end;
  972. Function tattreader.Assemble: tlinkedlist;
  973. Var
  974. hl : tasmlabel;
  975. commname,
  976. symname,
  977. symval ,sectionname: string;
  978. lasTSec : TAsmSectiontype;
  979. l1,
  980. l2,
  981. symofs : tcgint;
  982. symtyp : TAsmsymtype;
  983. section : tai_section;
  984. secflags : TSectionFlags;
  985. secprogbits : TSectionProgbits;
  986. Begin
  987. Message1(asmr_d_start_reading,'GNU AS');
  988. firsttoken:=TRUE;
  989. { sets up all opcode and register tables in uppercase }
  990. if not _asmsorted then
  991. Begin
  992. SetupTables;
  993. _asmsorted:=TRUE;
  994. end;
  995. curlist:=TAsmList.Create;
  996. { we might need to know which parameters are passed in registers }
  997. if not parse_generic then
  998. current_procinfo.generate_parameter_info;
  999. lasTSec:=sec_code;
  1000. { start tokenizer }
  1001. gettoken;
  1002. { main loop }
  1003. repeat
  1004. case actasmtoken of
  1005. AS_LLABEL:
  1006. Begin
  1007. if CreateLocalLabel(actasmpattern,hl,true) then
  1008. ConcatLabel(curlist,hl);
  1009. Consume(AS_LLABEL);
  1010. end;
  1011. AS_LABEL:
  1012. Begin
  1013. if SearchLabel(upper(actasmpattern),hl,true) then
  1014. begin
  1015. if hl.is_public then
  1016. ConcatPublic(curlist,actasmpattern);
  1017. ConcatLabel(curlist,hl);
  1018. end
  1019. else
  1020. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1021. Consume(AS_LABEL);
  1022. end;
  1023. AS_DW:
  1024. Begin
  1025. Consume(AS_DW);
  1026. BuildConstant(2);
  1027. end;
  1028. AS_DATA:
  1029. Begin
  1030. new_section(curList,sec_data,lower(current_procinfo.procdef.mangledname),0);
  1031. lasTSec:=sec_data;
  1032. Consume(AS_DATA);
  1033. end;
  1034. AS_TEXT:
  1035. Begin
  1036. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1037. lasTSec:=sec_code;
  1038. Consume(AS_TEXT);
  1039. end;
  1040. AS_INIT:
  1041. Begin
  1042. new_section(curList,sec_init,lower(current_procinfo.procdef.mangledname),0);
  1043. lasTSec:=sec_init;
  1044. Consume(AS_INIT);
  1045. end;
  1046. AS_FINI:
  1047. Begin
  1048. new_section(curList,sec_fini,lower(current_procinfo.procdef.mangledname),0);
  1049. lasTSec:=sec_fini;
  1050. Consume(AS_FINI);
  1051. end;
  1052. AS_DB:
  1053. Begin
  1054. Consume(AS_DB);
  1055. BuildConstant(1);
  1056. end;
  1057. AS_DD:
  1058. Begin
  1059. Consume(AS_DD);
  1060. BuildConstant(4);
  1061. end;
  1062. AS_DQ:
  1063. Begin
  1064. Consume(AS_DQ);
  1065. {$ifdef cpu64bitaddr}
  1066. BuildConstant(8);
  1067. {$else cpu64bitaddr}
  1068. BuildRealConstant(s64comp);
  1069. {$endif cpu64bitaddr}
  1070. end;
  1071. AS_SINGLE:
  1072. Begin
  1073. Consume(AS_SINGLE);
  1074. BuildRealConstant(s32real);
  1075. end;
  1076. AS_DOUBLE:
  1077. Begin
  1078. Consume(AS_DOUBLE);
  1079. BuildRealConstant(s64real);
  1080. end;
  1081. AS_EXTENDED:
  1082. Begin
  1083. Consume(AS_EXTENDED);
  1084. BuildRealConstant(s80real);
  1085. end;
  1086. AS_CEXTENDED:
  1087. Begin
  1088. Consume(AS_CEXTENDED);
  1089. BuildRealConstant(sc80real);
  1090. end;
  1091. AS_GLOBAL:
  1092. Begin
  1093. Consume(AS_GLOBAL);
  1094. if actasmtoken=AS_ID then
  1095. ConcatPublic(curlist,actasmpattern);
  1096. Consume(AS_ID);
  1097. if actasmtoken<>AS_SEPARATOR then
  1098. Consume(AS_SEPARATOR);
  1099. end;
  1100. AS_ALIGN:
  1101. Begin
  1102. Consume(AS_ALIGN);
  1103. l1:=BuildConstExpression(false,false);
  1104. if (target_info.system in [system_i386_GO32V2]) then
  1105. begin
  1106. l2:=1;
  1107. if (l1>=0) and (l1<=16) then
  1108. while (l1>0) do
  1109. begin
  1110. l2:=2*l2;
  1111. dec(l1);
  1112. end;
  1113. l1:=l2;
  1114. end;
  1115. ConcatAlign(curlist,l1);
  1116. Message(asmr_n_align_is_target_specific);
  1117. if actasmtoken<>AS_SEPARATOR then
  1118. Consume(AS_SEPARATOR);
  1119. end;
  1120. AS_BALIGN:
  1121. Begin
  1122. Consume(AS_BALIGN);
  1123. ConcatAlign(curlist,BuildConstExpression(false,false));
  1124. if actasmtoken<>AS_SEPARATOR then
  1125. Consume(AS_SEPARATOR);
  1126. end;
  1127. AS_P2ALIGN:
  1128. Begin
  1129. Consume(AS_P2ALIGN);
  1130. l1:=BuildConstExpression(false,false);
  1131. l2:=1;
  1132. if (l1>=0) and (l1<=16) then
  1133. while (l1>0) do
  1134. begin
  1135. l2:=2*l2;
  1136. dec(l1);
  1137. end;
  1138. l1:=l2;
  1139. ConcatAlign(curlist,l1);
  1140. if actasmtoken<>AS_SEPARATOR then
  1141. Consume(AS_SEPARATOR);
  1142. end;
  1143. AS_ASCIIZ:
  1144. Begin
  1145. Consume(AS_ASCIIZ);
  1146. BuildStringConstant(TRUE);
  1147. end;
  1148. AS_ASCII:
  1149. Begin
  1150. Consume(AS_ASCII);
  1151. BuildStringConstant(FALSE);
  1152. end;
  1153. AS_LCOMM:
  1154. Begin
  1155. Consume(AS_LCOMM);
  1156. commname:=actasmpattern;
  1157. Consume(AS_ID);
  1158. Consume(AS_COMMA);
  1159. symofs:=BuildConstExpression(false,false);
  1160. curList.concat(Tai_datablock.Create(commname,symofs,carraydef.getreusable(u8inttype,symofs)));
  1161. if actasmtoken<>AS_SEPARATOR then
  1162. Consume(AS_SEPARATOR);
  1163. end;
  1164. AS_COMM:
  1165. Begin
  1166. Consume(AS_COMM);
  1167. commname:=actasmpattern;
  1168. Consume(AS_ID);
  1169. Consume(AS_COMMA);
  1170. symofs:=BuildConstExpression(false,false);
  1171. curList.concat(Tai_datablock.Create_global(commname,symofs,carraydef.getreusable(u8inttype,symofs)));
  1172. if actasmtoken<>AS_SEPARATOR then
  1173. Consume(AS_SEPARATOR);
  1174. end;
  1175. AS_OPCODE:
  1176. Begin
  1177. HandleOpCode;
  1178. end;
  1179. AS_SEPARATOR:
  1180. Begin
  1181. Consume(AS_SEPARATOR);
  1182. end;
  1183. AS_RVA:
  1184. begin
  1185. { .rva generally applies to systems with COFF output format,
  1186. not just Windows. }
  1187. if not (target_info.system in systems_all_windows) then
  1188. Message1(asmr_e_unsupported_directive,token2str[AS_RVA]);
  1189. Consume(AS_RVA);
  1190. BuildRva;
  1191. end;
  1192. AS_SET:
  1193. begin
  1194. Consume(AS_SET);
  1195. BuildConstSymbolExpression(true,false,false, symofs,symname,symtyp);
  1196. Consume(AS_COMMA);
  1197. BuildConstSymbolExpression(true,false,false, symofs,symval,symtyp);
  1198. curList.concat(tai_symbolpair.create(spk_set,symname,symval));
  1199. end;
  1200. AS_WEAK:
  1201. begin
  1202. Consume(AS_WEAK);
  1203. BuildConstSymbolExpression(true,false,false, l1,symname,symtyp);
  1204. { ideally, we should look up the symbol here (or later) and
  1205. depending on whether it's external or net, generate
  1206. asd_weak_reference or asd_weak_definition -- this is different
  1207. on some targets) }
  1208. curList.concat(tai_directive.create(asd_weak_definition,symname));
  1209. end;
  1210. AS_SECTION:
  1211. begin
  1212. Consume(AS_SECTION);
  1213. sectionname:=actasmpattern;
  1214. secflags:=SF_None;
  1215. secprogbits:=SPB_None;
  1216. Consume(AS_STRING);
  1217. if actasmtoken=AS_COMMA then
  1218. begin
  1219. Consume(AS_COMMA);
  1220. if actasmtoken=AS_STRING then
  1221. begin
  1222. case actasmpattern of
  1223. 'a':
  1224. secflags:=SF_A;
  1225. 'w':
  1226. secflags:=SF_W;
  1227. 'x':
  1228. secflags:=SF_X;
  1229. '':
  1230. secflags:=SF_None;
  1231. else
  1232. Message(asmr_e_syntax_error);
  1233. end;
  1234. Consume(AS_STRING);
  1235. if actasmtoken=AS_COMMA then
  1236. begin
  1237. Consume(AS_COMMA);
  1238. if (actasmtoken=AS_MOD) or (actasmtoken=AS_AT) then
  1239. begin
  1240. Consume(actasmtoken);
  1241. if actasmtoken=AS_ID then
  1242. begin
  1243. case actasmpattern of
  1244. 'PROGBITS':
  1245. secprogbits:=SPB_PROGBITS;
  1246. 'NOBITS':
  1247. secprogbits:=SPB_NOBITS;
  1248. else
  1249. Message(asmr_e_syntax_error);
  1250. end;
  1251. Consume(AS_ID);
  1252. end
  1253. else
  1254. Message(asmr_e_syntax_error);
  1255. end
  1256. else
  1257. Message(asmr_e_syntax_error);
  1258. end;
  1259. end
  1260. else
  1261. Message(asmr_e_syntax_error);
  1262. end;
  1263. //curList.concat(tai_section.create(sec_user, actasmpattern, 0));
  1264. section:=new_section(curlist, sec_user, sectionname, 0);
  1265. section.secflags:=secflags;
  1266. section.secprogbits:=secprogbits;
  1267. end;
  1268. AS_TARGET_DIRECTIVE:
  1269. HandleTargetDirective;
  1270. AS_END:
  1271. begin
  1272. break; { end assembly block }
  1273. end;
  1274. else
  1275. Begin
  1276. Message(asmr_e_syntax_error);
  1277. RecoverConsume(false);
  1278. end;
  1279. end;
  1280. until false;
  1281. { check that all referenced local labels are defined }
  1282. checklocallabels;
  1283. { are we back in the code section? }
  1284. if lasTSec<>sec_code then
  1285. begin
  1286. Message(asmr_w_assembler_code_not_returned_to_text);
  1287. new_section(curList,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1288. end;
  1289. { Return the list in an asmnode }
  1290. assemble:=curlist;
  1291. Message1(asmr_d_finish_reading,'GNU AS');
  1292. end;
  1293. {*****************************************************************************
  1294. Parsing Helpers
  1295. *****************************************************************************}
  1296. Procedure tattreader.BuildRecordOffsetSize(const expr: string;var offset:tcgint;var size:tcgint; var mangledname: string; needvmtofs: boolean);
  1297. { Description: This routine builds up a record offset after a AS_DOT }
  1298. { token is encountered. }
  1299. { On entry actasmtoken should be equal to AS_DOT }
  1300. var
  1301. s : string;
  1302. hastypecast: boolean;
  1303. Begin
  1304. offset:=0;
  1305. size:=0;
  1306. s:=expr;
  1307. while (actasmtoken=AS_DOT) do
  1308. begin
  1309. Consume(AS_DOT);
  1310. { a record field could have the same name as a register }
  1311. if actasmtoken in [AS_ID,AS_REGISTER] then
  1312. begin
  1313. s:=s+'.'+actasmpattern;
  1314. consume(actasmtoken)
  1315. end
  1316. else
  1317. begin
  1318. Consume(AS_ID);
  1319. RecoverConsume(true);
  1320. break;
  1321. end;
  1322. end;
  1323. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs,hastypecast) then
  1324. Message(asmr_e_building_record_offset);
  1325. end;
  1326. procedure tattreader.BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:tcgint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  1327. var
  1328. hssymtyp : TAsmSymType;
  1329. hs,tempstr,expr,mangledname : string;
  1330. parenlevel : longint;
  1331. l,k : tcgint;
  1332. errorflag : boolean;
  1333. prevtok : tasmtoken;
  1334. sym : tsym;
  1335. srsymtable : TSymtable;
  1336. hl : tasmlabel;
  1337. Begin
  1338. asmsym:='';
  1339. asmsymtyp:=AT_DATA;
  1340. value:=0;
  1341. errorflag:=FALSE;
  1342. tempstr:='';
  1343. expr:='';
  1344. parenlevel:=0;
  1345. Repeat
  1346. Case actasmtoken of
  1347. AS_LPAREN:
  1348. Begin
  1349. { Exit if ref? }
  1350. if allowref and (prevasmtoken in [AS_INTNUM,AS_ID]) then
  1351. break;
  1352. Consume(AS_LPAREN);
  1353. expr:=expr + '(';
  1354. inc(parenlevel);
  1355. end;
  1356. AS_RBRACKET:
  1357. begin
  1358. if betweenbracket then
  1359. break;
  1360. { write error only once. }
  1361. if not errorflag then
  1362. Message(asmr_e_invalid_constant_expression);
  1363. { consume tokens until we find COMMA or SEPARATOR }
  1364. Consume(actasmtoken);
  1365. errorflag:=TRUE;
  1366. end;
  1367. AS_RPAREN:
  1368. Begin
  1369. { end of ref ? }
  1370. if (parenlevel=0) and betweenbracket then
  1371. break;
  1372. Consume(AS_RPAREN);
  1373. expr:=expr + ')';
  1374. dec(parenlevel);
  1375. end;
  1376. AS_SHL:
  1377. Begin
  1378. Consume(AS_SHL);
  1379. expr:=expr + '<';
  1380. end;
  1381. AS_SHR:
  1382. Begin
  1383. Consume(AS_SHR);
  1384. expr:=expr + '>';
  1385. end;
  1386. AS_SLASH:
  1387. Begin
  1388. Consume(AS_SLASH);
  1389. expr:=expr + '/';
  1390. end;
  1391. AS_MOD:
  1392. Begin
  1393. Consume(AS_MOD);
  1394. expr:=expr + '%';
  1395. end;
  1396. AS_STAR:
  1397. Begin
  1398. Consume(AS_STAR);
  1399. expr:=expr + '*';
  1400. end;
  1401. AS_PLUS:
  1402. Begin
  1403. Consume(AS_PLUS);
  1404. expr:=expr + '+';
  1405. end;
  1406. AS_MINUS:
  1407. Begin
  1408. Consume(AS_MINUS);
  1409. expr:=expr + '-';
  1410. end;
  1411. AS_AND:
  1412. Begin
  1413. Consume(AS_AND);
  1414. expr:=expr + '&';
  1415. end;
  1416. AS_NOT:
  1417. Begin
  1418. Consume(AS_NOT);
  1419. expr:=expr + '~';
  1420. end;
  1421. AS_XOR:
  1422. Begin
  1423. Consume(AS_XOR);
  1424. expr:=expr + '^';
  1425. end;
  1426. AS_OR:
  1427. Begin
  1428. Consume(AS_OR);
  1429. expr:=expr + '|';
  1430. end;
  1431. AS_INTNUM:
  1432. Begin
  1433. expr:=expr + actasmpattern;
  1434. Consume(AS_INTNUM);
  1435. end;
  1436. AS_DOLLAR:
  1437. begin
  1438. Consume(AS_DOLLAR);
  1439. if actasmtoken<>AS_ID then
  1440. Message(asmr_e_dollar_without_identifier);
  1441. end;
  1442. AS_STRING:
  1443. Begin
  1444. l:=0;
  1445. case Length(actasmpattern) of
  1446. 1 :
  1447. l:=ord(actasmpattern[1]);
  1448. 2 :
  1449. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  1450. 3 :
  1451. l:=ord(actasmpattern[3]) +
  1452. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  1453. 4 :
  1454. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1455. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1456. else
  1457. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1458. end;
  1459. str(l, tempstr);
  1460. expr:=expr + tempstr;
  1461. Consume(AS_STRING);
  1462. end;
  1463. AS_SIZEOF,
  1464. AS_TYPE:
  1465. begin
  1466. l:=0;
  1467. Consume(actasmtoken);
  1468. if actasmtoken<>AS_ID then
  1469. Message(asmr_e_type_without_identifier)
  1470. else
  1471. begin
  1472. tempstr:=actasmpattern;
  1473. Consume(AS_ID);
  1474. if actasmtoken=AS_DOT then
  1475. begin
  1476. BuildRecordOffsetSize(tempstr,k,l,mangledname,false);
  1477. if mangledname<>'' then
  1478. Message(asmr_e_wrong_sym_type);
  1479. end
  1480. else
  1481. begin
  1482. searchsym(tempstr,sym,srsymtable);
  1483. if assigned(sym) then
  1484. begin
  1485. case sym.typ of
  1486. staticvarsym,
  1487. localvarsym,
  1488. paravarsym :
  1489. l:=tabstractvarsym(sym).getsize;
  1490. typesym :
  1491. l:=ttypesym(sym).typedef.size;
  1492. else
  1493. Message(asmr_e_wrong_sym_type);
  1494. end;
  1495. end
  1496. else
  1497. Message1(sym_e_unknown_id,tempstr);
  1498. end;
  1499. end;
  1500. str(l, tempstr);
  1501. expr:=expr + tempstr;
  1502. end;
  1503. AS_VMTOFFSET:
  1504. begin
  1505. Consume(actasmtoken);
  1506. if actasmtoken<>AS_ID then
  1507. Message(asmr_e_type_without_identifier)
  1508. else
  1509. begin
  1510. tempstr:=actasmpattern;
  1511. consume(AS_ID);
  1512. BuildRecordOffsetSize(tempstr,k,l,mangledname,true);
  1513. if (mangledname <> '') then
  1514. Message(asmr_e_wrong_sym_type);
  1515. str(k,tempstr);
  1516. expr := expr + tempstr;
  1517. end
  1518. end;
  1519. AS_ID:
  1520. Begin
  1521. hs:='';
  1522. hssymtyp:=AT_DATA;
  1523. tempstr:=actasmpattern;
  1524. prevtok:=prevasmtoken;
  1525. consume(AS_ID);
  1526. if SearchIConstant(tempstr,l) then
  1527. begin
  1528. str(l, tempstr);
  1529. expr:=expr + tempstr;
  1530. end
  1531. else
  1532. begin
  1533. if is_locallabel(tempstr) then
  1534. begin
  1535. CreateLocalLabel(tempstr,hl,false);
  1536. hs:=hl.name;
  1537. hssymtyp:=AT_LABEL;
  1538. end
  1539. else
  1540. if SearchLabel(tempstr,hl,false) then
  1541. begin
  1542. hs:=hl.name;
  1543. hssymtyp:=AT_FUNCTION;
  1544. end
  1545. else
  1546. begin
  1547. searchsym(tempstr,sym,srsymtable);
  1548. if assigned(sym) then
  1549. begin
  1550. case sym.typ of
  1551. staticvarsym :
  1552. begin
  1553. { we always assume in asm statements that }
  1554. { that the variable is valid. }
  1555. tabstractvarsym(sym).varstate:=vs_readwritten;
  1556. inc(tabstractvarsym(sym).refs);
  1557. { variable can't be placed in a register }
  1558. tabstractvarsym(sym).varregable:=vr_none;
  1559. { and anything may happen with its address }
  1560. tabstractvarsym(sym).addr_taken:=true;
  1561. hs:=tstaticvarsym(sym).mangledname;
  1562. end;
  1563. localvarsym,
  1564. paravarsym :
  1565. Message(asmr_e_no_local_or_para_allowed);
  1566. procsym :
  1567. begin
  1568. if Tprocsym(sym).ProcdefList.Count>1 then
  1569. Message(asmr_w_calling_overload_func);
  1570. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1571. hssymtyp:=AT_FUNCTION;
  1572. end;
  1573. typesym :
  1574. begin
  1575. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1576. Message(asmr_e_wrong_sym_type);
  1577. end;
  1578. else
  1579. Message(asmr_e_wrong_sym_type);
  1580. end;
  1581. end
  1582. else
  1583. Message1(sym_e_unknown_id,tempstr);
  1584. end;
  1585. { symbol found? }
  1586. if hs<>'' then
  1587. begin
  1588. if needofs and (prevtok<>AS_DOLLAR) then
  1589. Message(asmr_e_need_dollar);
  1590. if asmsym='' then
  1591. begin
  1592. asmsym:=hs;
  1593. asmsymtyp:=hssymtyp;
  1594. end
  1595. else
  1596. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1597. if (expr='') or (expr[length(expr)]='+') then
  1598. begin
  1599. { don't remove the + if there could be a record field }
  1600. if actasmtoken<>AS_DOT then
  1601. delete(expr,length(expr),1);
  1602. end
  1603. else
  1604. Message(asmr_e_only_add_relocatable_symbol);
  1605. end;
  1606. if actasmtoken=AS_DOT then
  1607. begin
  1608. BuildRecordOffsetSize(tempstr,l,k,hs,false);
  1609. if (hs<>'') then
  1610. hssymtyp:=AT_FUNCTION
  1611. else
  1612. begin
  1613. str(l, tempstr);
  1614. expr:=expr + tempstr;
  1615. end
  1616. end
  1617. else
  1618. begin
  1619. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1620. delete(expr,length(expr),1);
  1621. end;
  1622. end;
  1623. { check if there are wrong operator used like / or mod etc. }
  1624. if (hs<>'') and
  1625. not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,
  1626. AS_LPAREN,AS_RPAREN,AS_RBRACKET,AS_END]) then
  1627. Message(asmr_e_only_add_relocatable_symbol);
  1628. end;
  1629. AS_END,
  1630. AS_SEPARATOR,
  1631. AS_COMMA:
  1632. break;
  1633. else
  1634. Begin
  1635. { write error only once. }
  1636. if not errorflag then
  1637. Message(asmr_e_invalid_constant_expression);
  1638. { consume tokens until we find COMMA or SEPARATOR }
  1639. Consume(actasmtoken);
  1640. errorflag:=TRUE;
  1641. end;
  1642. end;
  1643. Until false;
  1644. { calculate expression }
  1645. if not ErrorFlag then
  1646. value:=CalculateExpression(expr)
  1647. else
  1648. value:=0;
  1649. end;
  1650. function tattreader.BuildConstExpression(allowref,betweenbracket:boolean): tcgint;
  1651. var
  1652. l : tcgint;
  1653. hs : string;
  1654. hssymtyp : TAsmSymType;
  1655. begin
  1656. BuildConstSymbolExpression(allowref,betweenbracket,false,l,hs,hssymtyp);
  1657. if hs<>'' then
  1658. Message(asmr_e_relocatable_symbol_not_allowed);
  1659. BuildConstExpression:=l;
  1660. end;
  1661. Procedure tattreader.BuildConstantOperand(oper : toperand);
  1662. var
  1663. l : tcgint;
  1664. tempstr : string;
  1665. tempsymtyp : TAsmSymType;
  1666. begin
  1667. BuildConstSymbolExpression(false,false,true,l,tempstr,tempsymtyp);
  1668. if tempstr<>'' then
  1669. begin
  1670. oper.opr.typ:=OPR_SYMBOL;
  1671. oper.opr.symofs:=l;
  1672. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1673. end
  1674. else
  1675. begin
  1676. oper.opr.typ:=OPR_CONSTANT;
  1677. { cast properly to avoid a range check error }
  1678. {$if defined(AVR) or defined(i8086)}
  1679. oper.opr.val:=longint(l);
  1680. {$else}
  1681. oper.opr.val:=aint(l);
  1682. {$endif}
  1683. end;
  1684. end;
  1685. procedure tattreader.BuildRva;
  1686. var
  1687. asmsymtyp : TAsmSymType;
  1688. asmsym: string;
  1689. value : tcgint;
  1690. ai:tai_const;
  1691. begin
  1692. repeat
  1693. case actasmtoken of
  1694. AS_INTNUM,
  1695. AS_PLUS,
  1696. AS_MINUS,
  1697. AS_LPAREN,
  1698. AS_ID :
  1699. Begin
  1700. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  1701. if asmsym<>'' then
  1702. begin
  1703. ai:=tai_const.create_type_sym(aitconst_rva_symbol,current_asmdata.RefAsmSymbol(asmsym,asmsymtyp));
  1704. ai.value:=value;
  1705. curlist.concat(ai);
  1706. end
  1707. else
  1708. Message(asmr_e_invalid_symbol_ref);
  1709. end;
  1710. AS_COMMA:
  1711. Consume(AS_COMMA);
  1712. AS_END,
  1713. AS_SEPARATOR:
  1714. break;
  1715. else
  1716. begin
  1717. Message(asmr_e_syn_constant);
  1718. RecoverConsume(false);
  1719. end
  1720. end; { end case }
  1721. until false;
  1722. end;
  1723. end.