rax86int.pas 109 KB

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