rax86int.pas 96 KB

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