rax86int.pas 104 KB

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