rax86int.pas 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. Does the parsing process for the intel 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 Rax86int;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. cpubase,
  23. globtype,
  24. aasmbase,
  25. rasm,
  26. rax86;
  27. type
  28. tasmtoken = (
  29. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  30. AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  31. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  32. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,
  33. {------------------ Assembler directives --------------------}
  34. AS_ALIGN,AS_DB,AS_DW,AS_DD,AS_DQ,AS_PUBLIC,AS_END,
  35. {------------------ Assembler Operators --------------------}
  36. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_DQWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD,AS_NEAR,AS_FAR,
  37. AS_HIGH,AS_LOW,AS_OFFSET,AS_SIZEOF,AS_VMTOFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  38. AS_AND,AS_OR,AS_XOR,AS_WRT,AS___GOTPCREL);
  39. type
  40. tx86intreader = class(tasmreader)
  41. actasmpattern_origcase : string;
  42. actasmtoken : tasmtoken;
  43. prevasmtoken : tasmtoken;
  44. ActOpsize : topsize;
  45. inexpression : boolean;
  46. constructor create;override;
  47. function is_asmopcode(const s: string):boolean;
  48. function is_asmoperator(const s: string):boolean;
  49. function is_asmdirective(const s: string):boolean;
  50. function is_register(const s:string):boolean;
  51. function is_locallabel(const s:string):boolean;
  52. function Assemble: tlinkedlist;override;
  53. procedure GetToken;
  54. function consume(t : tasmtoken):boolean;
  55. procedure RecoverConsume(allowcomma:boolean);
  56. procedure BuildRecordOffsetSize(const expr: string;var offset:tcgint;var size:tcgint; var mangledname: string; needvmtofs: boolean; out hastypecast: boolean);
  57. procedure BuildConstSymbolExpression(needofs,isref,startingminus:boolean;var value:tcgint;var asmsym:string;var asmsymtyp:TAsmsymtype;out isseg,is_farproc_entry,hasofs:boolean);
  58. function BuildConstExpression:aint;
  59. function BuildRefConstExpression(startingminus:boolean=false):aint;
  60. procedure BuildReference(oper : tx86operand);
  61. procedure BuildOperand(oper: tx86operand;istypecast:boolean);
  62. procedure BuildConstantOperand(oper: tx86operand);
  63. procedure BuildOpCode(instr : tx86instruction);
  64. procedure BuildConstant(constsize: byte);
  65. end;
  66. implementation
  67. uses
  68. { common }
  69. cutils,
  70. { global }
  71. globals,verbose,
  72. systems,
  73. { aasm }
  74. aasmdata,aasmcpu,
  75. {$ifdef i8086}
  76. aasmtai,
  77. {$endif i8086}
  78. { symtable }
  79. symconst,symbase,symtype,symsym,symdef,
  80. {$ifdef i8086}
  81. symcpu,
  82. {$endif i8086}
  83. { parser }
  84. scanner,pbase,
  85. { register allocator }
  86. rautils,itx86int,
  87. { codegen }
  88. cgbase,procinfo,paramgr
  89. ;
  90. type
  91. tasmkeyword = string[9];
  92. const
  93. { These tokens should be modified accordingly to the modifications }
  94. { in the different enumerations. }
  95. firstdirective = AS_ALIGN;
  96. lastdirective = AS_END;
  97. firstoperator = AS_BYTE;
  98. lastoperator = AS___GOTPCREL;
  99. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  100. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  101. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  102. ('ALIGN','DB','DW','DD','DQ','PUBLIC','END');
  103. { problems with shl,shr,not,and,or and xor, they are }
  104. { context sensitive. }
  105. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  106. 'BYTE','WORD','DWORD','QWORD','TBYTE','DQWORD','OWORD','XMMWORD','YWORD','YMMWORD','NEAR','FAR','HIGH',
  107. 'LOW','OFFSET','SIZEOF','VMTOFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  108. 'OR','XOR','WRT','GOTPCREL');
  109. token2str : array[tasmtoken] of string[10] = (
  110. '','Label','LLabel','String','Integer',
  111. ',','[',']','(',
  112. ')',':','.','+','-','*',
  113. ';','identifier','register','opcode','/',
  114. '','','','','','','END',
  115. '','','','','','','','','','','','',
  116. '','','','sizeof','vmtoffset','','type','ptr','mod','shl','shr','not',
  117. 'and','or','xor','wrt','..gotpcrel'
  118. );
  119. constructor tx86intreader.create;
  120. var
  121. i : tasmop;
  122. Begin
  123. inherited create;
  124. iasmops:=TFPHashList.create;
  125. for i:=firstop to lastop do
  126. iasmops.Add(upper(std_op2str[i]),Pointer(PtrInt(i)));
  127. end;
  128. {---------------------------------------------------------------------}
  129. { Routines for the tokenizing }
  130. {---------------------------------------------------------------------}
  131. function tx86intreader.is_asmopcode(const s: string):boolean;
  132. var
  133. cond : string[4];
  134. cnd : tasmcond;
  135. j: longint;
  136. Begin
  137. is_asmopcode:=FALSE;
  138. actopcode:=A_None;
  139. actcondition:=C_None;
  140. actopsize:=S_NO;
  141. { Search opcodes }
  142. actopcode:=tasmop(PtrUInt(iasmops.Find(s)));
  143. if actopcode<>A_NONE then
  144. begin
  145. actasmtoken:=AS_OPCODE;
  146. result:=TRUE;
  147. exit;
  148. end;
  149. { not found yet, check condition opcodes }
  150. j:=0;
  151. while (j<CondAsmOps) do
  152. begin
  153. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  154. begin
  155. cond:=Copy(s,Length(CondAsmOpStr[j])+1,255);
  156. if cond<>'' then
  157. begin
  158. for cnd:=low(TasmCond) to high(TasmCond) do
  159. if Cond=Upper(cond2str[cnd]) then
  160. begin
  161. actopcode:=CondASmOp[j];
  162. actcondition:=cnd;
  163. is_asmopcode:=TRUE;
  164. actasmtoken:=AS_OPCODE;
  165. exit
  166. end;
  167. end;
  168. end;
  169. inc(j);
  170. end;
  171. end;
  172. function tx86intreader.is_asmoperator(const s: string):boolean;
  173. var
  174. i : longint;
  175. Begin
  176. for i:=0 to _count_asmoperators do
  177. if s=_asmoperators[i] then
  178. begin
  179. actasmtoken:=tasmtoken(longint(firstoperator)+i);
  180. is_asmoperator:=true;
  181. exit;
  182. end;
  183. is_asmoperator:=false;
  184. end;
  185. Function tx86intreader.is_asmdirective(const s: string):boolean;
  186. var
  187. i : longint;
  188. Begin
  189. for i:=0 to _count_asmdirectives do
  190. if s=_asmdirectives[i] then
  191. begin
  192. actasmtoken:=tasmtoken(longint(firstdirective)+i);
  193. is_asmdirective:=true;
  194. exit;
  195. end;
  196. is_asmdirective:=false;
  197. end;
  198. function tx86intreader.is_register(const s:string):boolean;
  199. begin
  200. is_register:=false;
  201. actasmregister:=masm_regnum_search(lower(s));
  202. { don't acceps "flags" as register name in an instruction }
  203. if (getsupreg(actasmregister)=RS_DEFAULTFLAGS) and (getregtype(actasmregister)=getregtype(NR_DEFAULTFLAGS)) then
  204. actasmregister:=NR_NO;
  205. if actasmregister<>NR_NO then
  206. begin
  207. is_register:=true;
  208. actasmtoken:=AS_REGISTER;
  209. end;
  210. end;
  211. function tx86intreader.is_locallabel(const s:string):boolean;
  212. begin
  213. is_locallabel:=(length(s)>1) and (s[1]='@');
  214. end;
  215. Procedure tx86intreader.GetToken;
  216. var
  217. len : longint;
  218. forcelabel : boolean;
  219. srsym : tsym;
  220. srsymtable : TSymtable;
  221. begin
  222. c:=scanner.c;
  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','_','@']) or
  241. { TP7 also allows $&? characters in local labels }
  242. (forcelabel and (c in ['$','&','?'])) do
  243. begin
  244. { if there is an at_sign, then this must absolutely be a label }
  245. if c = '@' then
  246. forcelabel:=TRUE;
  247. inc(len);
  248. actasmpattern[len]:=c;
  249. c:=current_scanner.asmgetchar;
  250. end;
  251. actasmpattern[0]:=chr(len);
  252. actasmpattern_origcase:=actasmpattern;
  253. uppervar(actasmpattern);
  254. { allow spaces }
  255. while (c in [' ',#9]) do
  256. c:=current_scanner.asmgetchar;
  257. { label ? }
  258. if c = ':' then
  259. begin
  260. if actasmpattern[1]='@' then
  261. actasmtoken:=AS_LLABEL
  262. else
  263. actasmtoken:=AS_LABEL;
  264. { let us point to the next character }
  265. c:=current_scanner.asmgetchar;
  266. firsttoken:=true;
  267. exit;
  268. end;
  269. { Are we trying to create an identifier with }
  270. { an at-sign...? }
  271. if forcelabel then
  272. Message(asmr_e_none_label_contain_at);
  273. { opcode ? }
  274. If is_asmopcode(actasmpattern) then
  275. Begin
  276. { check if we are in an expression }
  277. { then continue with asm directives }
  278. if not inexpression then
  279. exit;
  280. end;
  281. if is_asmdirective(actasmpattern) then
  282. exit;
  283. message1(asmr_e_unknown_opcode,actasmpattern);
  284. actasmtoken:=AS_NONE;
  285. exit;
  286. end
  287. else { else firsttoken }
  288. begin
  289. case c of
  290. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  291. { - @Result, @Code or @Data special variables. }
  292. begin
  293. actasmpattern:=c;
  294. c:=current_scanner.asmgetchar;
  295. while c in ['A'..'Z','a'..'z','0'..'9','_','@','$','&','?'] do
  296. begin
  297. actasmpattern:=actasmpattern + c;
  298. c:=current_scanner.asmgetchar;
  299. end;
  300. actasmpattern_origcase:=actasmpattern;
  301. uppervar(actasmpattern);
  302. actasmtoken:=AS_ID;
  303. exit;
  304. end;
  305. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  306. begin
  307. actasmpattern:=c;
  308. c:=current_scanner.asmgetchar;
  309. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  310. begin
  311. actasmpattern:=actasmpattern + c;
  312. c:=current_scanner.asmgetchar;
  313. end;
  314. actasmpattern_origcase:=actasmpattern;
  315. uppervar(actasmpattern);
  316. { after prefix (or segment override) we allow also a new opcode }
  317. If (is_prefix(actopcode) or is_override(actopcode)) and is_asmopcode(actasmpattern) then
  318. Begin
  319. { if we are not in a constant }
  320. { expression than this is an }
  321. { opcode. }
  322. if not inexpression then
  323. exit;
  324. end;
  325. { support st(X) for fpu registers }
  326. if (actasmpattern = 'ST') and (c='(') then
  327. Begin
  328. actasmpattern:=actasmpattern+c;
  329. c:=current_scanner.asmgetchar;
  330. { allow spaces }
  331. while (c in [' ',#9]) do
  332. c:=current_scanner.asmgetchar;
  333. if c in ['0'..'7'] then
  334. actasmpattern:=actasmpattern + c
  335. else
  336. Message(asmr_e_invalid_fpu_register);
  337. c:=current_scanner.asmgetchar;
  338. { allow spaces }
  339. while (c in [' ',#9]) do
  340. c:=current_scanner.asmgetchar;
  341. if c <> ')' then
  342. Message(asmr_e_invalid_fpu_register)
  343. else
  344. Begin
  345. actasmpattern:=actasmpattern + c;
  346. c:=current_scanner.asmgetchar;
  347. end;
  348. end;
  349. if is_asmdirective(actasmpattern) then
  350. exit;
  351. if is_asmoperator(actasmpattern) then
  352. exit;
  353. if is_register(actasmpattern) then
  354. exit;
  355. { allow spaces }
  356. while (c in [' ',#9]) do
  357. c:=current_scanner.asmgetchar;
  358. { if next is a '.' and this is a unitsym then we also need to
  359. parse the identifier }
  360. if (c='.') then
  361. begin
  362. asmsearchsym(actasmpattern,srsym,srsymtable);
  363. if assigned(srsym) and
  364. (srsym.typ=unitsym) and
  365. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  366. srsym.owner.iscurrentunit then
  367. begin
  368. { Add . to create System.Identifier }
  369. actasmpattern:=actasmpattern+c;
  370. c:=current_scanner.asmgetchar;
  371. { Delphi allows System.@Halt, just ignore the @ }
  372. if c='@' then
  373. c:=current_scanner.asmgetchar;
  374. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  375. begin
  376. actasmpattern:=actasmpattern + upcase(c);
  377. c:=current_scanner.asmgetchar;
  378. end;
  379. end;
  380. end;
  381. actasmtoken:=AS_ID;
  382. exit;
  383. end;
  384. '''' : { string or character }
  385. begin
  386. actasmpattern:='';
  387. repeat
  388. if c = '''' then
  389. begin
  390. c:=current_scanner.asmgetchar;
  391. if c in [#10,#13] then
  392. begin
  393. Message(scan_f_string_exceeds_line);
  394. break;
  395. end;
  396. repeat
  397. if c='''' then
  398. begin
  399. c:=current_scanner.asmgetchar;
  400. if c='''' then
  401. begin
  402. actasmpattern:=actasmpattern+'''';
  403. c:=current_scanner.asmgetchar;
  404. if c in [#10,#13] then
  405. begin
  406. Message(scan_f_string_exceeds_line);
  407. break;
  408. end;
  409. end
  410. else
  411. break;
  412. end
  413. else
  414. begin
  415. actasmpattern:=actasmpattern+c;
  416. c:=current_scanner.asmgetchar;
  417. if c in [#10,#13] then
  418. begin
  419. Message(scan_f_string_exceeds_line);
  420. break
  421. end;
  422. end;
  423. until false; { end repeat }
  424. end
  425. else
  426. break; { end if }
  427. until false;
  428. actasmtoken:=AS_STRING;
  429. exit;
  430. end;
  431. '"' : { string or character }
  432. begin
  433. actasmpattern:='';
  434. repeat
  435. if c = '"' then
  436. begin
  437. c:=current_scanner.asmgetchar;
  438. if c in [#10,#13] then
  439. begin
  440. Message(scan_f_string_exceeds_line);
  441. break;
  442. end;
  443. repeat
  444. if c='"' then
  445. begin
  446. c:=current_scanner.asmgetchar;
  447. if c='"' then
  448. begin
  449. actasmpattern:=actasmpattern+'"';
  450. c:=current_scanner.asmgetchar;
  451. if c in [#10,#13] then
  452. begin
  453. Message(scan_f_string_exceeds_line);
  454. break;
  455. end;
  456. end
  457. else
  458. break;
  459. end
  460. else
  461. begin
  462. actasmpattern:=actasmpattern+c;
  463. c:=current_scanner.asmgetchar;
  464. if c in [#10,#13] then
  465. begin
  466. Message(scan_f_string_exceeds_line);
  467. break
  468. end;
  469. end;
  470. until false; { end repeat }
  471. end
  472. else
  473. break; { end if }
  474. until false;
  475. actasmtoken:=AS_STRING;
  476. exit;
  477. end;
  478. '$' :
  479. begin
  480. c:=current_scanner.asmgetchar;
  481. while c in ['0'..'9','A'..'F','a'..'f'] do
  482. begin
  483. actasmpattern:=actasmpattern + c;
  484. c:=current_scanner.asmgetchar;
  485. end;
  486. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  487. actasmtoken:=AS_INTNUM;
  488. exit;
  489. end;
  490. '&' : { identifier }
  491. begin
  492. actasmpattern:='';
  493. c:=current_scanner.asmgetchar;
  494. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  495. begin
  496. actasmpattern:=actasmpattern + c;
  497. c:=current_scanner.asmgetchar;
  498. end;
  499. actasmpattern_origcase:=actasmpattern;
  500. uppervar(actasmpattern);
  501. actasmtoken:=AS_ID;
  502. exit;
  503. end;
  504. ',' :
  505. begin
  506. actasmtoken:=AS_COMMA;
  507. c:=current_scanner.asmgetchar;
  508. exit;
  509. end;
  510. '[' :
  511. begin
  512. actasmtoken:=AS_LBRACKET;
  513. c:=current_scanner.asmgetchar;
  514. exit;
  515. end;
  516. ']' :
  517. begin
  518. actasmtoken:=AS_RBRACKET;
  519. c:=current_scanner.asmgetchar;
  520. exit;
  521. end;
  522. '(' :
  523. begin
  524. c:=current_scanner.asmgetchar;
  525. if c='*' then
  526. begin
  527. current_scanner.skipoldtpcomment(true);
  528. GetToken;
  529. end
  530. else
  531. actasmtoken:=AS_LPAREN;
  532. exit;
  533. end;
  534. ')' :
  535. begin
  536. actasmtoken:=AS_RPAREN;
  537. c:=current_scanner.asmgetchar;
  538. exit;
  539. end;
  540. ':' :
  541. begin
  542. actasmtoken:=AS_COLON;
  543. c:=current_scanner.asmgetchar;
  544. exit;
  545. end;
  546. '.' :
  547. begin
  548. c:=current_scanner.asmgetchar;
  549. {$ifdef x86_64}
  550. if c='.' then
  551. begin
  552. actasmpattern:='..';
  553. c:=current_scanner.asmgetchar;
  554. repeat
  555. actasmpattern:=actasmpattern+c;
  556. c:=current_scanner.asmgetchar;
  557. until not(c in ['A'..'Z','a'..'z','0'..'9','_']);
  558. if upper(actasmpattern)<>'..GOTPCREL' then
  559. begin
  560. actasmtoken:=AS_ID;
  561. consume(AS___GOTPCREL);
  562. end;
  563. actasmtoken:=AS___GOTPCREL
  564. end
  565. else
  566. {$endif x86_64}
  567. actasmtoken:=AS_DOT;
  568. exit;
  569. end;
  570. '+' :
  571. begin
  572. actasmtoken:=AS_PLUS;
  573. c:=current_scanner.asmgetchar;
  574. exit;
  575. end;
  576. '-' :
  577. begin
  578. actasmtoken:=AS_MINUS;
  579. c:=current_scanner.asmgetchar;
  580. exit;
  581. end;
  582. '*' :
  583. begin
  584. actasmtoken:=AS_STAR;
  585. c:=current_scanner.asmgetchar;
  586. exit;
  587. end;
  588. '/' :
  589. begin
  590. c:=current_scanner.asmgetchar;
  591. if c='/' then
  592. begin
  593. current_scanner.skipdelphicomment;
  594. GetToken;
  595. end
  596. else
  597. actasmtoken:=AS_SLASH;
  598. exit;
  599. end;
  600. '0'..'9':
  601. begin
  602. actasmpattern:=c;
  603. c:=current_scanner.asmgetchar;
  604. { Get the possible characters }
  605. while c in ['0'..'9','A'..'F','a'..'f'] do
  606. begin
  607. actasmpattern:=actasmpattern + c;
  608. c:=current_scanner.asmgetchar;
  609. end;
  610. { Get ending character }
  611. actasmpattern_origcase:=actasmpattern;
  612. uppervar(actasmpattern);
  613. c:=upcase(c);
  614. { possibly a binary number. }
  615. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  616. Begin
  617. { Delete the last binary specifier }
  618. delete(actasmpattern,length(actasmpattern),1);
  619. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  620. actasmtoken:=AS_INTNUM;
  621. exit;
  622. end
  623. else
  624. Begin
  625. case c of
  626. 'O' :
  627. Begin
  628. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  629. actasmtoken:=AS_INTNUM;
  630. c:=current_scanner.asmgetchar;
  631. exit;
  632. end;
  633. 'H' :
  634. Begin
  635. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  636. actasmtoken:=AS_INTNUM;
  637. c:=current_scanner.asmgetchar;
  638. exit;
  639. end;
  640. else { must be an integer number }
  641. begin
  642. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  643. actasmtoken:=AS_INTNUM;
  644. exit;
  645. end;
  646. end;
  647. end;
  648. end;
  649. #13,#10:
  650. begin
  651. current_scanner.linebreak;
  652. c:=current_scanner.asmgetchar;
  653. firsttoken:=TRUE;
  654. actasmtoken:=AS_SEPARATOR;
  655. exit;
  656. end;
  657. ';':
  658. begin
  659. c:=current_scanner.asmgetchar;
  660. firsttoken:=TRUE;
  661. actasmtoken:=AS_SEPARATOR;
  662. exit;
  663. end;
  664. '{':
  665. begin
  666. current_scanner.skipcomment(true);
  667. GetToken;
  668. end;
  669. else
  670. current_scanner.illegal_char(c);
  671. end;
  672. end;
  673. end;
  674. function tx86intreader.consume(t : tasmtoken):boolean;
  675. begin
  676. Consume:=true;
  677. if t<>actasmtoken then
  678. begin
  679. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  680. Consume:=false;
  681. end;
  682. repeat
  683. gettoken;
  684. until actasmtoken<>AS_NONE;
  685. end;
  686. procedure tx86intreader.RecoverConsume(allowcomma:boolean);
  687. begin
  688. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  689. begin
  690. if allowcomma and (actasmtoken=AS_COMMA) then
  691. break;
  692. Consume(actasmtoken);
  693. end;
  694. end;
  695. {*****************************************************************************
  696. Parsing Helpers
  697. *****************************************************************************}
  698. { This routine builds up a record offset after a AS_DOT
  699. token is encountered.
  700. On entry actasmtoken should be equal to AS_DOT }
  701. Procedure tx86intreader.BuildRecordOffsetSize(const expr: string;var offset:tcgint;var size:tcgint; var mangledname: string; needvmtofs: boolean; out hastypecast: boolean);
  702. var
  703. s: string;
  704. Begin
  705. offset:=0;
  706. size:=0;
  707. hastypecast:=false;
  708. s:=expr;
  709. while (actasmtoken=AS_DOT) do
  710. begin
  711. Consume(AS_DOT);
  712. if actasmtoken in [AS_BYTE,AS_ID,AS_WORD,AS_DWORD,AS_QWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD,AS_REGISTER] then
  713. begin
  714. s:=s+'.'+actasmpattern;
  715. consume(actasmtoken);
  716. end
  717. else
  718. begin
  719. Consume(AS_ID);
  720. RecoverConsume(true);
  721. break;
  722. end;
  723. end;
  724. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs,hastypecast) then
  725. Message(asmr_e_building_record_offset);
  726. end;
  727. Procedure tx86intreader.BuildConstSymbolExpression(needofs,isref,startingminus:boolean;var value:tcgint;var asmsym:string;var asmsymtyp:TAsmsymtype;out isseg,is_farproc_entry,hasofs:boolean);
  728. var
  729. tempstr,expr,hs,mangledname : string;
  730. parenlevel : longint;
  731. l,k : tcgint;
  732. hasparen,
  733. errorflag,
  734. needvmtofs : boolean;
  735. prevtok : tasmtoken;
  736. hl : tasmlabel;
  737. hssymtyp : Tasmsymtype;
  738. def : tdef;
  739. sym : tsym;
  740. srsymtable : TSymtable;
  741. hastypecast : boolean;
  742. Begin
  743. { reset }
  744. value:=0;
  745. asmsym:='';
  746. asmsymtyp:=AT_DATA;
  747. isseg:=false;
  748. is_farproc_entry:=FALSE;
  749. hasofs:=FALSE;
  750. errorflag:=FALSE;
  751. tempstr:='';
  752. expr:='';
  753. if startingminus then
  754. expr:='-';
  755. inexpression:=TRUE;
  756. parenlevel:=0;
  757. sym:=nil;
  758. needvmtofs:=FALSE;
  759. Repeat
  760. { Support ugly delphi constructs like: [ECX].1+2[EDX] }
  761. if isref and (actasmtoken=AS_LBRACKET) then
  762. break;
  763. Case actasmtoken of
  764. AS_LPAREN:
  765. Begin
  766. Consume(AS_LPAREN);
  767. expr:=expr + '(';
  768. inc(parenlevel);
  769. end;
  770. AS_RPAREN:
  771. Begin
  772. { Keep the AS_PAREN in actasmtoken, it is maybe a typecast }
  773. if parenlevel=0 then
  774. break;
  775. Consume(AS_RPAREN);
  776. expr:=expr + ')';
  777. dec(parenlevel);
  778. end;
  779. AS_SHL:
  780. Begin
  781. Consume(AS_SHL);
  782. expr:=expr + '<';
  783. end;
  784. AS_SHR:
  785. Begin
  786. Consume(AS_SHR);
  787. expr:=expr + '>';
  788. end;
  789. AS_SLASH:
  790. Begin
  791. Consume(AS_SLASH);
  792. expr:=expr + '/';
  793. end;
  794. AS_MOD:
  795. Begin
  796. Consume(AS_MOD);
  797. expr:=expr + '%';
  798. end;
  799. AS_STAR:
  800. Begin
  801. Consume(AS_STAR);
  802. if isref and (actasmtoken=AS_REGISTER) then
  803. break;
  804. expr:=expr + '*';
  805. end;
  806. AS_PLUS:
  807. Begin
  808. Consume(AS_PLUS);
  809. if isref and (actasmtoken=AS_REGISTER) then
  810. break;
  811. expr:=expr + '+';
  812. end;
  813. AS_MINUS:
  814. Begin
  815. Consume(AS_MINUS);
  816. expr:=expr + '-';
  817. end;
  818. AS_AND:
  819. Begin
  820. Consume(AS_AND);
  821. expr:=expr + '&';
  822. end;
  823. AS_NOT:
  824. Begin
  825. Consume(AS_NOT);
  826. expr:=expr + '~';
  827. end;
  828. AS_XOR:
  829. Begin
  830. Consume(AS_XOR);
  831. expr:=expr + '^';
  832. end;
  833. AS_OR:
  834. Begin
  835. Consume(AS_OR);
  836. expr:=expr + '|';
  837. end;
  838. AS_INTNUM:
  839. Begin
  840. expr:=expr + actasmpattern;
  841. Consume(AS_INTNUM);
  842. end;
  843. {$ifdef i8086}
  844. AS_SEG:
  845. begin
  846. isseg:=true;
  847. Consume(actasmtoken);
  848. if actasmtoken<>AS_ID then
  849. Message(asmr_e_seg_without_identifier);
  850. end;
  851. {$endif i8086}
  852. AS_VMTOFFSET,
  853. AS_OFFSET:
  854. begin
  855. if (actasmtoken = AS_OFFSET) then
  856. begin
  857. needofs:=true;
  858. hasofs:=true;
  859. end
  860. else
  861. needvmtofs:=true;
  862. Consume(actasmtoken);
  863. if actasmtoken<>AS_ID then
  864. Message(asmr_e_offset_without_identifier);
  865. end;
  866. AS_SIZEOF,
  867. AS_TYPE:
  868. begin
  869. l:=0;
  870. hasparen:=false;
  871. Consume(actasmtoken);
  872. if actasmtoken=AS_LPAREN then
  873. begin
  874. hasparen:=true;
  875. Consume(AS_LPAREN);
  876. end;
  877. if actasmtoken<>AS_ID then
  878. Message(asmr_e_type_without_identifier)
  879. else
  880. begin
  881. tempstr:=actasmpattern;
  882. Consume(AS_ID);
  883. if actasmtoken=AS_DOT then
  884. begin
  885. BuildRecordOffsetSize(tempstr,k,l,mangledname,false,hastypecast);
  886. if mangledname<>'' then
  887. { procsym }
  888. Message(asmr_e_wrong_sym_type);
  889. if hastypecast then
  890. end
  891. else
  892. begin
  893. asmsearchsym(tempstr,sym,srsymtable);
  894. if assigned(sym) then
  895. begin
  896. case sym.typ of
  897. staticvarsym,
  898. localvarsym,
  899. paravarsym :
  900. l:=tabstractvarsym(sym).getsize;
  901. typesym :
  902. l:=ttypesym(sym).typedef.size;
  903. else
  904. Message(asmr_e_wrong_sym_type);
  905. end;
  906. end
  907. else
  908. Message1(sym_e_unknown_id,tempstr);
  909. end;
  910. end;
  911. str(l, tempstr);
  912. expr:=expr + tempstr;
  913. if hasparen then
  914. Consume(AS_RPAREN);
  915. end;
  916. AS_PTR :
  917. begin
  918. { Support ugly delphi constructs like <constant> PTR [ref] }
  919. break;
  920. end;
  921. AS_STRING:
  922. begin
  923. l:=0;
  924. case Length(actasmpattern) of
  925. 1 :
  926. l:=ord(actasmpattern[1]);
  927. 2 :
  928. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  929. 3 :
  930. l:=ord(actasmpattern[3]) +
  931. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  932. 4 :
  933. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  934. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  935. else
  936. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  937. end;
  938. str(l, tempstr);
  939. expr:=expr + tempstr;
  940. Consume(AS_STRING);
  941. end;
  942. AS_ID:
  943. begin
  944. hs:='';
  945. hssymtyp:=AT_DATA;
  946. def:=nil;
  947. tempstr:=actasmpattern;
  948. prevtok:=prevasmtoken;
  949. { stop parsing a constant expression if we find an opcode after a
  950. non-operator like "db $66 mov eax,ebx" }
  951. if (prevtok in [AS_ID,AS_INTNUM,AS_RPAREN]) and
  952. is_asmopcode(actasmpattern) then
  953. break;
  954. consume(AS_ID);
  955. if (tempstr='@CODE') or (tempstr='@DATA') then
  956. begin
  957. if asmsym='' then
  958. begin
  959. asmsym:=tempstr;
  960. asmsymtyp:=AT_SECTION;
  961. end
  962. else
  963. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  964. end
  965. else if SearchIConstant(tempstr,l) then
  966. begin
  967. str(l, tempstr);
  968. expr:=expr + tempstr;
  969. end
  970. else
  971. begin
  972. if is_locallabel(tempstr) then
  973. begin
  974. CreateLocalLabel(tempstr,hl,false);
  975. hs:=hl.name;
  976. hssymtyp:=AT_FUNCTION;
  977. end
  978. else
  979. if SearchLabel(tempstr,hl,false) then
  980. begin
  981. hs:=hl.name;
  982. hssymtyp:=AT_FUNCTION;
  983. end
  984. else
  985. begin
  986. asmsearchsym(tempstr,sym,srsymtable);
  987. if assigned(sym) then
  988. begin
  989. case sym.typ of
  990. staticvarsym :
  991. begin
  992. hs:=tstaticvarsym(sym).mangledname;
  993. def:=tstaticvarsym(sym).vardef;
  994. end;
  995. localvarsym,
  996. paravarsym :
  997. begin
  998. Message(asmr_e_no_local_or_para_allowed);
  999. end;
  1000. procsym :
  1001. begin
  1002. if Tprocsym(sym).ProcdefList.Count>1 then
  1003. Message(asmr_w_calling_overload_func);
  1004. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1005. {$ifdef i8086}
  1006. is_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  1007. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  1008. {$endif i8086}
  1009. hssymtyp:=AT_FUNCTION;
  1010. end;
  1011. typesym :
  1012. begin
  1013. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1014. Message(asmr_e_wrong_sym_type);
  1015. end;
  1016. fieldvarsym :
  1017. begin
  1018. tempstr:=upper(tdef(sym.owner.defowner).GetTypeName)+'.'+tempstr;
  1019. end;
  1020. else
  1021. Message(asmr_e_wrong_sym_type);
  1022. end;
  1023. end
  1024. else
  1025. Message1(sym_e_unknown_id,tempstr);
  1026. end;
  1027. { symbol found? }
  1028. if hs<>'' then
  1029. begin
  1030. if asmsym='' then
  1031. begin
  1032. asmsym:=hs;
  1033. asmsymtyp:=hssymtyp;
  1034. end
  1035. else
  1036. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1037. if (expr='') or (expr[length(expr)]='+') then
  1038. begin
  1039. { don't remove the + if there could be a record field }
  1040. if actasmtoken<>AS_DOT then
  1041. delete(expr,length(expr),1);
  1042. end
  1043. else
  1044. if needofs then
  1045. begin
  1046. if (prevtok<>AS_OFFSET) then
  1047. Message(asmr_e_need_offset);
  1048. end
  1049. else
  1050. Message(asmr_e_only_add_relocatable_symbol);
  1051. end;
  1052. if (actasmtoken=AS_DOT) or
  1053. (assigned(sym) and
  1054. (sym.typ = fieldvarsym) and
  1055. not(sp_static in sym.symoptions)) then
  1056. begin
  1057. BuildRecordOffsetSize(tempstr,l,k,hs,needvmtofs,hastypecast);
  1058. if hs <> '' then
  1059. hssymtyp:=AT_FUNCTION
  1060. else
  1061. begin
  1062. str(l, tempstr);
  1063. expr:=expr + tempstr;
  1064. end
  1065. end
  1066. else
  1067. begin
  1068. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1069. delete(expr,length(expr),1);
  1070. end;
  1071. if (actasmtoken=AS_LBRACKET) and
  1072. assigned(def) and
  1073. (def.typ=arraydef) then
  1074. begin
  1075. consume(AS_LBRACKET);
  1076. l:=BuildConstExpression;
  1077. if l<tarraydef(def).lowrange then
  1078. begin
  1079. Message(asmr_e_constant_out_of_bounds);
  1080. l:=0;
  1081. end
  1082. else
  1083. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  1084. str(l, tempstr);
  1085. expr:=expr + '+' + tempstr;
  1086. consume(AS_RBRACKET);
  1087. end;
  1088. end;
  1089. { check if there are wrong operator used like / or mod etc. }
  1090. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  1091. Message(asmr_e_only_add_relocatable_symbol);
  1092. end;
  1093. AS_ALIGN,
  1094. AS_DB,
  1095. AS_DW,
  1096. AS_DD,
  1097. AS_DQ,
  1098. AS_END,
  1099. AS_RBRACKET,
  1100. AS_SEPARATOR,
  1101. AS_COMMA,
  1102. AS_COLON:
  1103. break;
  1104. else
  1105. begin
  1106. { write error only once. }
  1107. if not errorflag then
  1108. Message(asmr_e_invalid_constant_expression);
  1109. { consume tokens until we find COMMA or SEPARATOR }
  1110. Consume(actasmtoken);
  1111. errorflag:=TRUE;
  1112. end;
  1113. end;
  1114. Until false;
  1115. { calculate expression }
  1116. if not ErrorFlag then
  1117. value:=CalculateExpression(expr)
  1118. else
  1119. value:=0;
  1120. { no longer in an expression }
  1121. inexpression:=FALSE;
  1122. end;
  1123. Function tx86intreader.BuildConstExpression:aint;
  1124. var
  1125. l : tcgint;
  1126. hs : string;
  1127. hssymtyp : TAsmsymtype;
  1128. isseg,is_farproc_entry,hasofs : boolean;
  1129. begin
  1130. BuildConstSymbolExpression(false,false,false,l,hs,hssymtyp,isseg,is_farproc_entry,hasofs);
  1131. if hs<>'' then
  1132. Message(asmr_e_relocatable_symbol_not_allowed);
  1133. BuildConstExpression:=l;
  1134. end;
  1135. Function tx86intreader.BuildRefConstExpression(startingminus:boolean):aint;
  1136. var
  1137. l : tcgint;
  1138. hs : string;
  1139. hssymtyp : TAsmsymtype;
  1140. isseg,is_farproc_entry,hasofs : boolean;
  1141. begin
  1142. BuildConstSymbolExpression(false,true,startingminus,l,hs,hssymtyp,isseg,is_farproc_entry,hasofs);
  1143. if hs<>'' then
  1144. Message(asmr_e_relocatable_symbol_not_allowed);
  1145. BuildRefConstExpression:=l;
  1146. end;
  1147. procedure tx86intreader.BuildReference(oper : tx86operand);
  1148. var
  1149. scale : byte;
  1150. k,l : tcgint;
  1151. tempstr,hs : string;
  1152. tempsymtyp : tasmsymtype;
  1153. code : integer;
  1154. hreg : tregister;
  1155. GotStar,GotOffset,HadVar,
  1156. GotPlus,Negative : boolean;
  1157. hl : tasmlabel;
  1158. isseg: boolean;
  1159. is_farproc_entry,hasofs,
  1160. hastypecast: boolean;
  1161. Begin
  1162. Consume(AS_LBRACKET);
  1163. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  1164. oper.InitRef;
  1165. GotStar:=false;
  1166. GotPlus:=true;
  1167. GotOffset:=false;
  1168. Negative:=false;
  1169. Scale:=0;
  1170. repeat
  1171. if GotOffset and (actasmtoken<>AS_ID) then
  1172. Message(asmr_e_invalid_reference_syntax);
  1173. Case actasmtoken of
  1174. AS_ID, { Constant reference expression OR variable reference expression }
  1175. AS_VMTOFFSET:
  1176. Begin
  1177. if not GotPlus then
  1178. Message(asmr_e_invalid_reference_syntax);
  1179. GotStar:=false;
  1180. GotPlus:=false;
  1181. if (actasmtoken = AS_VMTOFFSET) or
  1182. (SearchIConstant(actasmpattern,l) or
  1183. SearchRecordType(actasmpattern)) then
  1184. begin
  1185. l:=BuildRefConstExpression(negative);
  1186. negative:=false; { "l" was negated if necessary }
  1187. GotPlus:=(prevasmtoken=AS_PLUS);
  1188. GotStar:=(prevasmtoken=AS_STAR);
  1189. case oper.opr.typ of
  1190. OPR_LOCAL :
  1191. begin
  1192. if GotStar then
  1193. Message(asmr_e_invalid_reference_syntax);
  1194. Inc(oper.opr.localsymofs,l);
  1195. end;
  1196. OPR_REFERENCE :
  1197. begin
  1198. if GotStar then
  1199. oper.opr.ref.scalefactor:=l
  1200. else
  1201. Inc(oper.opr.ref.offset,l);
  1202. end;
  1203. end;
  1204. end
  1205. else
  1206. Begin
  1207. if negative and not oper.hasvar then
  1208. Message(asmr_e_only_add_relocatable_symbol)
  1209. else if oper.hasvar and not GotOffset and
  1210. (not negative or assigned(oper.opr.ref.relsymbol)) then
  1211. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1212. HadVar:=oper.hasvar and GotOffset;
  1213. tempstr:=actasmpattern;
  1214. Consume(AS_ID);
  1215. { typecasting? }
  1216. if (actasmtoken=AS_LPAREN) and
  1217. SearchType(tempstr,l) then
  1218. begin
  1219. oper.hastype:=true;
  1220. oper.typesize:=l;
  1221. Consume(AS_LPAREN);
  1222. BuildOperand(oper,true);
  1223. Consume(AS_RPAREN);
  1224. end
  1225. else
  1226. if is_locallabel(tempstr) then
  1227. begin
  1228. CreateLocalLabel(tempstr,hl,false);
  1229. oper.InitRef;
  1230. oper.haslabelref:=true;
  1231. if not negative then
  1232. begin
  1233. oper.opr.ref.symbol:=hl;
  1234. oper.hasvar:=true;
  1235. end
  1236. else
  1237. oper.opr.ref.relsymbol:=hl;
  1238. {$ifdef i8086}
  1239. if oper.opr.ref.segment=NR_NO then
  1240. oper.opr.ref.segment:=NR_CS;
  1241. {$endif i8086}
  1242. end
  1243. else
  1244. if oper.SetupVar(tempstr,GotOffset) then
  1245. begin
  1246. { convert OPR_LOCAL register para into a reference base }
  1247. if (oper.opr.typ=OPR_LOCAL) and
  1248. AsmRegisterPara(oper.opr.localsym) then
  1249. oper.InitRefConvertLocal
  1250. else
  1251. begin
  1252. {$ifdef x86_64}
  1253. if actasmtoken=AS_WRT then
  1254. begin
  1255. if (oper.opr.typ=OPR_REFERENCE) then
  1256. begin
  1257. Consume(AS_WRT);
  1258. Consume(AS___GOTPCREL);
  1259. if (oper.opr.ref.base<>NR_NO) or
  1260. (oper.opr.ref.index<>NR_NO) or
  1261. (oper.opr.ref.offset<>0) then
  1262. Message(asmr_e_wrong_gotpcrel_intel_syntax);
  1263. if tf_no_pic_supported in target_info.flags then
  1264. Message(asmr_e_no_gotpcrel_support);
  1265. oper.opr.ref.refaddr:=addr_pic;
  1266. oper.opr.ref.base:=NR_RIP;
  1267. end
  1268. else
  1269. message(asmr_e_invalid_reference_syntax);
  1270. end;
  1271. {$endif x86_64}
  1272. end;
  1273. end
  1274. else
  1275. Message1(sym_e_unknown_id,tempstr);
  1276. { record.field ? }
  1277. if actasmtoken=AS_DOT then
  1278. begin
  1279. BuildRecordOffsetSize(tempstr,l,k,hs,false,hastypecast);
  1280. if (hs<>'') then
  1281. Message(asmr_e_invalid_symbol_ref);
  1282. case oper.opr.typ of
  1283. OPR_LOCAL :
  1284. inc(oper.opr.localsymofs,l);
  1285. OPR_REFERENCE :
  1286. inc(oper.opr.ref.offset,l);
  1287. end;
  1288. if hastypecast then
  1289. oper.hastype:=true;
  1290. oper.SetSize(k,false);
  1291. end;
  1292. if GotOffset then
  1293. begin
  1294. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1295. begin
  1296. if (oper.opr.typ=OPR_REFERENCE) then
  1297. oper.opr.ref.base:=NR_NO;
  1298. oper.hasvar:=hadvar;
  1299. end
  1300. else
  1301. begin
  1302. if oper.hasvar and hadvar then
  1303. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1304. { should we allow ?? }
  1305. end;
  1306. end;
  1307. end;
  1308. GotOffset:=false;
  1309. end;
  1310. AS_PLUS :
  1311. Begin
  1312. Consume(AS_PLUS);
  1313. Negative:=false;
  1314. GotPlus:=true;
  1315. GotStar:=false;
  1316. Scale:=0;
  1317. end;
  1318. AS_DOT :
  1319. Begin
  1320. { Handle like a + }
  1321. Consume(AS_DOT);
  1322. Negative:=false;
  1323. GotPlus:=true;
  1324. GotStar:=false;
  1325. Scale:=0;
  1326. end;
  1327. AS_MINUS :
  1328. begin
  1329. Consume(AS_MINUS);
  1330. Negative:=true;
  1331. GotPlus:=true;
  1332. GotStar:=false;
  1333. Scale:=0;
  1334. end;
  1335. AS_STAR : { Scaling, with eax*4 order }
  1336. begin
  1337. Consume(AS_STAR);
  1338. hs:='';
  1339. l:=0;
  1340. case actasmtoken of
  1341. AS_ID,
  1342. AS_LPAREN :
  1343. l:=BuildConstExpression;
  1344. AS_INTNUM:
  1345. Begin
  1346. hs:=actasmpattern;
  1347. Consume(AS_INTNUM);
  1348. end;
  1349. AS_REGISTER :
  1350. begin
  1351. case oper.opr.typ of
  1352. OPR_REFERENCE :
  1353. begin
  1354. if oper.opr.ref.scalefactor=0 then
  1355. begin
  1356. if scale<>0 then
  1357. begin
  1358. oper.opr.ref.scalefactor:=scale;
  1359. scale:=0;
  1360. end
  1361. else
  1362. Message(asmr_e_wrong_scale_factor);
  1363. end
  1364. else
  1365. Message(asmr_e_invalid_reference_syntax);
  1366. end;
  1367. OPR_LOCAL :
  1368. begin
  1369. if oper.opr.localscale=0 then
  1370. begin
  1371. if scale<>0 then
  1372. begin
  1373. oper.opr.localscale:=scale;
  1374. scale:=0;
  1375. end
  1376. else
  1377. Message(asmr_e_wrong_scale_factor);
  1378. end
  1379. else
  1380. Message(asmr_e_invalid_reference_syntax);
  1381. end;
  1382. end;
  1383. end;
  1384. else
  1385. Message(asmr_e_invalid_reference_syntax);
  1386. end;
  1387. if actasmtoken<>AS_REGISTER then
  1388. begin
  1389. if hs<>'' then
  1390. val(hs,l,code);
  1391. case oper.opr.typ of
  1392. OPR_REFERENCE :
  1393. oper.opr.ref.scalefactor:=l;
  1394. OPR_LOCAL :
  1395. oper.opr.localscale:=l;
  1396. end;
  1397. if l>9 then
  1398. Message(asmr_e_wrong_scale_factor);
  1399. end;
  1400. GotPlus:=false;
  1401. GotStar:=false;
  1402. end;
  1403. AS_REGISTER :
  1404. begin
  1405. if not((GotPlus and (not Negative)) or
  1406. GotStar) then
  1407. Message(asmr_e_invalid_reference_syntax);
  1408. hreg:=actasmregister;
  1409. Consume(AS_REGISTER);
  1410. { this register will be the index:
  1411. 1. just read a *
  1412. 2. next token is a *
  1413. 3. base register is already used }
  1414. case oper.opr.typ of
  1415. OPR_LOCAL :
  1416. begin
  1417. if (oper.opr.localindexreg<>NR_NO) then
  1418. Message(asmr_e_multiple_index);
  1419. {$ifdef x86_64}
  1420. { Locals/parameters cannot be accessed RIP-relative. Need a dedicated error message here? }
  1421. if (hreg=NR_RIP) then
  1422. Message(asmr_e_no_local_or_para_allowed);
  1423. {$endif x86_64}
  1424. oper.opr.localindexreg:=hreg;
  1425. if scale<>0 then
  1426. begin
  1427. oper.opr.localscale:=scale;
  1428. scale:=0;
  1429. end;
  1430. end;
  1431. OPR_REFERENCE :
  1432. begin
  1433. if (GotStar) or
  1434. (actasmtoken=AS_STAR) or
  1435. (oper.opr.ref.base<>NR_NO) then
  1436. begin
  1437. if (oper.opr.ref.index<>NR_NO) then
  1438. Message(asmr_e_multiple_index);
  1439. oper.opr.ref.index:=hreg;
  1440. if scale<>0 then
  1441. begin
  1442. oper.opr.ref.scalefactor:=scale;
  1443. scale:=0;
  1444. end;
  1445. end
  1446. else
  1447. begin
  1448. oper.opr.ref.base:=hreg;
  1449. {$ifdef x86_64}
  1450. { non-GOT based RIP-relative accesses are also position-independent }
  1451. if (oper.opr.ref.base=NR_RIP) and
  1452. (oper.opr.ref.refaddr<>addr_pic) then
  1453. oper.opr.ref.refaddr:=addr_pic_no_got;
  1454. {$endif x86_64}
  1455. end;
  1456. end;
  1457. end;
  1458. GotPlus:=false;
  1459. GotStar:=false;
  1460. end;
  1461. AS_OFFSET :
  1462. begin
  1463. Consume(AS_OFFSET);
  1464. GotOffset:=true;
  1465. end;
  1466. AS_TYPE,
  1467. AS_NOT,
  1468. AS_STRING,
  1469. AS_INTNUM,
  1470. AS_LPAREN : { Constant reference expression }
  1471. begin
  1472. if not GotPlus and not GotStar then
  1473. Message(asmr_e_invalid_reference_syntax);
  1474. BuildConstSymbolExpression(true,true,GotPlus and negative,l,tempstr,tempsymtyp,isseg,is_farproc_entry,hasofs);
  1475. { already handled by BuildConstSymbolExpression(); must be
  1476. handled there to avoid [reg-1+1] being interpreted as
  1477. [reg-(1+1)] }
  1478. negative:=false;
  1479. if tempstr<>'' then
  1480. begin
  1481. if GotStar then
  1482. Message(asmr_e_only_add_relocatable_symbol);
  1483. if not assigned(oper.opr.ref.symbol) then
  1484. begin
  1485. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1486. {$ifdef i8086}
  1487. if isseg then
  1488. begin
  1489. if not (oper.opr.ref.refaddr in [addr_fardataseg,addr_dgroup]) then
  1490. oper.opr.ref.refaddr:=addr_seg;
  1491. end
  1492. else if (tempsymtyp=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  1493. oper.opr.ref.segment:=NR_CS;
  1494. {$endif i8086}
  1495. end
  1496. else
  1497. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1498. end;
  1499. case oper.opr.typ of
  1500. OPR_REFERENCE :
  1501. begin
  1502. if GotStar then
  1503. oper.opr.ref.scalefactor:=l
  1504. else if (prevasmtoken = AS_STAR) then
  1505. begin
  1506. if scale<>0 then
  1507. scale:=l*scale
  1508. else
  1509. scale:=l;
  1510. end
  1511. else
  1512. begin
  1513. Inc(oper.opr.ref.offset,l);
  1514. Inc(oper.opr.constoffset,l);
  1515. end;
  1516. end;
  1517. OPR_LOCAL :
  1518. begin
  1519. if GotStar then
  1520. oper.opr.localscale:=l
  1521. else if (prevasmtoken = AS_STAR) then
  1522. begin
  1523. if scale<>0 then
  1524. scale:=l*scale
  1525. else
  1526. scale:=l;
  1527. end
  1528. else
  1529. Inc(oper.opr.localsymofs,l);
  1530. end;
  1531. end;
  1532. GotPlus:=(prevasmtoken=AS_PLUS) or
  1533. (prevasmtoken=AS_MINUS);
  1534. if GotPlus then
  1535. negative := prevasmtoken = AS_MINUS;
  1536. GotStar:=(prevasmtoken=AS_STAR);
  1537. end;
  1538. AS_RBRACKET :
  1539. begin
  1540. if GotPlus or GotStar then
  1541. Message(asmr_e_invalid_reference_syntax);
  1542. Consume(AS_RBRACKET);
  1543. break;
  1544. end;
  1545. else
  1546. Begin
  1547. Message(asmr_e_invalid_reference_syntax);
  1548. RecoverConsume(true);
  1549. break;
  1550. end;
  1551. end;
  1552. until false;
  1553. end;
  1554. Procedure tx86intreader.BuildConstantOperand(oper: tx86operand);
  1555. var
  1556. l : tcgint;
  1557. tempstr : string;
  1558. tempsymtyp : tasmsymtype;
  1559. isseg,is_farproc_entry,hasofs : boolean;
  1560. begin
  1561. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1562. Message(asmr_e_invalid_operand_type);
  1563. BuildConstSymbolExpression(true,false,false,l,tempstr,tempsymtyp,isseg,is_farproc_entry,hasofs);
  1564. {$ifdef i8086}
  1565. if tempstr='@DATA' then
  1566. begin
  1567. if not isseg then
  1568. Message(asmr_e_CODE_or_DATA_without_SEG);
  1569. oper.SetupData;
  1570. end
  1571. else if tempstr='@CODE' then
  1572. begin
  1573. if not isseg then
  1574. Message(asmr_e_CODE_or_DATA_without_SEG);
  1575. oper.SetupCode;
  1576. end
  1577. else
  1578. {$endif i8086}
  1579. if tempstr<>'' then
  1580. begin
  1581. oper.opr.typ:=OPR_SYMBOL;
  1582. oper.opr.symofs:=l;
  1583. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1584. oper.opr.symseg:=isseg;
  1585. oper.opr.sym_farproc_entry:=is_farproc_entry;
  1586. end
  1587. else
  1588. if oper.opr.typ=OPR_NONE then
  1589. begin
  1590. oper.opr.typ:=OPR_CONSTANT;
  1591. oper.opr.val:=l;
  1592. end
  1593. else
  1594. inc(oper.opr.val,l);
  1595. end;
  1596. Procedure tx86intreader.BuildOperand(oper: tx86operand;istypecast:boolean);
  1597. procedure AddLabelOperand(hl:tasmlabel);
  1598. begin
  1599. if (oper.opr.typ=OPR_NONE) and
  1600. is_calljmp(actopcode) then
  1601. begin
  1602. oper.opr.typ:=OPR_SYMBOL;
  1603. oper.opr.symbol:=hl;
  1604. end
  1605. else
  1606. begin
  1607. oper.InitRef;
  1608. oper.opr.ref.symbol:=hl;
  1609. oper.haslabelref:=true;
  1610. {$ifdef i8086}
  1611. if oper.opr.ref.segment=NR_NO then
  1612. oper.opr.ref.segment:=NR_CS;
  1613. {$endif i8086}
  1614. end;
  1615. end;
  1616. var
  1617. expr,
  1618. hs : string;
  1619. tempreg : tregister;
  1620. l : tcgint;
  1621. hl : tasmlabel;
  1622. toffset,
  1623. tsize : tcgint;
  1624. hastypecast: boolean;
  1625. begin
  1626. expr:='';
  1627. repeat
  1628. if actasmtoken=AS_DOT then
  1629. begin
  1630. if expr<>'' then
  1631. begin
  1632. BuildRecordOffsetSize(expr,toffset,tsize,hs,false,hastypecast);
  1633. if (oper.opr.typ<>OPR_NONE) and
  1634. (hs<>'') then
  1635. Message(asmr_e_wrong_sym_type);
  1636. oper.SetSize(tsize,true);
  1637. if hastypecast then
  1638. oper.hastype:=true;
  1639. { we have used the size of a field. Reset the typesize of the record }
  1640. oper.typesize:=0;
  1641. case oper.opr.typ of
  1642. OPR_LOCAL :
  1643. begin
  1644. { don't allow direct access to fields of parameters, because that
  1645. will generate buggy code. Allow it only for explicit typecasting
  1646. and when the parameter is in a register (delphi compatible) }
  1647. if (not oper.hastype) then
  1648. checklocalsubscript(oper.opr.localsym);
  1649. oper.opr.localforceref:=true;
  1650. inc(oper.opr.localsymofs,toffset);
  1651. oper.opr.localvarsize := tsize;
  1652. end;
  1653. OPR_CONSTANT :
  1654. inc(oper.opr.val,toffset);
  1655. OPR_REFERENCE :
  1656. begin
  1657. inc(oper.opr.ref.offset,toffset);
  1658. oper.opr.varsize := tsize;
  1659. end;
  1660. OPR_NONE :
  1661. begin
  1662. if (hs <> '') then
  1663. begin
  1664. oper.opr.typ:=OPR_SYMBOL;
  1665. oper.opr.symbol:=current_asmdata.RefAsmSymbol(hs,AT_FUNCTION);
  1666. end
  1667. else
  1668. begin
  1669. oper.opr.typ:=OPR_CONSTANT;
  1670. oper.opr.val:=toffset;
  1671. end;
  1672. end;
  1673. OPR_REGISTER :
  1674. Message(asmr_e_invalid_reference_syntax);
  1675. OPR_SYMBOL:
  1676. Message(asmr_e_invalid_symbol_ref);
  1677. else
  1678. internalerror(200309222);
  1679. end;
  1680. expr:='';
  1681. end
  1682. else
  1683. begin
  1684. { See it as a separator }
  1685. Consume(AS_DOT);
  1686. end;
  1687. end;
  1688. case actasmtoken of
  1689. {$ifndef i8086}
  1690. AS_SEG :
  1691. Begin
  1692. Message(asmr_e_seg_not_supported);
  1693. Consume(actasmtoken);
  1694. end;
  1695. {$else not i8086}
  1696. AS_SEG,
  1697. {$endif not i8086}
  1698. AS_OFFSET,
  1699. AS_SIZEOF,
  1700. AS_VMTOFFSET,
  1701. AS_TYPE,
  1702. AS_NOT,
  1703. AS_STRING,
  1704. AS_PLUS,
  1705. AS_MINUS,
  1706. AS_LPAREN,
  1707. AS_INTNUM :
  1708. begin
  1709. case oper.opr.typ of
  1710. OPR_REFERENCE :
  1711. {$ifndef x86_64}
  1712. { this is for the i386 scenario where you have
  1713. <load got into ebx>
  1714. mov eax, [ebx].offset globalvar
  1715. x86-64 uses RIP-based addresses (both for GOT and non-GOT
  1716. relative accesses)
  1717. }
  1718. if (actasmtoken=AS_OFFSET) and
  1719. (cs_create_pic in current_settings.moduleswitches) then
  1720. begin
  1721. Consume(AS_OFFSET);
  1722. oper.opr.ref.refaddr:=addr_pic;
  1723. BuildOperand(oper,false);
  1724. end
  1725. else
  1726. {$endif x86_64}
  1727. begin
  1728. l := BuildRefConstExpression;
  1729. inc(oper.opr.ref.offset,l);
  1730. inc(oper.opr.constoffset,l);
  1731. end;
  1732. OPR_LOCAL :
  1733. begin
  1734. l := BuildConstExpression;
  1735. inc(oper.opr.localsymofs,l);
  1736. inc(oper.opr.localconstoffset,l);
  1737. end;
  1738. OPR_NONE,
  1739. OPR_CONSTANT :
  1740. BuildConstantOperand(oper);
  1741. else
  1742. Message(asmr_e_invalid_operand_type);
  1743. end;
  1744. end;
  1745. AS_PTR :
  1746. begin
  1747. if not oper.hastype then
  1748. begin
  1749. if (oper.opr.typ=OPR_CONSTANT) then
  1750. begin
  1751. oper.typesize:=oper.opr.val;
  1752. { reset constant value of operand }
  1753. oper.opr.typ:=OPR_NONE;
  1754. oper.opr.val:=0;
  1755. end
  1756. else
  1757. Message(asmr_e_syn_operand);
  1758. end;
  1759. Consume(AS_PTR);
  1760. { in delphi mode, allow e.g. call dword ptr eax,
  1761. see also webtbs/tw18225.pp }
  1762. if not(m_delphi in current_settings.modeswitches) then
  1763. oper.InitRef;
  1764. { if the operand subscripts a record, the typesize will be
  1765. rest -> save it here and restore it afterwards }
  1766. l:=oper.typesize;
  1767. BuildOperand(oper,false);
  1768. oper.setsize(l,true);
  1769. end;
  1770. AS_ID : { A constant expression, or a Variable ref. }
  1771. Begin
  1772. { Label or Special symbol reference? }
  1773. if actasmpattern[1] = '@' then
  1774. Begin
  1775. if actasmpattern = '@RESULT' then
  1776. Begin
  1777. oper.SetupResult;
  1778. Consume(AS_ID);
  1779. expr:='result';
  1780. end
  1781. else
  1782. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1783. begin
  1784. {$ifdef i8086}
  1785. Message(asmr_e_CODE_or_DATA_without_SEG);
  1786. {$else i8086}
  1787. Message(asmr_w_CODE_and_DATA_not_supported);
  1788. {$endif i8086}
  1789. Consume(AS_ID);
  1790. end
  1791. else
  1792. { Local Label }
  1793. begin
  1794. CreateLocalLabel(actasmpattern,hl,false);
  1795. Consume(AS_ID);
  1796. AddLabelOperand(hl);
  1797. end;
  1798. end
  1799. else
  1800. { support result for delphi modes }
  1801. if (m_objpas in current_settings.modeswitches) and (actasmpattern='RESULT') then
  1802. begin
  1803. oper.SetUpResult;
  1804. Consume(AS_ID);
  1805. expr:='result';
  1806. end
  1807. { probably a variable or normal expression }
  1808. { or a procedure (such as in CALL ID) }
  1809. else
  1810. Begin
  1811. { is it a constant ? }
  1812. if SearchIConstant(actasmpattern,l) then
  1813. Begin
  1814. case oper.opr.typ of
  1815. OPR_REFERENCE :
  1816. begin
  1817. l := BuildRefConstExpression;
  1818. inc(oper.opr.ref.offset,l);
  1819. inc(oper.opr.constoffset,l);
  1820. end;
  1821. OPR_LOCAL :
  1822. begin
  1823. l := BuildRefConstExpression;
  1824. inc(oper.opr.localsymofs,l);
  1825. inc(oper.opr.localconstoffset,l);
  1826. end;
  1827. OPR_NONE,
  1828. OPR_CONSTANT :
  1829. BuildConstantOperand(oper);
  1830. else
  1831. Message(asmr_e_invalid_operand_type);
  1832. end;
  1833. end
  1834. else
  1835. { Check for pascal label }
  1836. if SearchLabel(actasmpattern,hl,false) then
  1837. begin
  1838. Consume(AS_ID);
  1839. AddLabelOperand(hl);
  1840. end
  1841. else
  1842. { is it a normal variable ? }
  1843. Begin
  1844. expr:=actasmpattern;
  1845. Consume(AS_ID);
  1846. { typecasting? }
  1847. if SearchType(expr,l) then
  1848. begin
  1849. oper.hastype:=true;
  1850. oper.typesize:=l;
  1851. case actasmtoken of
  1852. AS_LPAREN :
  1853. begin
  1854. { Support Type([Reference]) }
  1855. Consume(AS_LPAREN);
  1856. BuildOperand(oper,true);
  1857. { Delphi also supports Type(Register) and
  1858. interprets it the same as Type([Register]). }
  1859. if (oper.opr.typ = OPR_REGISTER) then
  1860. { This also sets base to the register. }
  1861. oper.InitRef;
  1862. Consume(AS_RPAREN);
  1863. end;
  1864. AS_LBRACKET :
  1865. begin
  1866. { Support Var.Type[Index] }
  1867. { Convert @label.Byte[1] to reference }
  1868. if oper.opr.typ=OPR_SYMBOL then
  1869. oper.initref;
  1870. end;
  1871. end;
  1872. end
  1873. else
  1874. begin
  1875. if not oper.SetupVar(expr,false) then
  1876. Begin
  1877. { not a variable, check special variables.. }
  1878. if expr = 'SELF' then
  1879. begin
  1880. oper.SetupSelf;
  1881. expr:='self';
  1882. end
  1883. else
  1884. begin
  1885. Message1(sym_e_unknown_id,expr);
  1886. expr:='';
  1887. end;
  1888. end;
  1889. { indexed access to variable? }
  1890. if actasmtoken=AS_LBRACKET then
  1891. begin
  1892. { ... then the operand size is not known anymore }
  1893. oper.size:=OS_NO;
  1894. BuildReference(oper);
  1895. end;
  1896. end;
  1897. end;
  1898. end;
  1899. end;
  1900. AS_REGISTER : { Register, a variable reference or a constant reference }
  1901. begin
  1902. { save the type of register used. }
  1903. tempreg:=actasmregister;
  1904. Consume(AS_REGISTER);
  1905. if actasmtoken = AS_COLON then
  1906. Begin
  1907. Consume(AS_COLON);
  1908. oper.InitRef;
  1909. oper.opr.ref.segment:=tempreg;
  1910. BuildReference(oper);
  1911. end
  1912. else
  1913. { Simple register }
  1914. begin
  1915. if (oper.opr.typ <> OPR_NONE) then
  1916. Message(asmr_e_syn_operand);
  1917. oper.opr.typ:=OPR_REGISTER;
  1918. oper.opr.reg:=tempreg;
  1919. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  1920. end;
  1921. end;
  1922. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1923. Begin
  1924. BuildReference(oper);
  1925. end;
  1926. AS_DWORD,
  1927. AS_BYTE,
  1928. AS_WORD,
  1929. AS_TBYTE,
  1930. AS_DQWORD,
  1931. AS_QWORD,
  1932. AS_OWORD,
  1933. AS_XMMWORD,
  1934. AS_YWORD,
  1935. AS_YMMWORD:
  1936. begin
  1937. { Type specifier }
  1938. oper.hastype:=true;
  1939. oper.typesize:=0;
  1940. case actasmtoken of
  1941. AS_DWORD : oper.typesize:=4;
  1942. AS_WORD : oper.typesize:=2;
  1943. AS_BYTE : oper.typesize:=1;
  1944. AS_QWORD : oper.typesize:=8;
  1945. AS_DQWORD : oper.typesize:=16;
  1946. AS_TBYTE : oper.typesize:=10;
  1947. AS_OWORD,
  1948. AS_XMMWORD: oper.typesize:=16;
  1949. AS_YWORD,
  1950. AS_YMMWORD: oper.typesize:=32;
  1951. else
  1952. internalerror(2010061101);
  1953. end;
  1954. Consume(actasmtoken);
  1955. if (actasmtoken=AS_LPAREN) then
  1956. begin
  1957. { Support "xxx ptr [Reference]" }
  1958. { in case the expression subscripts a record, the typesize
  1959. is reset, so save the explicit size we set above }
  1960. l:=oper.typesize;
  1961. Consume(AS_LPAREN);
  1962. BuildOperand(oper,true);
  1963. Consume(AS_RPAREN);
  1964. oper.setsize(l,true);
  1965. end;
  1966. end;
  1967. AS_SEPARATOR,
  1968. AS_END,
  1969. AS_COMMA,
  1970. AS_COLON:
  1971. begin
  1972. break;
  1973. end;
  1974. AS_RPAREN:
  1975. begin
  1976. if not istypecast then
  1977. begin
  1978. Message(asmr_e_syn_operand);
  1979. Consume(AS_RPAREN);
  1980. end
  1981. else
  1982. break;
  1983. end;
  1984. else
  1985. begin
  1986. Message(asmr_e_syn_operand);
  1987. RecoverConsume(true);
  1988. break;
  1989. end;
  1990. end;
  1991. until false;
  1992. { End of operand, update size if a typecast is forced }
  1993. if (oper.typesize<>0) and
  1994. (oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL]) then
  1995. oper.SetSize(oper.typesize,true);
  1996. {$ifdef i8086}
  1997. { references to a procedure/function entry, without an explicit segment
  1998. override, are added an CS: override by default (this is Turbo Pascal 7
  1999. compatible) }
  2000. if (oper.opr.typ=OPR_REFERENCE) and assigned(oper.opr.ref.symbol) and
  2001. (oper.opr.ref.symbol.typ=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  2002. oper.opr.ref.segment:=NR_CS;
  2003. {$endif i8086}
  2004. end;
  2005. Procedure tx86intreader.BuildOpCode(instr : tx86instruction);
  2006. var
  2007. PrefixOp,OverrideOp: tasmop;
  2008. operandnum : longint;
  2009. t: TRegister;
  2010. is_far_const:boolean;
  2011. i:byte;
  2012. tmp: toperand;
  2013. di_param, si_param: ShortInt;
  2014. prefix_or_override_pending_concat: boolean = false;
  2015. {$ifdef i8086}
  2016. hsymbol: TAsmSymbol;
  2017. hoffset: ASizeInt;
  2018. href_farproc_entry: Boolean;
  2019. {$endif i8086}
  2020. begin
  2021. PrefixOp:=A_None;
  2022. OverrideOp:=A_None;
  2023. is_far_const:=false;
  2024. { prefix seg opcode / prefix opcode }
  2025. repeat
  2026. if is_prefix(actopcode) then
  2027. with instr do
  2028. begin
  2029. if prefix_or_override_pending_concat then
  2030. ConcatInstruction(curlist);
  2031. PrefixOp:=ActOpcode;
  2032. opcode:=ActOpcode;
  2033. condition:=ActCondition;
  2034. opsize:=ActOpsize;
  2035. prefix_or_override_pending_concat:=true;
  2036. consume(AS_OPCODE);
  2037. end
  2038. else
  2039. if is_override(actopcode) then
  2040. with instr do
  2041. begin
  2042. if prefix_or_override_pending_concat then
  2043. ConcatInstruction(curlist);
  2044. OverrideOp:=ActOpcode;
  2045. opcode:=ActOpcode;
  2046. condition:=ActCondition;
  2047. opsize:=ActOpsize;
  2048. prefix_or_override_pending_concat:=true;
  2049. consume(AS_OPCODE);
  2050. end
  2051. else
  2052. break;
  2053. { allow for newline after prefix or override }
  2054. while actasmtoken=AS_SEPARATOR do
  2055. consume(AS_SEPARATOR);
  2056. until (actasmtoken<>AS_OPCODE);
  2057. { opcode }
  2058. if (actasmtoken <> AS_OPCODE) then
  2059. begin
  2060. { allow a prefix or override to be used standalone, like an opcode
  2061. with zero operands; this is TP7 compatible and allows compiling
  2062. ugly code like 'seges; db $67,$66; lodsw' }
  2063. if prefix_or_override_pending_concat then
  2064. exit
  2065. else
  2066. begin
  2067. Message(asmr_e_invalid_or_missing_opcode);
  2068. RecoverConsume(false);
  2069. exit;
  2070. end;
  2071. end;
  2072. if prefix_or_override_pending_concat then
  2073. begin
  2074. instr.ConcatInstruction(curlist);
  2075. prefix_or_override_pending_concat:=false;
  2076. end;
  2077. { Fill the instr object with the current state }
  2078. with instr do
  2079. begin
  2080. Opcode:=ActOpcode;
  2081. condition:=ActCondition;
  2082. opsize:=ActOpsize;
  2083. { Valid combination of prefix/override and instruction ? }
  2084. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  2085. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  2086. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  2087. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  2088. end;
  2089. { pushf/popf/pusha/popa have to default to 16 bit in Intel mode
  2090. (Intel manual and Delphi-compatbile) -- setting the opsize for
  2091. these instructions doesn't change anything in the internal assember,
  2092. so change the opcode }
  2093. if (instr.opcode=A_POPF) then
  2094. instr.opcode:=A_POPFW
  2095. else if (instr.opcode=A_PUSHF) then
  2096. instr.opcode:=A_PUSHFW
  2097. {$ifndef x86_64}
  2098. else if (instr.opcode=A_PUSHA) then
  2099. instr.opcode:=A_PUSHAW
  2100. else if (instr.opcode=A_POPA) then
  2101. instr.opcode:=A_POPAW
  2102. {$endif x86_64}
  2103. {$ifdef i8086}
  2104. { ret is converted to retn or retf, depending on the call model of the
  2105. current procedure (BP7 compatible) }
  2106. else if (instr.opcode=A_RET) then
  2107. begin
  2108. if is_proc_far(current_procinfo.procdef) and
  2109. not (po_interrupt in current_procinfo.procdef.procoptions) then
  2110. instr.opcode:=A_RETF
  2111. else
  2112. instr.opcode:=A_RETN;
  2113. end
  2114. {$endif i8086}
  2115. ;
  2116. { We are reading operands, so opcode will be an AS_ID }
  2117. { process operands backwards to get them in AT&T order }
  2118. operandnum:=max_operands;
  2119. is_far_const:=false;
  2120. Consume(AS_OPCODE);
  2121. { Zero operand opcode ? }
  2122. if actasmtoken in [AS_SEPARATOR,AS_END] then
  2123. exit;
  2124. { Read Operands }
  2125. repeat
  2126. case actasmtoken of
  2127. { End of asm operands for this opcode }
  2128. AS_END,
  2129. AS_SEPARATOR :
  2130. break;
  2131. { Operand delimiter }
  2132. AS_COMMA :
  2133. begin
  2134. { should have something before the comma }
  2135. if instr.operands[operandnum].opr.typ=OPR_NONE then
  2136. Message(asmr_e_syntax_error);
  2137. if operandnum <= 1 then
  2138. Message(asmr_e_too_many_operands)
  2139. else
  2140. Dec(operandnum);
  2141. Consume(AS_COMMA);
  2142. end;
  2143. {Far constant, i.e. jmp $0000:$11111111.}
  2144. AS_COLON:
  2145. begin
  2146. is_far_const:=true;
  2147. if operandnum<max_operands then
  2148. message(asmr_e_too_many_operands)
  2149. else
  2150. dec(operandnum);
  2151. consume(AS_COLON);
  2152. end;
  2153. { Type specifier }
  2154. AS_NEAR,
  2155. AS_FAR :
  2156. begin
  2157. if actasmtoken = AS_NEAR then
  2158. begin
  2159. {$ifndef i8086}
  2160. Message(asmr_w_near_ignored);
  2161. {$endif not i8086}
  2162. instr.opsize:=S_NEAR;
  2163. end
  2164. else
  2165. begin
  2166. {$ifndef i8086}
  2167. Message(asmr_w_far_ignored);
  2168. {$endif not i8086}
  2169. instr.opsize:=S_FAR;
  2170. end;
  2171. Consume(actasmtoken);
  2172. if actasmtoken=AS_PTR then
  2173. begin
  2174. Consume(AS_PTR);
  2175. instr.Operands[operandnum].InitRef;
  2176. end;
  2177. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2178. end;
  2179. else
  2180. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2181. end; { end case }
  2182. until false;
  2183. { shift operands to start from 1, exchange to make sure they are destroyed correctly }
  2184. for i:=operandnum to max_operands do
  2185. begin
  2186. tmp:=instr.operands[i+1-operandnum];
  2187. instr.operands[i+1-operandnum]:=instr.operands[i];
  2188. instr.operands[i]:=tmp;
  2189. end;
  2190. operandnum:=(max_operands+1)-operandnum;
  2191. instr.ops:=operandnum;
  2192. { Check operands }
  2193. for i:=1 to operandnum do
  2194. begin
  2195. if is_far_const and
  2196. (instr.operands[i].opr.typ<>OPR_CONSTANT) then
  2197. message(asmr_e_expr_illegal)
  2198. else
  2199. if instr.operands[i].opr.typ=OPR_NONE then
  2200. Message(asmr_e_syntax_error);
  2201. end;
  2202. { Check for invalid ES: overrides }
  2203. if is_x86_parameterized_string_op(instr.opcode) then
  2204. begin
  2205. si_param:=get_x86_string_op_si_param(instr.opcode);
  2206. if si_param<>-1 then
  2207. si_param:=x86_parameterized_string_op_param_count(instr.opcode)-si_param;
  2208. di_param:=get_x86_string_op_di_param(instr.opcode);
  2209. if di_param<>-1 then
  2210. begin
  2211. di_param:=x86_parameterized_string_op_param_count(instr.opcode)-di_param;
  2212. if di_param<=operandnum then
  2213. with instr.operands[di_param] do
  2214. if (opr.typ=OPR_REFERENCE) and
  2215. (opr.ref.segment<>NR_NO) and
  2216. (opr.ref.segment<>NR_ES) then
  2217. Message(asmr_e_cannot_override_es_segment);
  2218. end;
  2219. { if two memory parameters, check whether their address sizes are equal }
  2220. if (si_param<>-1) and (di_param<>-1) and
  2221. (si_param<=operandnum) and (di_param<=operandnum) and
  2222. (instr.operands[si_param].opr.typ=OPR_REFERENCE) and
  2223. (instr.operands[di_param].opr.typ=OPR_REFERENCE) then
  2224. begin
  2225. if get_ref_address_size(instr.operands[si_param].opr.ref)<>
  2226. get_ref_address_size(instr.operands[di_param].opr.ref) then
  2227. Message(asmr_e_address_sizes_do_not_match);
  2228. end;
  2229. end;
  2230. { e.g. for "push dword 1", "push word 6" }
  2231. if (instr.ops=1) and
  2232. (instr.operands[1].typesize<>0) then
  2233. instr.operands[1].setsize(instr.operands[1].typesize,false);
  2234. {$ifdef i8086}
  2235. for i:=1 to operandnum do
  2236. with instr.operands[i].opr do
  2237. begin
  2238. { convert 'call/jmp [proc/label]' to 'call/jmp proc/label'. Ugly,
  2239. but Turbo Pascal 7 compatible. }
  2240. if (instr.opcode in [A_CALL,A_JMP]) and
  2241. (instr.operands[i].haslabelref or instr.operands[i].hasproc)
  2242. and (typ=OPR_REFERENCE) and
  2243. assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL,AT_ADDR]) and
  2244. (ref.base=NR_NO) and (ref.index=NR_NO) then
  2245. begin
  2246. hsymbol:=ref.symbol;
  2247. hoffset:=ref.offset;
  2248. href_farproc_entry:=ref_farproc_entry;
  2249. typ:=OPR_SYMBOL;
  2250. symbol:=hsymbol;
  2251. symofs:=hoffset;
  2252. symseg:=False;
  2253. sym_farproc_entry:=href_farproc_entry;
  2254. end;
  2255. { convert 'call/jmp symbol' to 'call/jmp far symbol' for symbols that are an entry point of a far procedure }
  2256. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2257. (typ=OPR_SYMBOL) and sym_farproc_entry then
  2258. instr.opsize:=S_FAR;
  2259. { convert 'call/jmp dword [something]' to 'call/jmp far [something]' (BP7 compatibility) }
  2260. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2261. (typ in [OPR_LOCAL,OPR_REFERENCE]) and (instr.operands[i].size=OS_32) then
  2262. instr.opsize:=S_FAR;
  2263. end;
  2264. {$endif i8086}
  2265. if (MemRefInfo(instr.opcode).ExistsSSEAVX) and
  2266. (MemRefInfo(instr.opcode).MemRefSize in MemRefSizeInfoVMems) then
  2267. begin
  2268. for i:=1 to operandnum do
  2269. begin
  2270. if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2271. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2272. (instr.operands[i].opr.ref.index = NR_NO) then
  2273. begin
  2274. instr.operands[i].opr.ref.index := instr.operands[i].opr.ref.base;
  2275. instr.operands[i].opr.ref.base := NR_NO;
  2276. end
  2277. else if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2278. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2279. (getregtype(instr.operands[i].opr.ref.index) = R_INTREGISTER) and
  2280. (getsubreg(instr.operands[i].opr.ref.index) = R_SUBADDR) then
  2281. begin
  2282. // exchange base- and index-register
  2283. // e.g. VGATHERDPD XMM0, [XMM1 + RAX], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1], XMM2
  2284. // e.g. VGATHERDPD XMM0, [XMM1 + RAX * 2], XMM2 =>> not supported
  2285. // e.g. VGATHERDPD XMM0, [XMM1 + RAX + 16], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1 + 16]
  2286. if instr.operands[i].opr.ref.scalefactor > 1 then Message(asmr_e_invalid_reference_syntax)
  2287. else
  2288. begin
  2289. t := instr.operands[i].opr.ref.base;
  2290. instr.operands[i].opr.ref.base := instr.operands[i].opr.ref.index;
  2291. instr.operands[i].opr.ref.index := t;
  2292. end;
  2293. end;
  2294. end;
  2295. end;
  2296. end;
  2297. Procedure tx86intreader.BuildConstant(constsize: byte);
  2298. var
  2299. asmsymtyp : tasmsymtype;
  2300. asmsym,
  2301. expr: string;
  2302. value : tcgint;
  2303. isseg,is_farproc_entry,hasofs : boolean;
  2304. Begin
  2305. Repeat
  2306. Case actasmtoken of
  2307. AS_STRING:
  2308. Begin
  2309. { DD and DW cases }
  2310. if constsize <> 1 then
  2311. Begin
  2312. if Not PadZero(actasmpattern,constsize) then
  2313. Message(scan_f_string_exceeds_line);
  2314. end;
  2315. expr:=actasmpattern;
  2316. Consume(AS_STRING);
  2317. Case actasmtoken of
  2318. AS_COMMA:
  2319. Consume(AS_COMMA);
  2320. AS_END,
  2321. AS_SEPARATOR: ;
  2322. else
  2323. Message(asmr_e_invalid_string_expression);
  2324. end;
  2325. ConcatString(curlist,expr);
  2326. end;
  2327. AS_PLUS,
  2328. AS_MINUS,
  2329. AS_LPAREN,
  2330. AS_NOT,
  2331. AS_INTNUM,
  2332. AS_OFFSET,
  2333. {$ifdef i8086}
  2334. AS_SEG,
  2335. {$endif i8086}
  2336. AS_ID :
  2337. Begin
  2338. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp,isseg,is_farproc_entry,hasofs);
  2339. if asmsym<>'' then
  2340. begin
  2341. if (not isseg) and
  2342. {$ifdef i8086}
  2343. ((constsize<>2) and (constsize<>4))
  2344. {$else i8086}
  2345. (constsize<>sizeof(pint))
  2346. {$endif i8086}
  2347. then
  2348. begin
  2349. Message1(asmr_w_const32bit_for_address,asmsym);
  2350. constsize:=sizeof(pint);
  2351. end;
  2352. {$ifdef i8086}
  2353. if asmsym='@DATA' then
  2354. begin
  2355. if not isseg then
  2356. Message(asmr_e_CODE_or_DATA_without_SEG);
  2357. if constsize<2 then
  2358. Message1(asmr_e_const16bit_for_segment,asmsym);
  2359. if current_settings.x86memorymodel=mm_huge then
  2360. curlist.concat(Tai_const.Create_fardataseg)
  2361. else
  2362. curlist.concat(Tai_const.Create_dgroup);
  2363. if constsize>2 then
  2364. ConcatConstant(curlist,0,constsize-2);
  2365. end
  2366. else if asmsym='@CODE' then
  2367. begin
  2368. if not isseg then
  2369. Message(asmr_e_CODE_or_DATA_without_SEG);
  2370. if constsize<2 then
  2371. Message1(asmr_e_const16bit_for_segment,asmsym);
  2372. curlist.concat(Tai_const.Create_seg_name(current_procinfo.procdef.mangledname));
  2373. if constsize>2 then
  2374. ConcatConstant(curlist,0,constsize-2);
  2375. end
  2376. else if isseg then
  2377. begin
  2378. if constsize<2 then
  2379. Message1(asmr_e_const16bit_for_segment,asmsym);
  2380. curlist.concat(Tai_const.Create_seg_name(asmsym));
  2381. if constsize>2 then
  2382. ConcatConstant(curlist,0,constsize-2);
  2383. end
  2384. else
  2385. {$endif i8086}
  2386. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value,constsize,hasofs);
  2387. end
  2388. else
  2389. ConcatConstant(curlist,value,constsize);
  2390. end;
  2391. AS_COMMA:
  2392. begin
  2393. Consume(AS_COMMA);
  2394. end;
  2395. AS_ALIGN,
  2396. AS_DB,
  2397. AS_DW,
  2398. AS_DD,
  2399. AS_DQ,
  2400. AS_OPCODE,
  2401. AS_END,
  2402. AS_SEPARATOR:
  2403. break;
  2404. else
  2405. begin
  2406. Message(asmr_e_syn_constant);
  2407. RecoverConsume(false);
  2408. end
  2409. end;
  2410. Until false;
  2411. end;
  2412. function tx86intreader.Assemble: tlinkedlist;
  2413. Var
  2414. hl : tasmlabel;
  2415. instr : Tx86Instruction;
  2416. tmpsym: tsym;
  2417. tmpsrsymtable: TSymtable;
  2418. Begin
  2419. Message1(asmr_d_start_reading,'intel');
  2420. inexpression:=FALSE;
  2421. firsttoken:=TRUE;
  2422. { sets up all opcode and register tables in uppercase
  2423. done in the construtor now
  2424. if not _asmsorted then
  2425. Begin
  2426. SetupTables;
  2427. _asmsorted:=TRUE;
  2428. end;
  2429. }
  2430. curlist:=TAsmList.Create;
  2431. { we might need to know which parameters are passed in registers }
  2432. if not parse_generic then
  2433. current_procinfo.generate_parameter_info;
  2434. { start tokenizer }
  2435. gettoken;
  2436. { main loop }
  2437. repeat
  2438. case actasmtoken of
  2439. AS_LLABEL:
  2440. Begin
  2441. if CreateLocalLabel(actasmpattern,hl,true) then
  2442. ConcatLabel(curlist,hl);
  2443. Consume(AS_LLABEL);
  2444. end;
  2445. AS_LABEL:
  2446. Begin
  2447. if SearchLabel(upper(actasmpattern),hl,true) then
  2448. begin
  2449. if hl.is_public then
  2450. ConcatPublic(curlist,actasmpattern_origcase);
  2451. ConcatLabel(curlist,hl);
  2452. end
  2453. else
  2454. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2455. Consume(AS_LABEL);
  2456. end;
  2457. AS_DW :
  2458. Begin
  2459. inexpression:=true;
  2460. Consume(AS_DW);
  2461. BuildConstant(2);
  2462. inexpression:=false;
  2463. end;
  2464. AS_DB :
  2465. Begin
  2466. inexpression:=true;
  2467. Consume(AS_DB);
  2468. BuildConstant(1);
  2469. inexpression:=false;
  2470. end;
  2471. AS_DD :
  2472. Begin
  2473. inexpression:=true;
  2474. Consume(AS_DD);
  2475. BuildConstant(4);
  2476. inexpression:=false;
  2477. end;
  2478. {$ifdef cpu64bitaddr}
  2479. AS_DQ:
  2480. Begin
  2481. inexpression:=true;
  2482. Consume(AS_DQ);
  2483. BuildConstant(8);
  2484. inexpression:=false;
  2485. end;
  2486. {$endif cpu64bitaddr}
  2487. AS_PUBLIC:
  2488. Begin
  2489. Consume(AS_PUBLIC);
  2490. repeat
  2491. if actasmtoken=AS_ID then
  2492. begin
  2493. if (actasmpattern<>'') and (actasmpattern[1]='@') then
  2494. Message1(asmr_e_local_label_cannot_be_declared_public,actasmpattern)
  2495. else if SearchLabel(upper(actasmpattern),hl,false) then
  2496. begin
  2497. if not hl.is_public then
  2498. begin
  2499. hl.is_public:=true;
  2500. asmsearchsym(upper(actasmpattern),tmpsym,tmpsrsymtable);
  2501. if tlabelsym(tmpsym).defined then
  2502. Message1(asmr_e_public_must_be_used_before_label_definition,actasmpattern);
  2503. end;
  2504. end
  2505. else
  2506. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2507. end;
  2508. Consume(AS_ID);
  2509. if actasmtoken=AS_COMMA then
  2510. begin
  2511. Consume(AS_COMMA);
  2512. if actasmtoken<>AS_ID then
  2513. Consume(AS_ID);
  2514. end;
  2515. until actasmtoken=AS_SEPARATOR;
  2516. end;
  2517. AS_ALIGN:
  2518. Begin
  2519. Consume(AS_ALIGN);
  2520. ConcatAlign(curlist,BuildConstExpression);
  2521. if actasmtoken<>AS_SEPARATOR then
  2522. Consume(AS_SEPARATOR);
  2523. end;
  2524. AS_OPCODE :
  2525. Begin
  2526. instr:=Tx86Instruction.Create(Tx86Operand);
  2527. BuildOpcode(instr);
  2528. with instr do
  2529. begin
  2530. CheckNonCommutativeOpcodes;
  2531. AddReferenceSizes;
  2532. SetInstructionOpsize;
  2533. CheckOperandSizes;
  2534. ConcatInstruction(curlist);
  2535. end;
  2536. instr.Free;
  2537. end;
  2538. AS_SEPARATOR :
  2539. Begin
  2540. Consume(AS_SEPARATOR);
  2541. end;
  2542. AS_END :
  2543. break; { end assembly block }
  2544. else
  2545. Begin
  2546. Message(asmr_e_syntax_error);
  2547. RecoverConsume(false);
  2548. end;
  2549. end; { end case }
  2550. until false;
  2551. { check that all referenced local labels are defined }
  2552. checklocallabels;
  2553. { Return the list in an asmnode }
  2554. assemble:=curlist;
  2555. Message1(asmr_d_finish_reading,'intel');
  2556. end;
  2557. end.