ra386int.pas 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  4. Does the parsing process for the intel 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 Ra386int;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. cpubase,
  24. globtype,
  25. aasmbase,
  26. rasm,
  27. rax86;
  28. type
  29. tasmtoken = (
  30. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  31. AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  32. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  33. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,
  34. {------------------ Assembler directives --------------------}
  35. AS_ALIGN,AS_DB,AS_DW,AS_DD,AS_END,
  36. {------------------ Assembler Operators --------------------}
  37. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_DQWORD,AS_NEAR,AS_FAR,
  38. AS_HIGH,AS_LOW,AS_OFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  39. AS_AND,AS_OR,AS_XOR);
  40. type
  41. ti386intreader = class(tasmreader)
  42. actasmtoken : tasmtoken;
  43. prevasmtoken : tasmtoken;
  44. ActOpsize : topsize;
  45. constructor create;override;
  46. function is_asmopcode(const s: string):boolean;
  47. function is_asmoperator(const s: string):boolean;
  48. function is_asmdirective(const s: string):boolean;
  49. function is_register(const s:string):boolean;
  50. function is_locallabel(const s:string):boolean;
  51. function Assemble: tlinkedlist;override;
  52. procedure GetToken;
  53. function consume(t : tasmtoken):boolean;
  54. procedure RecoverConsume(allowcomma:boolean);
  55. procedure BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint);
  56. procedure BuildConstSymbolExpression(needofs,isref:boolean;var value:aint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  57. function BuildConstExpression:aint;
  58. function BuildRefConstExpression:aint;
  59. procedure BuildReference(oper : tx86operand);
  60. procedure BuildOperand(oper: tx86operand);
  61. procedure BuildConstantOperand(oper: tx86operand);
  62. procedure BuildOpCode(instr : tx86instruction);
  63. procedure BuildConstant(constsize: longint);
  64. end;
  65. implementation
  66. uses
  67. { common }
  68. cutils,
  69. { global }
  70. globals,verbose,
  71. systems,
  72. { aasm }
  73. aasmtai,aasmcpu,
  74. { symtable }
  75. symconst,symbase,symtype,symsym,symdef,symtable,
  76. { parser }
  77. scanner,
  78. { register allocator }
  79. rabase,rautils,itx86int,
  80. { codegen }
  81. cgbase,cgobj,procinfo
  82. ;
  83. type
  84. tasmkeyword = string[6];
  85. const
  86. { These tokens should be modified accordingly to the modifications }
  87. { in the different enumerations. }
  88. firstdirective = AS_ALIGN;
  89. lastdirective = AS_END;
  90. firstoperator = AS_BYTE;
  91. lastoperator = AS_XOR;
  92. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  93. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  94. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  95. ('ALIGN','DB','DW','DD','END');
  96. { problems with shl,shr,not,and,or and xor, they are }
  97. { context sensitive. }
  98. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  99. 'BYTE','WORD','DWORD','QWORD','TBYTE','DQWORD','NEAR','FAR','HIGH',
  100. 'LOW','OFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  101. 'OR','XOR');
  102. token2str : array[tasmtoken] of string[10] = (
  103. '','Label','LLabel','String','Integer',
  104. ',','[',']','(',
  105. ')',':','.','+','-','*',
  106. ';','identifier','register','opcode','/',
  107. '','','','','END',
  108. '','','','','','','','','',
  109. '','','','type','ptr','mod','shl','shr','not',
  110. 'and','or','xor'
  111. );
  112. var
  113. inexpression : boolean;
  114. constructor ti386intreader.create;
  115. var
  116. i : tasmop;
  117. str2opentry: tstr2opentry;
  118. Begin
  119. inherited create;
  120. { opcodes }
  121. { creates uppercased symbol tables for speed access }
  122. iasmops:=tdictionary.create;
  123. iasmops.delete_doubles:=true;
  124. for i:=firstop to lastop do
  125. begin
  126. str2opentry:=tstr2opentry.createname(upper(std_op2str[i]));
  127. str2opentry.op:=i;
  128. iasmops.insert(str2opentry);
  129. end;
  130. end;
  131. {---------------------------------------------------------------------}
  132. { Routines for the tokenizing }
  133. {---------------------------------------------------------------------}
  134. function ti386intreader.is_asmopcode(const s: string):boolean;
  135. var
  136. str2opentry: tstr2opentry;
  137. cond : string[4];
  138. cnd : tasmcond;
  139. j: longint;
  140. Begin
  141. is_asmopcode:=FALSE;
  142. actopcode:=A_None;
  143. actcondition:=C_None;
  144. actopsize:=S_NO;
  145. str2opentry:=tstr2opentry(iasmops.search(s));
  146. if assigned(str2opentry) then
  147. begin
  148. actopcode:=str2opentry.op;
  149. actasmtoken:=AS_OPCODE;
  150. is_asmopcode:=TRUE;
  151. exit;
  152. end;
  153. { not found yet, check condition opcodes }
  154. j:=0;
  155. while (j<CondAsmOps) do
  156. begin
  157. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  158. begin
  159. cond:=Copy(s,Length(CondAsmOpStr[j])+1,255);
  160. if cond<>'' then
  161. begin
  162. for cnd:=low(TasmCond) to high(TasmCond) do
  163. if Cond=Upper(cond2str[cnd]) then
  164. begin
  165. actopcode:=CondASmOp[j];
  166. actcondition:=cnd;
  167. is_asmopcode:=TRUE;
  168. actasmtoken:=AS_OPCODE;
  169. exit
  170. end;
  171. end;
  172. end;
  173. inc(j);
  174. end;
  175. end;
  176. function ti386intreader.is_asmoperator(const s: string):boolean;
  177. var
  178. i : longint;
  179. Begin
  180. for i:=0 to _count_asmoperators do
  181. if s=_asmoperators[i] then
  182. begin
  183. actasmtoken:=tasmtoken(longint(firstoperator)+i);
  184. is_asmoperator:=true;
  185. exit;
  186. end;
  187. is_asmoperator:=false;
  188. end;
  189. Function ti386intreader.is_asmdirective(const s: string):boolean;
  190. var
  191. i : longint;
  192. Begin
  193. for i:=0 to _count_asmdirectives do
  194. if s=_asmdirectives[i] then
  195. begin
  196. actasmtoken:=tasmtoken(longint(firstdirective)+i);
  197. is_asmdirective:=true;
  198. exit;
  199. end;
  200. is_asmdirective:=false;
  201. end;
  202. function ti386intreader.is_register(const s:string):boolean;
  203. begin
  204. is_register:=false;
  205. actasmregister:=masm_regnum_search(lower(s));
  206. if actasmregister<>NR_NO then
  207. begin
  208. is_register:=true;
  209. actasmtoken:=AS_REGISTER;
  210. end;
  211. end;
  212. function ti386intreader.is_locallabel(const s:string):boolean;
  213. begin
  214. is_locallabel:=(length(s)>1) and (s[1]='@');
  215. end;
  216. Procedure ti386intreader.GetToken;
  217. var
  218. len : longint;
  219. forcelabel : boolean;
  220. srsym : tsym;
  221. srsymtable : tsymtable;
  222. begin
  223. { save old token and reset new token }
  224. prevasmtoken:=actasmtoken;
  225. actasmtoken:=AS_NONE;
  226. { reset }
  227. forcelabel:=FALSE;
  228. actasmpattern:='';
  229. { while space and tab , continue scan... }
  230. while (c in [' ',#9]) do
  231. c:=current_scanner.asmgetchar;
  232. { get token pos }
  233. if not (c in [#10,#13,'{',';']) then
  234. current_scanner.gettokenpos;
  235. { Local Label, Label, Directive, Prefix or Opcode }
  236. if firsttoken and not (c in [#10,#13,'{',';']) then
  237. begin
  238. firsttoken:=FALSE;
  239. len:=0;
  240. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  241. begin
  242. { if there is an at_sign, then this must absolutely be a label }
  243. if c = '@' then
  244. forcelabel:=TRUE;
  245. inc(len);
  246. actasmpattern[len]:=c;
  247. c:=current_scanner.asmgetchar;
  248. end;
  249. actasmpattern[0]:=chr(len);
  250. uppervar(actasmpattern);
  251. { allow spaces }
  252. while (c in [' ',#9]) do
  253. c:=current_scanner.asmgetchar;
  254. { label ? }
  255. if c = ':' then
  256. begin
  257. if actasmpattern[1]='@' then
  258. actasmtoken:=AS_LLABEL
  259. else
  260. actasmtoken:=AS_LABEL;
  261. { let us point to the next character }
  262. c:=current_scanner.asmgetchar;
  263. firsttoken:=true;
  264. exit;
  265. end;
  266. { Are we trying to create an identifier with }
  267. { an at-sign...? }
  268. if forcelabel then
  269. Message(asmr_e_none_label_contain_at);
  270. { opcode ? }
  271. If is_asmopcode(actasmpattern) then
  272. Begin
  273. { check if we are in an expression }
  274. { then continue with asm directives }
  275. if not inexpression then
  276. exit;
  277. end;
  278. if is_asmdirective(actasmpattern) then
  279. exit;
  280. message1(asmr_e_unknown_opcode,actasmpattern);
  281. actasmtoken:=AS_NONE;
  282. exit;
  283. end
  284. else { else firsttoken }
  285. begin
  286. case c of
  287. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  288. { - @Result, @Code or @Data special variables. }
  289. begin
  290. actasmpattern:=c;
  291. c:=current_scanner.asmgetchar;
  292. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  293. begin
  294. actasmpattern:=actasmpattern + c;
  295. c:=current_scanner.asmgetchar;
  296. end;
  297. uppervar(actasmpattern);
  298. actasmtoken:=AS_ID;
  299. exit;
  300. end;
  301. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  302. begin
  303. actasmpattern:=c;
  304. c:=current_scanner.asmgetchar;
  305. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  306. begin
  307. actasmpattern:=actasmpattern + c;
  308. c:=current_scanner.asmgetchar;
  309. end;
  310. uppervar(actasmpattern);
  311. { after prefix we allow also a new opcode }
  312. If is_prefix(actopcode) and is_asmopcode(actasmpattern) then
  313. Begin
  314. { if we are not in a constant }
  315. { expression than this is an }
  316. { opcode. }
  317. if not inexpression then
  318. exit;
  319. end;
  320. { support st(X) for fpu registers }
  321. if (actasmpattern = 'ST') and (c='(') then
  322. Begin
  323. actasmpattern:=actasmpattern+c;
  324. c:=current_scanner.asmgetchar;
  325. if c in ['0'..'7'] then
  326. actasmpattern:=actasmpattern + c
  327. else
  328. Message(asmr_e_invalid_fpu_register);
  329. c:=current_scanner.asmgetchar;
  330. if c <> ')' then
  331. Message(asmr_e_invalid_fpu_register)
  332. else
  333. Begin
  334. actasmpattern:=actasmpattern + c;
  335. c:=current_scanner.asmgetchar;
  336. end;
  337. end;
  338. if is_register(actasmpattern) then
  339. exit;
  340. if is_asmdirective(actasmpattern) then
  341. exit;
  342. if is_asmoperator(actasmpattern) then
  343. exit;
  344. { if next is a '.' and this is a unitsym then we also need to
  345. parse the identifier }
  346. if (c='.') then
  347. begin
  348. searchsym(actasmpattern,srsym,srsymtable);
  349. if assigned(srsym) and
  350. (srsym.typ=unitsym) and
  351. (srsym.owner.unitid=0) then
  352. begin
  353. { Add . to create System.Identifier }
  354. actasmpattern:=actasmpattern+c;
  355. c:=current_scanner.asmgetchar;
  356. { Delphi allows System.@Halt, just ignore the @ }
  357. if c='@' then
  358. c:=current_scanner.asmgetchar;
  359. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  360. begin
  361. actasmpattern:=actasmpattern + upcase(c);
  362. c:=current_scanner.asmgetchar;
  363. end;
  364. end;
  365. end;
  366. actasmtoken:=AS_ID;
  367. exit;
  368. end;
  369. '''' : { string or character }
  370. begin
  371. actasmpattern:='';
  372. current_scanner.in_asm_string:=true;
  373. repeat
  374. if c = '''' then
  375. begin
  376. c:=current_scanner.asmgetchar;
  377. if c in [#10,#13] then
  378. begin
  379. Message(scan_f_string_exceeds_line);
  380. break;
  381. end;
  382. repeat
  383. if c='''' then
  384. begin
  385. c:=current_scanner.asmgetchar;
  386. if c='''' then
  387. begin
  388. actasmpattern:=actasmpattern+'''';
  389. c:=current_scanner.asmgetchar;
  390. if c in [#10,#13] then
  391. begin
  392. Message(scan_f_string_exceeds_line);
  393. break;
  394. end;
  395. end
  396. else
  397. break;
  398. end
  399. else
  400. begin
  401. actasmpattern:=actasmpattern+c;
  402. c:=current_scanner.asmgetchar;
  403. if c in [#10,#13] then
  404. begin
  405. Message(scan_f_string_exceeds_line);
  406. break
  407. end;
  408. end;
  409. until false; { end repeat }
  410. end
  411. else
  412. break; { end if }
  413. until false;
  414. current_scanner.in_asm_string:=false;
  415. actasmtoken:=AS_STRING;
  416. exit;
  417. end;
  418. '"' : { string or character }
  419. begin
  420. current_scanner.in_asm_string:=true;
  421. actasmpattern:='';
  422. repeat
  423. if c = '"' then
  424. begin
  425. c:=current_scanner.asmgetchar;
  426. if c in [#10,#13] then
  427. begin
  428. Message(scan_f_string_exceeds_line);
  429. break;
  430. end;
  431. repeat
  432. if c='"' then
  433. begin
  434. c:=current_scanner.asmgetchar;
  435. if c='"' then
  436. begin
  437. actasmpattern:=actasmpattern+'"';
  438. c:=current_scanner.asmgetchar;
  439. if c in [#10,#13] then
  440. begin
  441. Message(scan_f_string_exceeds_line);
  442. break;
  443. end;
  444. end
  445. else
  446. break;
  447. end
  448. else
  449. begin
  450. actasmpattern:=actasmpattern+c;
  451. c:=current_scanner.asmgetchar;
  452. if c in [#10,#13] then
  453. begin
  454. Message(scan_f_string_exceeds_line);
  455. break
  456. end;
  457. end;
  458. until false; { end repeat }
  459. end
  460. else
  461. break; { end if }
  462. until false;
  463. current_scanner.in_asm_string:=false;
  464. actasmtoken:=AS_STRING;
  465. exit;
  466. end;
  467. '$' :
  468. begin
  469. c:=current_scanner.asmgetchar;
  470. while c in ['0'..'9','A'..'F','a'..'f'] do
  471. begin
  472. actasmpattern:=actasmpattern + c;
  473. c:=current_scanner.asmgetchar;
  474. end;
  475. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  476. actasmtoken:=AS_INTNUM;
  477. exit;
  478. end;
  479. ',' :
  480. begin
  481. actasmtoken:=AS_COMMA;
  482. c:=current_scanner.asmgetchar;
  483. exit;
  484. end;
  485. '[' :
  486. begin
  487. actasmtoken:=AS_LBRACKET;
  488. c:=current_scanner.asmgetchar;
  489. exit;
  490. end;
  491. ']' :
  492. begin
  493. actasmtoken:=AS_RBRACKET;
  494. c:=current_scanner.asmgetchar;
  495. exit;
  496. end;
  497. '(' :
  498. begin
  499. actasmtoken:=AS_LPAREN;
  500. c:=current_scanner.asmgetchar;
  501. exit;
  502. end;
  503. ')' :
  504. begin
  505. actasmtoken:=AS_RPAREN;
  506. c:=current_scanner.asmgetchar;
  507. exit;
  508. end;
  509. ':' :
  510. begin
  511. actasmtoken:=AS_COLON;
  512. c:=current_scanner.asmgetchar;
  513. exit;
  514. end;
  515. '.' :
  516. begin
  517. actasmtoken:=AS_DOT;
  518. c:=current_scanner.asmgetchar;
  519. exit;
  520. end;
  521. '+' :
  522. begin
  523. actasmtoken:=AS_PLUS;
  524. c:=current_scanner.asmgetchar;
  525. exit;
  526. end;
  527. '-' :
  528. begin
  529. actasmtoken:=AS_MINUS;
  530. c:=current_scanner.asmgetchar;
  531. exit;
  532. end;
  533. '*' :
  534. begin
  535. actasmtoken:=AS_STAR;
  536. c:=current_scanner.asmgetchar;
  537. exit;
  538. end;
  539. '/' :
  540. begin
  541. actasmtoken:=AS_SLASH;
  542. c:=current_scanner.asmgetchar;
  543. exit;
  544. end;
  545. '0'..'9':
  546. begin
  547. actasmpattern:=c;
  548. c:=current_scanner.asmgetchar;
  549. { Get the possible characters }
  550. while c in ['0'..'9','A'..'F','a'..'f'] do
  551. begin
  552. actasmpattern:=actasmpattern + c;
  553. c:=current_scanner.asmgetchar;
  554. end;
  555. { Get ending character }
  556. uppervar(actasmpattern);
  557. c:=upcase(c);
  558. { possibly a binary number. }
  559. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  560. Begin
  561. { Delete the last binary specifier }
  562. delete(actasmpattern,length(actasmpattern),1);
  563. actasmpattern:=tostr(ValBinary(actasmpattern));
  564. actasmtoken:=AS_INTNUM;
  565. exit;
  566. end
  567. else
  568. Begin
  569. case c of
  570. 'O' :
  571. Begin
  572. actasmpattern:=tostr(ValOctal(actasmpattern));
  573. actasmtoken:=AS_INTNUM;
  574. c:=current_scanner.asmgetchar;
  575. exit;
  576. end;
  577. 'H' :
  578. Begin
  579. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  580. actasmtoken:=AS_INTNUM;
  581. c:=current_scanner.asmgetchar;
  582. exit;
  583. end;
  584. else { must be an integer number }
  585. begin
  586. actasmpattern:=tostr(ValDecimal(actasmpattern));
  587. actasmtoken:=AS_INTNUM;
  588. exit;
  589. end;
  590. end;
  591. end;
  592. end;
  593. ';','{',#13,#10 :
  594. begin
  595. c:=current_scanner.asmgetchar;
  596. firsttoken:=TRUE;
  597. actasmtoken:=AS_SEPARATOR;
  598. exit;
  599. end;
  600. else
  601. current_scanner.illegal_char(c);
  602. end;
  603. end;
  604. end;
  605. function ti386intreader.consume(t : tasmtoken):boolean;
  606. begin
  607. Consume:=true;
  608. if t<>actasmtoken then
  609. begin
  610. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  611. Consume:=false;
  612. end;
  613. repeat
  614. gettoken;
  615. until actasmtoken<>AS_NONE;
  616. end;
  617. procedure ti386intreader.RecoverConsume(allowcomma:boolean);
  618. begin
  619. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  620. begin
  621. if allowcomma and (actasmtoken=AS_COMMA) then
  622. break;
  623. Consume(actasmtoken);
  624. end;
  625. end;
  626. {*****************************************************************************
  627. Parsing Helpers
  628. *****************************************************************************}
  629. { This routine builds up a record offset after a AS_DOT
  630. token is encountered.
  631. On entry actasmtoken should be equal to AS_DOT }
  632. Procedure ti386intreader.BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint);
  633. var
  634. s : string;
  635. Begin
  636. offset:=0;
  637. size:=0;
  638. s:=expr;
  639. while (actasmtoken=AS_DOT) do
  640. begin
  641. Consume(AS_DOT);
  642. if actasmtoken=AS_ID then
  643. s:=s+'.'+actasmpattern;
  644. if not Consume(AS_ID) then
  645. begin
  646. RecoverConsume(true);
  647. break;
  648. end;
  649. end;
  650. if not GetRecordOffsetSize(s,offset,size) then
  651. Message(asmr_e_building_record_offset);
  652. end;
  653. Procedure ti386intreader.BuildConstSymbolExpression(needofs,isref:boolean;var value:aint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  654. var
  655. tempstr,expr,hs : string;
  656. parenlevel : longint;
  657. l,k : aint;
  658. hasparen,
  659. errorflag : boolean;
  660. prevtok : tasmtoken;
  661. hl : tasmlabel;
  662. hssymtyp : Tasmsymtype;
  663. def : tdef;
  664. sym : tsym;
  665. srsymtable : tsymtable;
  666. Begin
  667. { reset }
  668. value:=0;
  669. asmsym:='';
  670. asmsymtyp:=AT_DATA;
  671. errorflag:=FALSE;
  672. tempstr:='';
  673. expr:='';
  674. inexpression:=TRUE;
  675. parenlevel:=0;
  676. Repeat
  677. Case actasmtoken of
  678. AS_LPAREN:
  679. Begin
  680. Consume(AS_LPAREN);
  681. expr:=expr + '(';
  682. inc(parenlevel);
  683. end;
  684. AS_RPAREN:
  685. Begin
  686. Consume(AS_RPAREN);
  687. expr:=expr + ')';
  688. dec(parenlevel);
  689. end;
  690. AS_SHL:
  691. Begin
  692. Consume(AS_SHL);
  693. expr:=expr + '<';
  694. end;
  695. AS_SHR:
  696. Begin
  697. Consume(AS_SHR);
  698. expr:=expr + '>';
  699. end;
  700. AS_SLASH:
  701. Begin
  702. Consume(AS_SLASH);
  703. expr:=expr + '/';
  704. end;
  705. AS_MOD:
  706. Begin
  707. Consume(AS_MOD);
  708. expr:=expr + '%';
  709. end;
  710. AS_STAR:
  711. Begin
  712. Consume(AS_STAR);
  713. if isref and (actasmtoken=AS_REGISTER) then
  714. break;
  715. expr:=expr + '*';
  716. end;
  717. AS_PLUS:
  718. Begin
  719. Consume(AS_PLUS);
  720. if isref and (actasmtoken=AS_REGISTER) then
  721. break;
  722. expr:=expr + '+';
  723. end;
  724. AS_LBRACKET:
  725. begin
  726. { Support ugly delphi constructs like: [ECX].1+2[EDX] }
  727. if isref then
  728. break;
  729. end;
  730. AS_MINUS:
  731. Begin
  732. Consume(AS_MINUS);
  733. expr:=expr + '-';
  734. end;
  735. AS_AND:
  736. Begin
  737. Consume(AS_AND);
  738. expr:=expr + '&';
  739. end;
  740. AS_NOT:
  741. Begin
  742. Consume(AS_NOT);
  743. expr:=expr + '~';
  744. end;
  745. AS_XOR:
  746. Begin
  747. Consume(AS_XOR);
  748. expr:=expr + '^';
  749. end;
  750. AS_OR:
  751. Begin
  752. Consume(AS_OR);
  753. expr:=expr + '|';
  754. end;
  755. AS_INTNUM:
  756. Begin
  757. expr:=expr + actasmpattern;
  758. Consume(AS_INTNUM);
  759. end;
  760. AS_OFFSET:
  761. begin
  762. Consume(AS_OFFSET);
  763. needofs:=true;
  764. if actasmtoken<>AS_ID then
  765. Message(asmr_e_offset_without_identifier);
  766. end;
  767. AS_TYPE:
  768. begin
  769. l:=0;
  770. hasparen:=false;
  771. Consume(AS_TYPE);
  772. if actasmtoken=AS_LPAREN then
  773. begin
  774. hasparen:=true;
  775. Consume(AS_LPAREN);
  776. end;
  777. if actasmtoken<>AS_ID then
  778. Message(asmr_e_type_without_identifier)
  779. else
  780. begin
  781. tempstr:=actasmpattern;
  782. Consume(AS_ID);
  783. if actasmtoken=AS_DOT then
  784. BuildRecordOffsetSize(tempstr,k,l)
  785. else
  786. begin
  787. searchsym(tempstr,sym,srsymtable);
  788. if assigned(sym) then
  789. begin
  790. case sym.typ of
  791. globalvarsym,
  792. localvarsym,
  793. paravarsym :
  794. l:=tabstractvarsym(sym).getsize;
  795. typedconstsym :
  796. l:=ttypedconstsym(sym).getsize;
  797. typesym :
  798. l:=ttypesym(sym).restype.def.size;
  799. else
  800. Message(asmr_e_wrong_sym_type);
  801. end;
  802. end
  803. else
  804. Message1(sym_e_unknown_id,tempstr);
  805. end;
  806. end;
  807. str(l, tempstr);
  808. expr:=expr + tempstr;
  809. if hasparen then
  810. Consume(AS_RPAREN);
  811. end;
  812. AS_STRING:
  813. Begin
  814. l:=0;
  815. case Length(actasmpattern) of
  816. 1 :
  817. l:=ord(actasmpattern[1]);
  818. 2 :
  819. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  820. 3 :
  821. l:=ord(actasmpattern[3]) +
  822. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  823. 4 :
  824. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  825. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  826. else
  827. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  828. end;
  829. str(l, tempstr);
  830. expr:=expr + tempstr;
  831. Consume(AS_STRING);
  832. end;
  833. AS_ID:
  834. Begin
  835. hs:='';
  836. hssymtyp:=AT_DATA;
  837. def:=nil;
  838. tempstr:=actasmpattern;
  839. prevtok:=prevasmtoken;
  840. consume(AS_ID);
  841. if SearchIConstant(tempstr,l) then
  842. begin
  843. str(l, tempstr);
  844. expr:=expr + tempstr;
  845. end
  846. else
  847. begin
  848. if is_locallabel(tempstr) then
  849. begin
  850. CreateLocalLabel(tempstr,hl,false);
  851. hs:=hl.name;
  852. hssymtyp:=AT_FUNCTION;
  853. end
  854. else
  855. if SearchLabel(tempstr,hl,false) then
  856. begin
  857. hs:=hl.name;
  858. hssymtyp:=AT_FUNCTION;
  859. end
  860. else
  861. begin
  862. searchsym(tempstr,sym,srsymtable);
  863. if assigned(sym) then
  864. begin
  865. case sym.typ of
  866. globalvarsym :
  867. begin
  868. hs:=tglobalvarsym(sym).mangledname;
  869. def:=tglobalvarsym(sym).vartype.def;
  870. end;
  871. localvarsym,
  872. paravarsym :
  873. begin
  874. Message(asmr_e_no_local_or_para_allowed);
  875. end;
  876. typedconstsym :
  877. begin
  878. hs:=ttypedconstsym(sym).mangledname;
  879. def:=ttypedconstsym(sym).typedconsttype.def;
  880. end;
  881. procsym :
  882. begin
  883. if Tprocsym(sym).procdef_count>1 then
  884. Message(asmr_w_calling_overload_func);
  885. hs:=tprocsym(sym).first_procdef.mangledname;
  886. hssymtyp:=AT_FUNCTION;
  887. end;
  888. typesym :
  889. begin
  890. if not(ttypesym(sym).restype.def.deftype in [recorddef,objectdef]) then
  891. Message(asmr_e_wrong_sym_type);
  892. end;
  893. else
  894. Message(asmr_e_wrong_sym_type);
  895. end;
  896. end
  897. else
  898. Message1(sym_e_unknown_id,tempstr);
  899. end;
  900. { symbol found? }
  901. if hs<>'' then
  902. begin
  903. if asmsym='' then
  904. begin
  905. asmsym:=hs;
  906. asmsymtyp:=hssymtyp;
  907. end
  908. else
  909. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  910. if (expr='') or (expr[length(expr)]='+') then
  911. begin
  912. { don't remove the + if there could be a record field }
  913. if actasmtoken<>AS_DOT then
  914. delete(expr,length(expr),1);
  915. end
  916. else
  917. if needofs then
  918. begin
  919. if (prevtok<>AS_OFFSET) then
  920. Message(asmr_e_need_offset);
  921. end
  922. else
  923. Message(asmr_e_only_add_relocatable_symbol);
  924. end;
  925. if actasmtoken=AS_DOT then
  926. begin
  927. BuildRecordOffsetSize(tempstr,l,k);
  928. str(l, tempstr);
  929. expr:=expr + tempstr;
  930. end
  931. else
  932. begin
  933. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  934. delete(expr,length(expr),1);
  935. end;
  936. if (actasmtoken=AS_LBRACKET) and
  937. assigned(def) and
  938. (def.deftype=arraydef) then
  939. begin
  940. consume(AS_LBRACKET);
  941. l:=BuildConstExpression;
  942. if l<tarraydef(def).lowrange then
  943. begin
  944. Message(asmr_e_constant_out_of_bounds);
  945. l:=0;
  946. end
  947. else
  948. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  949. str(l, tempstr);
  950. expr:=expr + '+' + tempstr;
  951. consume(AS_RBRACKET);
  952. end;
  953. end;
  954. { check if there are wrong operator used like / or mod etc. }
  955. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  956. Message(asmr_e_only_add_relocatable_symbol);
  957. end;
  958. AS_END,
  959. AS_RBRACKET,
  960. AS_SEPARATOR,
  961. AS_COMMA:
  962. Begin
  963. break;
  964. end;
  965. else
  966. Begin
  967. { write error only once. }
  968. if not errorflag then
  969. Message(asmr_e_invalid_constant_expression);
  970. { consume tokens until we find COMMA or SEPARATOR }
  971. Consume(actasmtoken);
  972. errorflag:=TRUE;
  973. end;
  974. end;
  975. Until false;
  976. { calculate expression }
  977. if not ErrorFlag then
  978. value:=CalculateExpression(expr)
  979. else
  980. value:=0;
  981. { no longer in an expression }
  982. inexpression:=FALSE;
  983. end;
  984. Function ti386intreader.BuildConstExpression:aint;
  985. var
  986. l : aint;
  987. hs : string;
  988. hssymtyp : TAsmsymtype;
  989. begin
  990. BuildConstSymbolExpression(false,false,l,hs,hssymtyp);
  991. if hs<>'' then
  992. Message(asmr_e_relocatable_symbol_not_allowed);
  993. BuildConstExpression:=l;
  994. end;
  995. Function ti386intreader.BuildRefConstExpression:aint;
  996. var
  997. l : aint;
  998. hs : string;
  999. hssymtyp : TAsmsymtype;
  1000. begin
  1001. BuildConstSymbolExpression(false,true,l,hs,hssymtyp);
  1002. if hs<>'' then
  1003. Message(asmr_e_relocatable_symbol_not_allowed);
  1004. BuildRefConstExpression:=l;
  1005. end;
  1006. procedure ti386intreader.BuildReference(oper : tx86operand);
  1007. var
  1008. k,l,scale : aint;
  1009. tempstr,hs : string;
  1010. tempsymtyp : tasmsymtype;
  1011. typesize : longint;
  1012. code : integer;
  1013. hreg : tregister;
  1014. GotStar,GotOffset,HadVar,
  1015. GotPlus,Negative : boolean;
  1016. Begin
  1017. Consume(AS_LBRACKET);
  1018. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  1019. oper.InitRef;
  1020. GotStar:=false;
  1021. GotPlus:=true;
  1022. GotOffset:=false;
  1023. Negative:=false;
  1024. Scale:=0;
  1025. repeat
  1026. if GotOffset and (actasmtoken<>AS_ID) then
  1027. Message(asmr_e_invalid_reference_syntax);
  1028. Case actasmtoken of
  1029. AS_ID: { Constant reference expression OR variable reference expression }
  1030. Begin
  1031. if not GotPlus then
  1032. Message(asmr_e_invalid_reference_syntax);
  1033. if actasmpattern[1] = '@' then
  1034. Message(asmr_e_local_label_not_allowed_as_ref);
  1035. GotStar:=false;
  1036. GotPlus:=false;
  1037. if SearchIConstant(actasmpattern,l) or
  1038. SearchRecordType(actasmpattern) then
  1039. begin
  1040. l:=BuildRefConstExpression;
  1041. GotPlus:=(prevasmtoken=AS_PLUS);
  1042. GotStar:=(prevasmtoken=AS_STAR);
  1043. case oper.opr.typ of
  1044. OPR_LOCAL :
  1045. begin
  1046. if GotStar then
  1047. Message(asmr_e_invalid_reference_syntax);
  1048. if negative then
  1049. Dec(oper.opr.localsymofs,l)
  1050. else
  1051. Inc(oper.opr.localsymofs,l);
  1052. end;
  1053. OPR_REFERENCE :
  1054. begin
  1055. if GotStar then
  1056. oper.opr.ref.scalefactor:=l
  1057. else
  1058. begin
  1059. if negative then
  1060. Dec(oper.opr.ref.offset,l)
  1061. else
  1062. Inc(oper.opr.ref.offset,l);
  1063. end;
  1064. end;
  1065. end;
  1066. end
  1067. else
  1068. Begin
  1069. if oper.hasvar and not GotOffset then
  1070. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1071. HadVar:=oper.hasvar and GotOffset;
  1072. if negative then
  1073. Message(asmr_e_only_add_relocatable_symbol);
  1074. tempstr:=actasmpattern;
  1075. Consume(AS_ID);
  1076. { typecasting? }
  1077. if (actasmtoken=AS_LPAREN) and
  1078. SearchType(tempstr,typesize) then
  1079. begin
  1080. oper.hastype:=true;
  1081. Consume(AS_LPAREN);
  1082. BuildOperand(oper);
  1083. Consume(AS_RPAREN);
  1084. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  1085. oper.SetSize(typesize,true);
  1086. end
  1087. else
  1088. if not oper.SetupVar(tempstr,GotOffset) then
  1089. Message1(sym_e_unknown_id,tempstr);
  1090. { record.field ? }
  1091. if actasmtoken=AS_DOT then
  1092. begin
  1093. BuildRecordOffsetSize(tempstr,l,k);
  1094. case oper.opr.typ of
  1095. OPR_LOCAL :
  1096. inc(oper.opr.localsymofs,l);
  1097. OPR_REFERENCE :
  1098. inc(oper.opr.ref.offset,l);
  1099. end;
  1100. end;
  1101. if GotOffset then
  1102. begin
  1103. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1104. begin
  1105. if (oper.opr.typ=OPR_REFERENCE) then
  1106. oper.opr.ref.base:=NR_NO;
  1107. oper.hasvar:=hadvar;
  1108. end
  1109. else
  1110. begin
  1111. if oper.hasvar and hadvar then
  1112. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1113. { should we allow ?? }
  1114. end;
  1115. end;
  1116. end;
  1117. GotOffset:=false;
  1118. end;
  1119. AS_PLUS :
  1120. Begin
  1121. Consume(AS_PLUS);
  1122. Negative:=false;
  1123. GotPlus:=true;
  1124. GotStar:=false;
  1125. Scale:=0;
  1126. end;
  1127. AS_MINUS :
  1128. begin
  1129. Consume(AS_MINUS);
  1130. Negative:=true;
  1131. GotPlus:=true;
  1132. GotStar:=false;
  1133. Scale:=0;
  1134. end;
  1135. AS_STAR : { Scaling, with eax*4 order }
  1136. begin
  1137. Consume(AS_STAR);
  1138. hs:='';
  1139. l:=0;
  1140. case actasmtoken of
  1141. AS_LPAREN :
  1142. l:=BuildConstExpression;
  1143. AS_INTNUM:
  1144. Begin
  1145. hs:=actasmpattern;
  1146. Consume(AS_INTNUM);
  1147. end;
  1148. AS_REGISTER :
  1149. begin
  1150. case oper.opr.typ of
  1151. OPR_REFERENCE :
  1152. begin
  1153. if oper.opr.ref.scalefactor=0 then
  1154. begin
  1155. if scale<>0 then
  1156. begin
  1157. oper.opr.ref.scalefactor:=scale;
  1158. scale:=0;
  1159. end
  1160. else
  1161. Message(asmr_e_wrong_scale_factor);
  1162. end
  1163. else
  1164. Message(asmr_e_invalid_reference_syntax);
  1165. end;
  1166. OPR_LOCAL :
  1167. begin
  1168. if oper.opr.localscale=0 then
  1169. begin
  1170. if scale<>0 then
  1171. begin
  1172. oper.opr.localscale:=scale;
  1173. scale:=0;
  1174. end
  1175. else
  1176. Message(asmr_e_wrong_scale_factor);
  1177. end
  1178. else
  1179. Message(asmr_e_invalid_reference_syntax);
  1180. end;
  1181. end;
  1182. end;
  1183. else
  1184. Message(asmr_e_invalid_reference_syntax);
  1185. end;
  1186. if actasmtoken<>AS_REGISTER then
  1187. begin
  1188. if hs<>'' then
  1189. val(hs,l,code);
  1190. case oper.opr.typ of
  1191. OPR_REFERENCE :
  1192. oper.opr.ref.scalefactor:=l;
  1193. OPR_LOCAL :
  1194. oper.opr.localscale:=l;
  1195. end;
  1196. if l>9 then
  1197. Message(asmr_e_wrong_scale_factor);
  1198. end;
  1199. GotPlus:=false;
  1200. GotStar:=false;
  1201. end;
  1202. AS_REGISTER :
  1203. begin
  1204. if not((GotPlus and (not Negative)) or
  1205. GotStar) then
  1206. Message(asmr_e_invalid_reference_syntax);
  1207. hreg:=actasmregister;
  1208. Consume(AS_REGISTER);
  1209. { this register will be the index:
  1210. 1. just read a *
  1211. 2. next token is a *
  1212. 3. base register is already used }
  1213. case oper.opr.typ of
  1214. OPR_LOCAL :
  1215. begin
  1216. if (oper.opr.localindexreg<>NR_NO) then
  1217. Message(asmr_e_multiple_index);
  1218. oper.opr.localindexreg:=hreg;
  1219. if scale<>0 then
  1220. begin
  1221. oper.opr.localscale:=scale;
  1222. scale:=0;
  1223. end;
  1224. end;
  1225. OPR_REFERENCE :
  1226. begin
  1227. if (GotStar) or
  1228. (actasmtoken=AS_STAR) or
  1229. (oper.opr.ref.base<>NR_NO) then
  1230. begin
  1231. if (oper.opr.ref.index<>NR_NO) then
  1232. Message(asmr_e_multiple_index);
  1233. oper.opr.ref.index:=hreg;
  1234. if scale<>0 then
  1235. begin
  1236. oper.opr.ref.scalefactor:=scale;
  1237. scale:=0;
  1238. end;
  1239. end
  1240. else
  1241. oper.opr.ref.base:=hreg;
  1242. end;
  1243. end;
  1244. GotPlus:=false;
  1245. GotStar:=false;
  1246. end;
  1247. AS_OFFSET :
  1248. begin
  1249. Consume(AS_OFFSET);
  1250. GotOffset:=true;
  1251. end;
  1252. AS_TYPE,
  1253. AS_NOT,
  1254. AS_STRING,
  1255. AS_INTNUM,
  1256. AS_LPAREN : { Constant reference expression }
  1257. begin
  1258. if not GotPlus and not GotStar then
  1259. Message(asmr_e_invalid_reference_syntax);
  1260. BuildConstSymbolExpression(true,true,l,tempstr,tempsymtyp);
  1261. if tempstr<>'' then
  1262. begin
  1263. if GotStar then
  1264. Message(asmr_e_only_add_relocatable_symbol);
  1265. if not assigned(oper.opr.ref.symbol) then
  1266. oper.opr.ref.symbol:=objectlibrary.newasmsymbol(tempstr,AB_EXTERNAL,tempsymtyp)
  1267. else
  1268. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1269. end;
  1270. case oper.opr.typ of
  1271. OPR_REFERENCE :
  1272. begin
  1273. if GotStar then
  1274. oper.opr.ref.scalefactor:=l
  1275. else if (prevasmtoken = AS_STAR) then
  1276. begin
  1277. if scale<>0 then
  1278. scale:=l*scale
  1279. else
  1280. scale:=l;
  1281. end
  1282. else
  1283. begin
  1284. if negative then
  1285. Dec(oper.opr.ref.offset,l)
  1286. else
  1287. Inc(oper.opr.ref.offset,l);
  1288. end;
  1289. end;
  1290. OPR_LOCAL :
  1291. begin
  1292. if GotStar then
  1293. oper.opr.localscale:=l
  1294. else if (prevasmtoken = AS_STAR) then
  1295. begin
  1296. if scale<>0 then
  1297. scale:=l*scale
  1298. else
  1299. scale:=l;
  1300. end
  1301. else
  1302. begin
  1303. if negative then
  1304. Dec(oper.opr.localsymofs,l)
  1305. else
  1306. Inc(oper.opr.localsymofs,l);
  1307. end;
  1308. end;
  1309. end;
  1310. GotPlus:=(prevasmtoken=AS_PLUS) or
  1311. (prevasmtoken=AS_MINUS);
  1312. if GotPlus then
  1313. negative := prevasmtoken = AS_MINUS;
  1314. GotStar:=(prevasmtoken=AS_STAR);
  1315. end;
  1316. AS_RBRACKET :
  1317. begin
  1318. if GotPlus or GotStar then
  1319. Message(asmr_e_invalid_reference_syntax);
  1320. Consume(AS_RBRACKET);
  1321. break;
  1322. end;
  1323. else
  1324. Begin
  1325. Message(asmr_e_invalid_reference_syntax);
  1326. RecoverConsume(true);
  1327. break;
  1328. end;
  1329. end;
  1330. until false;
  1331. end;
  1332. Procedure ti386intreader.BuildConstantOperand(oper: tx86operand);
  1333. var
  1334. l : aint;
  1335. tempstr : string;
  1336. tempsymtyp : tasmsymtype;
  1337. begin
  1338. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1339. Message(asmr_e_invalid_operand_type);
  1340. BuildConstSymbolExpression(true,false,l,tempstr,tempsymtyp);
  1341. if tempstr<>'' then
  1342. begin
  1343. oper.opr.typ:=OPR_SYMBOL;
  1344. oper.opr.symofs:=l;
  1345. oper.opr.symbol:=objectlibrary.newasmsymbol(tempstr,AB_EXTERNAL,tempsymtyp);
  1346. end
  1347. else
  1348. begin
  1349. if oper.opr.typ=OPR_NONE then
  1350. begin
  1351. oper.opr.typ:=OPR_CONSTANT;
  1352. oper.opr.val:=l;
  1353. end
  1354. else
  1355. inc(oper.opr.val,l);
  1356. end;
  1357. end;
  1358. Procedure ti386intreader.BuildOperand(oper: tx86operand);
  1359. procedure AddLabelOperand(hl:tasmlabel);
  1360. begin
  1361. if is_calljmp(actopcode) then
  1362. begin
  1363. oper.opr.typ:=OPR_SYMBOL;
  1364. oper.opr.symbol:=hl;
  1365. end
  1366. else
  1367. begin
  1368. oper.InitRef;
  1369. oper.opr.ref.symbol:=hl;
  1370. end;
  1371. end;
  1372. var
  1373. expr : string;
  1374. tempreg : tregister;
  1375. typesize,
  1376. l : aint;
  1377. hl : tasmlabel;
  1378. toffset,
  1379. tsize : aint;
  1380. Begin
  1381. expr:='';
  1382. repeat
  1383. if actasmtoken=AS_DOT then
  1384. begin
  1385. if expr<>'' then
  1386. begin
  1387. BuildRecordOffsetSize(expr,toffset,tsize);
  1388. oper.SetSize(tsize,true);
  1389. case oper.opr.typ of
  1390. OPR_LOCAL :
  1391. begin
  1392. { don't allow direct access to fields of parameters, becuase that
  1393. will generate buggy code. Allow it only for explicit typecasting
  1394. and when the parameter is in a register (delphi compatible) }
  1395. if (not oper.hastype) and
  1396. (oper.opr.localsym.owner.symtabletype=parasymtable) and
  1397. (current_procinfo.procdef.proccalloption<>pocall_register) then
  1398. Message(asmr_e_cannot_access_field_directly_for_parameters);
  1399. inc(oper.opr.localsymofs,toffset)
  1400. end;
  1401. OPR_CONSTANT :
  1402. inc(oper.opr.val,toffset);
  1403. OPR_REFERENCE :
  1404. inc(oper.opr.ref.offset,toffset);
  1405. OPR_NONE :
  1406. begin
  1407. oper.opr.typ:=OPR_CONSTANT;
  1408. oper.opr.val:=toffset;
  1409. end;
  1410. else
  1411. internalerror(200309222);
  1412. end;
  1413. expr:='';
  1414. end
  1415. else
  1416. begin
  1417. { See it as a separator }
  1418. Consume(AS_DOT);
  1419. end;
  1420. end;
  1421. case actasmtoken of
  1422. AS_OFFSET,
  1423. AS_TYPE,
  1424. AS_NOT,
  1425. AS_STRING :
  1426. Begin
  1427. BuildConstantOperand(oper);
  1428. end;
  1429. AS_PLUS,
  1430. AS_MINUS,
  1431. AS_LPAREN,
  1432. AS_INTNUM :
  1433. begin
  1434. case oper.opr.typ of
  1435. OPR_REFERENCE :
  1436. inc(oper.opr.ref.offset,BuildRefConstExpression);
  1437. OPR_LOCAL :
  1438. inc(oper.opr.localsymofs,BuildConstExpression);
  1439. OPR_NONE,
  1440. OPR_CONSTANT :
  1441. BuildConstantOperand(oper);
  1442. else
  1443. Message(asmr_e_invalid_operand_type);
  1444. end;
  1445. end;
  1446. AS_ID : { A constant expression, or a Variable ref. }
  1447. Begin
  1448. { Label or Special symbol reference? }
  1449. if actasmpattern[1] = '@' then
  1450. Begin
  1451. if actasmpattern = '@RESULT' then
  1452. Begin
  1453. oper.SetupResult;
  1454. Consume(AS_ID);
  1455. end
  1456. else
  1457. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1458. begin
  1459. Message(asmr_w_CODE_and_DATA_not_supported);
  1460. Consume(AS_ID);
  1461. end
  1462. else
  1463. { Local Label }
  1464. begin
  1465. CreateLocalLabel(actasmpattern,hl,false);
  1466. Consume(AS_ID);
  1467. AddLabelOperand(hl);
  1468. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1469. Message(asmr_e_syntax_error);
  1470. end;
  1471. end
  1472. else
  1473. { support result for delphi modes }
  1474. if (m_objpas in aktmodeswitches) and (actasmpattern='RESULT') then
  1475. begin
  1476. oper.SetUpResult;
  1477. Consume(AS_ID);
  1478. end
  1479. { probably a variable or normal expression }
  1480. { or a procedure (such as in CALL ID) }
  1481. else
  1482. Begin
  1483. { is it a constant ? }
  1484. if SearchIConstant(actasmpattern,l) then
  1485. Begin
  1486. case oper.opr.typ of
  1487. OPR_REFERENCE :
  1488. inc(oper.opr.ref.offset,BuildRefConstExpression);
  1489. OPR_LOCAL :
  1490. inc(oper.opr.localsymofs,BuildRefConstExpression);
  1491. OPR_NONE,
  1492. OPR_CONSTANT :
  1493. BuildConstantOperand(oper);
  1494. else
  1495. Message(asmr_e_invalid_operand_type);
  1496. end;
  1497. end
  1498. else
  1499. { Check for pascal label }
  1500. if SearchLabel(actasmpattern,hl,false) then
  1501. begin
  1502. Consume(AS_ID);
  1503. AddLabelOperand(hl);
  1504. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1505. Message(asmr_e_syntax_error);
  1506. end
  1507. else
  1508. { is it a normal variable ? }
  1509. Begin
  1510. expr:=actasmpattern;
  1511. Consume(AS_ID);
  1512. { typecasting? }
  1513. if SearchType(expr,typesize) then
  1514. begin
  1515. oper.hastype:=true;
  1516. if (actasmtoken=AS_LPAREN) then
  1517. begin
  1518. Consume(AS_LPAREN);
  1519. BuildOperand(oper);
  1520. Consume(AS_RPAREN);
  1521. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  1522. oper.SetSize(typesize,true);
  1523. end;
  1524. end
  1525. else
  1526. begin
  1527. if not oper.SetupVar(expr,false) then
  1528. Begin
  1529. { not a variable, check special variables.. }
  1530. if expr = 'SELF' then
  1531. oper.SetupSelf
  1532. else
  1533. Message1(sym_e_unknown_id,expr);
  1534. expr:='';
  1535. end;
  1536. end;
  1537. end;
  1538. end;
  1539. end;
  1540. AS_REGISTER : { Register, a variable reference or a constant reference }
  1541. begin
  1542. { save the type of register used. }
  1543. tempreg:=actasmregister;
  1544. Consume(AS_REGISTER);
  1545. if actasmtoken = AS_COLON then
  1546. Begin
  1547. Consume(AS_COLON);
  1548. oper.InitRef;
  1549. oper.opr.ref.segment:=tempreg;
  1550. BuildReference(oper);
  1551. end
  1552. else
  1553. { Simple register }
  1554. begin
  1555. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1556. Message(asmr_e_invalid_operand_type);
  1557. oper.opr.typ:=OPR_REGISTER;
  1558. oper.opr.reg:=tempreg;
  1559. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  1560. end;
  1561. end;
  1562. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1563. Begin
  1564. BuildReference(oper);
  1565. end;
  1566. AS_SEG :
  1567. Begin
  1568. Message(asmr_e_seg_not_supported);
  1569. Consume(actasmtoken);
  1570. end;
  1571. AS_SEPARATOR,
  1572. AS_END,
  1573. AS_COMMA:
  1574. break;
  1575. else
  1576. Message(asmr_e_syn_operand);
  1577. end;
  1578. until not(actasmtoken in [AS_DOT,AS_PLUS,AS_LBRACKET]);
  1579. if not((actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) or
  1580. (oper.hastype and (actasmtoken=AS_RPAREN))) then
  1581. begin
  1582. Message(asmr_e_syntax_error);
  1583. RecoverConsume(true);
  1584. end;
  1585. end;
  1586. Procedure ti386intreader.BuildOpCode(instr : tx86instruction);
  1587. var
  1588. PrefixOp,OverrideOp: tasmop;
  1589. size,
  1590. operandnum : longint;
  1591. Begin
  1592. PrefixOp:=A_None;
  1593. OverrideOp:=A_None;
  1594. { prefix seg opcode / prefix opcode }
  1595. repeat
  1596. if is_prefix(actopcode) then
  1597. begin
  1598. with instr do
  1599. begin
  1600. OpOrder:=op_intel;
  1601. PrefixOp:=ActOpcode;
  1602. opcode:=ActOpcode;
  1603. condition:=ActCondition;
  1604. opsize:=ActOpsize;
  1605. ConcatInstruction(curlist);
  1606. end;
  1607. Consume(AS_OPCODE);
  1608. end
  1609. else
  1610. if is_override(actopcode) then
  1611. begin
  1612. with instr do
  1613. begin
  1614. OpOrder:=op_intel;
  1615. OverrideOp:=ActOpcode;
  1616. opcode:=ActOpcode;
  1617. condition:=ActCondition;
  1618. opsize:=ActOpsize;
  1619. ConcatInstruction(curlist);
  1620. end;
  1621. Consume(AS_OPCODE);
  1622. end
  1623. else
  1624. break;
  1625. { allow for newline after prefix or override }
  1626. while actasmtoken=AS_SEPARATOR do
  1627. Consume(AS_SEPARATOR);
  1628. until (actasmtoken<>AS_OPCODE);
  1629. { opcode }
  1630. if (actasmtoken <> AS_OPCODE) then
  1631. Begin
  1632. Message(asmr_e_invalid_or_missing_opcode);
  1633. RecoverConsume(false);
  1634. exit;
  1635. end;
  1636. { Fill the instr object with the current state }
  1637. with instr do
  1638. begin
  1639. OpOrder:=op_intel;
  1640. Opcode:=ActOpcode;
  1641. condition:=ActCondition;
  1642. opsize:=ActOpsize;
  1643. { Valid combination of prefix/override and instruction ? }
  1644. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1645. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1646. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1647. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1648. end;
  1649. { We are reading operands, so opcode will be an AS_ID }
  1650. operandnum:=1;
  1651. Consume(AS_OPCODE);
  1652. { Zero operand opcode ? }
  1653. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1654. begin
  1655. operandnum:=0;
  1656. exit;
  1657. end;
  1658. { Read Operands }
  1659. repeat
  1660. case actasmtoken of
  1661. { End of asm operands for this opcode }
  1662. AS_END,
  1663. AS_SEPARATOR :
  1664. break;
  1665. { Operand delimiter }
  1666. AS_COMMA :
  1667. Begin
  1668. if operandnum > Max_Operands then
  1669. Message(asmr_e_too_many_operands)
  1670. else
  1671. Inc(operandnum);
  1672. Consume(AS_COMMA);
  1673. end;
  1674. { Typecast, Constant Expression, Type Specifier }
  1675. AS_DWORD,
  1676. AS_BYTE,
  1677. AS_WORD,
  1678. AS_TBYTE,
  1679. AS_DQWORD,
  1680. AS_QWORD :
  1681. Begin
  1682. { load the size in a temp variable, so it can be set when the
  1683. operand is read }
  1684. size:=0;
  1685. Case actasmtoken of
  1686. AS_DWORD : size:=4;
  1687. AS_WORD : size:=2;
  1688. AS_BYTE : size:=1;
  1689. AS_QWORD : size:=8;
  1690. AS_DQWORD : size:=16;
  1691. AS_TBYTE : size:=10;
  1692. end;
  1693. Consume(actasmtoken);
  1694. case actasmtoken of
  1695. AS_LPAREN :
  1696. begin
  1697. instr.Operands[operandnum].hastype:=true;
  1698. Consume(AS_LPAREN);
  1699. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1700. Consume(AS_RPAREN);
  1701. end;
  1702. AS_PTR :
  1703. begin
  1704. Consume(AS_PTR);
  1705. instr.Operands[operandnum].InitRef;
  1706. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1707. end;
  1708. else
  1709. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1710. end;
  1711. { now set the size which was specified by the override }
  1712. instr.Operands[operandnum].setsize(size,true);
  1713. end;
  1714. { Type specifier }
  1715. AS_NEAR,
  1716. AS_FAR :
  1717. Begin
  1718. if actasmtoken = AS_NEAR then
  1719. begin
  1720. Message(asmr_w_near_ignored);
  1721. instr.opsize:=S_NEAR;
  1722. end
  1723. else
  1724. begin
  1725. Message(asmr_w_far_ignored);
  1726. instr.opsize:=S_FAR;
  1727. end;
  1728. Consume(actasmtoken);
  1729. if actasmtoken=AS_PTR then
  1730. begin
  1731. Consume(AS_PTR);
  1732. instr.Operands[operandnum].InitRef;
  1733. end;
  1734. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1735. end;
  1736. else
  1737. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1738. end; { end case }
  1739. until false;
  1740. instr.Ops:=operandnum;
  1741. end;
  1742. Procedure ti386intreader.BuildConstant(constsize: longint);
  1743. var
  1744. asmsymtyp : tasmsymtype;
  1745. asmsym,
  1746. expr: string;
  1747. value : aint;
  1748. Begin
  1749. Repeat
  1750. Case actasmtoken of
  1751. AS_STRING:
  1752. Begin
  1753. { DD and DW cases }
  1754. if constsize <> 1 then
  1755. Begin
  1756. if Not PadZero(actasmpattern,constsize) then
  1757. Message(scan_f_string_exceeds_line);
  1758. end;
  1759. expr:=actasmpattern;
  1760. Consume(AS_STRING);
  1761. Case actasmtoken of
  1762. AS_COMMA:
  1763. Consume(AS_COMMA);
  1764. AS_END,
  1765. AS_SEPARATOR: ;
  1766. else
  1767. Message(asmr_e_invalid_string_expression);
  1768. end;
  1769. ConcatString(curlist,expr);
  1770. end;
  1771. AS_PLUS,
  1772. AS_MINUS,
  1773. AS_LPAREN,
  1774. AS_NOT,
  1775. AS_INTNUM,
  1776. AS_ID :
  1777. Begin
  1778. BuildConstSymbolExpression(false,false,value,asmsym,asmsymtyp);
  1779. if asmsym<>'' then
  1780. begin
  1781. if constsize<>sizeof(aint) then
  1782. Message1(asmr_w_const32bit_for_address,asmsym);
  1783. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value)
  1784. end
  1785. else
  1786. ConcatConstant(curlist,value,constsize);
  1787. end;
  1788. AS_COMMA:
  1789. Consume(AS_COMMA);
  1790. AS_END,
  1791. AS_SEPARATOR:
  1792. break;
  1793. else
  1794. begin
  1795. Message(asmr_e_syn_constant);
  1796. RecoverConsume(false);
  1797. end
  1798. end;
  1799. Until false;
  1800. end;
  1801. function ti386intreader.Assemble: tlinkedlist;
  1802. Var
  1803. hl : tasmlabel;
  1804. instr : Tx86Instruction;
  1805. Begin
  1806. Message1(asmr_d_start_reading,'intel');
  1807. inexpression:=FALSE;
  1808. firsttoken:=TRUE;
  1809. { sets up all opcode and register tables in uppercase
  1810. done in the construtor now
  1811. if not _asmsorted then
  1812. Begin
  1813. SetupTables;
  1814. _asmsorted:=TRUE;
  1815. end;
  1816. }
  1817. curlist:=TAAsmoutput.Create;
  1818. { setup label linked list }
  1819. LocalLabelList:=TLocalLabelList.Create;
  1820. { start tokenizer }
  1821. c:=current_scanner.asmgetcharstart;
  1822. gettoken;
  1823. { main loop }
  1824. repeat
  1825. case actasmtoken of
  1826. AS_LLABEL:
  1827. Begin
  1828. if CreateLocalLabel(actasmpattern,hl,true) then
  1829. ConcatLabel(curlist,hl);
  1830. Consume(AS_LLABEL);
  1831. end;
  1832. AS_LABEL:
  1833. Begin
  1834. if SearchLabel(upper(actasmpattern),hl,true) then
  1835. ConcatLabel(curlist,hl)
  1836. else
  1837. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1838. Consume(AS_LABEL);
  1839. end;
  1840. AS_DW :
  1841. Begin
  1842. inexpression:=true;
  1843. Consume(AS_DW);
  1844. BuildConstant(2);
  1845. inexpression:=false;
  1846. end;
  1847. AS_DB :
  1848. Begin
  1849. inexpression:=true;
  1850. Consume(AS_DB);
  1851. BuildConstant(1);
  1852. inexpression:=false;
  1853. end;
  1854. AS_DD :
  1855. Begin
  1856. inexpression:=true;
  1857. Consume(AS_DD);
  1858. BuildConstant(4);
  1859. inexpression:=false;
  1860. end;
  1861. AS_ALIGN:
  1862. Begin
  1863. Consume(AS_ALIGN);
  1864. ConcatAlign(curlist,BuildConstExpression);
  1865. if actasmtoken<>AS_SEPARATOR then
  1866. Consume(AS_SEPARATOR);
  1867. end;
  1868. AS_OPCODE :
  1869. Begin
  1870. instr:=Tx86Instruction.Create(Tx86Operand);
  1871. BuildOpcode(instr);
  1872. with instr do
  1873. begin
  1874. { We need AT&T style operands }
  1875. Swapoperands;
  1876. { Must be done with args in ATT order }
  1877. CheckNonCommutativeOpcodes;
  1878. AddReferenceSizes;
  1879. SetInstructionOpsize;
  1880. CheckOperandSizes;
  1881. ConcatInstruction(curlist);
  1882. end;
  1883. instr.Free;
  1884. end;
  1885. AS_SEPARATOR :
  1886. Begin
  1887. Consume(AS_SEPARATOR);
  1888. end;
  1889. AS_END :
  1890. break; { end assembly block }
  1891. else
  1892. Begin
  1893. Message(asmr_e_syntax_error);
  1894. RecoverConsume(false);
  1895. end;
  1896. end; { end case }
  1897. until false;
  1898. { Check LocalLabelList }
  1899. LocalLabelList.CheckEmitted;
  1900. LocalLabelList.Free;
  1901. { Return the list in an asmnode }
  1902. assemble:=curlist;
  1903. Message1(asmr_d_finish_reading,'intel');
  1904. end;
  1905. {*****************************************************************************
  1906. Initialize
  1907. *****************************************************************************}
  1908. const
  1909. asmmode_i386_intel_info : tasmmodeinfo =
  1910. (
  1911. id : asmmode_i386_intel;
  1912. idtxt : 'INTEL';
  1913. casmreader : ti386intreader;
  1914. );
  1915. begin
  1916. RegisterAsmMode(asmmode_i386_intel_info);
  1917. end.
  1918. {
  1919. $Log$
  1920. Revision 1.82 2004-11-29 18:50:15 peter
  1921. * os2 fixes for import
  1922. * asmsymtype support for intel reader
  1923. Revision 1.81 2004/11/21 21:36:13 peter
  1924. * allow spaces before : of a label
  1925. Revision 1.80 2004/11/09 22:32:59 peter
  1926. * small m68k updates to bring it up2date
  1927. * give better error for external local variable
  1928. Revision 1.79 2004/11/08 22:09:59 peter
  1929. * tvarsym splitted
  1930. Revision 1.78 2004/10/31 21:45:03 peter
  1931. * generic tlocation
  1932. * move tlocation to cgutils
  1933. Revision 1.77 2004/09/13 20:25:52 peter
  1934. * support byte() typecast
  1935. * support array index
  1936. Revision 1.76 2004/07/06 19:47:19 peter
  1937. * fixed parsing of strings in db
  1938. Revision 1.75 2004/06/23 14:54:46 peter
  1939. * align directive added
  1940. Revision 1.74 2004/06/20 08:55:31 florian
  1941. * logs truncated
  1942. Revision 1.73 2004/06/16 20:07:10 florian
  1943. * dwarf branch merged
  1944. Revision 1.72 2004/05/20 21:54:33 florian
  1945. + <pointer> - <pointer> result is divided by the pointer element size now
  1946. this is delphi compatible as well as resulting in the expected result for p1+(p2-p1)
  1947. Revision 1.71.2.3 2004/05/02 00:31:33 peter
  1948. * fixedi i386 compile
  1949. Revision 1.71.2.2 2004/05/01 23:36:47 peter
  1950. * assembler reader 64bit fixes
  1951. Revision 1.71.2.1 2004/05/01 16:02:10 peter
  1952. * POINTER_SIZE replaced with sizeof(aint)
  1953. * aint,aword,tconst*int moved to globtype
  1954. }