rax86int.pas 120 KB

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