rautils.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. This unit implements some support routines for assembler parsing
  4. independent of the processor
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. **********************************************************************}
  17. Unit RAUtils;
  18. {$i fpcdefs.inc}
  19. Interface
  20. Uses
  21. cutils,cclasses,
  22. globtype,aasmbase,aasmtai,aasmdata,cpubase,cpuinfo,cgbase,cgutils,
  23. symconst,symbase,symtype,symdef,symsym;
  24. Const
  25. RPNMax = 10; { I think you only need 4, but just to be safe }
  26. OpMax = 25;
  27. {$if max_operands = 2}
  28. {$define MAX_OPER_2}
  29. {$endif}
  30. {$if max_operands = 3}
  31. {$define MAX_OPER_3}
  32. {$endif}
  33. {---------------------------------------------------------------------
  34. Local Label Management
  35. ---------------------------------------------------------------------}
  36. Type
  37. { Each local label has this structure associated with it }
  38. TLocalLabel = class(TFPHashObject)
  39. Emitted : boolean;
  40. constructor Create(AList:TFPHashObjectList;const n:string);
  41. function Gettasmlabel:tasmlabel;
  42. private
  43. lab : tasmlabel;
  44. end;
  45. TLocalLabelList = class(TFPHashObjectList)
  46. procedure CheckEmitted;
  47. end;
  48. var
  49. LocalLabelList : TLocalLabelList;
  50. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  51. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  52. {---------------------------------------------------------------------
  53. Instruction management
  54. ---------------------------------------------------------------------}
  55. type
  56. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_LOCAL,
  57. OPR_REFERENCE,OPR_REGISTER,OPR_COND,OPR_REGSET,OPR_SHIFTEROP,OPR_MODEFLAGS,OPR_SPECIALREG);
  58. TOprRec = record
  59. case typ:TOprType of
  60. OPR_NONE : ();
  61. OPR_CONSTANT : (val:aint);
  62. OPR_SYMBOL : (symbol:tasmsymbol;symofs:aint);
  63. OPR_REFERENCE : (varsize:asizeint; constoffset: asizeint; ref:treference);
  64. OPR_LOCAL : (localvarsize, localconstoffset: asizeint;localsym:tabstractnormalvarsym;localsymofs:aint;localindexreg:tregister;localscale:byte;localgetoffset,localforceref:boolean);
  65. OPR_REGISTER : (reg:tregister);
  66. {$ifdef m68k}
  67. OPR_REGSET : (regsetdata,regsetaddr : tcpuregisterset);
  68. {$endif m68k}
  69. {$ifdef powerpc}
  70. OPR_COND : (cond : tasmcond);
  71. {$endif powerpc}
  72. {$ifdef POWERPC64}
  73. OPR_COND : (cond : tasmcond);
  74. {$endif POWERPC64}
  75. {$ifdef arm}
  76. OPR_REGSET : (regset : tcpuregisterset; regtype: tregistertype; subreg: tsubregister; usermode: boolean);
  77. OPR_SHIFTEROP : (shifterop : tshifterop);
  78. OPR_COND : (cc : tasmcond);
  79. OPR_MODEFLAGS : (flags : tcpumodeflags);
  80. OPR_SPECIALREG: (specialreg : tregister; specialregflags : tspecialregflags);
  81. {$endif arm}
  82. end;
  83. TOperand = class
  84. opr : TOprRec;
  85. typesize : byte;
  86. hastype, { if the operand has typecasted variable }
  87. hasvar : boolean; { if the operand is loaded with a variable }
  88. size : TCGSize;
  89. constructor create;virtual;
  90. destructor destroy;override;
  91. Procedure SetSize(_size:longint;force:boolean);virtual;
  92. Procedure SetCorrectSize(opcode:tasmop);virtual;
  93. Function SetupResult:boolean;virtual;
  94. Function SetupSelf:boolean;
  95. Function SetupOldEBP:boolean;
  96. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  97. Function CheckOperand: boolean; virtual;
  98. Procedure InitRef;
  99. end;
  100. TCOperand = class of TOperand;
  101. TInstruction = class
  102. operands : array[1..max_operands] of toperand;
  103. opcode : tasmop;
  104. condition : tasmcond;
  105. ops : byte;
  106. labeled : boolean;
  107. filepos : tfileposinfo;
  108. constructor create(optype : tcoperand);virtual;
  109. destructor destroy;override;
  110. { converts the instruction to an instruction how it's used by the assembler writer
  111. and concats it to the passed list. The newly created item is returned if the
  112. instruction was valid, otherwise nil is returned }
  113. function ConcatInstruction(p:TAsmList) : tai;virtual;
  114. Procedure Swapoperands;
  115. end;
  116. {---------------------------------------------------------------------}
  117. { Expression parser types }
  118. {---------------------------------------------------------------------}
  119. TExprOperator = record
  120. ch: char; { operator }
  121. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  122. end;
  123. {**********************************************************************}
  124. { The following operators are supported: }
  125. { '+' : addition }
  126. { '-' : subtraction }
  127. { '*' : multiplication }
  128. { '/' : modulo division }
  129. { '^' : exclusive or }
  130. { '<' : shift left }
  131. { '>' : shift right }
  132. { '&' : bitwise and }
  133. { '|' : bitwise or }
  134. { '~' : bitwise complement }
  135. { '%' : modulo division }
  136. { nnn: longint numbers }
  137. { ( and ) parenthesis }
  138. {**********************************************************************}
  139. TExprParse = class
  140. public
  141. Constructor create;
  142. Destructor Destroy;override;
  143. Function Evaluate(Expr: String): aint;
  144. Function Priority(_Operator: Char): aint;
  145. private
  146. RPNStack : Array[1..RPNMax] of aint; { Stack For RPN calculator }
  147. RPNTop : aint;
  148. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  149. OpTop : aint;
  150. Procedure RPNPush(Num: aint);
  151. Function RPNPop: aint;
  152. Procedure RPNCalc(const token: String; prefix: boolean);
  153. Procedure OpPush(_Operator: char; prefix: boolean);
  154. { In reality returns TExprOperaotr }
  155. Procedure OpPop(var _Operator:TExprOperator);
  156. end;
  157. { Evaluate an expression string to a aint }
  158. Function CalculateExpression(const expression: string): aint;
  159. {---------------------------------------------------------------------}
  160. { String routines }
  161. {---------------------------------------------------------------------}
  162. Function ParseVal(const S:String;base:byte):aint;
  163. Function PadZero(Var s: String; n: byte): Boolean;
  164. Function EscapeToPascal(const s:string): string;
  165. {---------------------------------------------------------------------
  166. Symbol helper routines
  167. ---------------------------------------------------------------------}
  168. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:TSymtable);
  169. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint; var mangledname: string; needvmtofs: boolean):boolean;
  170. Function SearchType(const hs:string;var size:aint): Boolean;
  171. Function SearchRecordType(const s:string): boolean;
  172. Function SearchIConstant(const s:string; var l:aint): boolean;
  173. {---------------------------------------------------------------------
  174. Instruction generation routines
  175. ---------------------------------------------------------------------}
  176. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  177. Procedure ConcatConstant(p : TAsmList;value: aint; constsize:byte);
  178. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:aint);
  179. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  180. Procedure ConcatString(p : TAsmList;s:string);
  181. procedure ConcatAlign(p:TAsmList;l:aint);
  182. Procedure ConcatPublic(p:TAsmList;const s : string);
  183. Procedure ConcatLocal(p:TAsmList;const s : string);
  184. Implementation
  185. uses
  186. SysUtils,
  187. defutil,systems,verbose,globals,
  188. symtable,paramgr,
  189. aasmcpu,
  190. procinfo;
  191. {*************************************************************************
  192. TExprParse
  193. *************************************************************************}
  194. Constructor TExprParse.create;
  195. Begin
  196. end;
  197. Procedure TExprParse.RPNPush(Num : aint);
  198. { Add an operand to the top of the RPN stack }
  199. begin
  200. if RPNTop < RPNMax then
  201. begin
  202. Inc(RPNTop);
  203. RPNStack[RPNTop]:=Num;
  204. end
  205. else
  206. Message(asmr_e_expr_illegal);
  207. end;
  208. Function TExprParse.RPNPop : aint;
  209. { Get the operand at the top of the RPN stack }
  210. begin
  211. if RPNTop > 0 then
  212. begin
  213. RPNPop:=RPNStack[RPNTop];
  214. Dec(RPNTop);
  215. end
  216. else
  217. Message(asmr_e_expr_illegal);
  218. end;
  219. Procedure TExprParse.RPNCalc(const Token : String; prefix:boolean); { RPN Calculator }
  220. Var
  221. Temp : aint;
  222. n1,n2 : aint;
  223. LocalError : Integer;
  224. begin
  225. { Handle operators }
  226. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  227. Case Token[1] of
  228. '+' :
  229. Begin
  230. if not prefix then
  231. RPNPush(RPNPop + RPNPop);
  232. end;
  233. '-' :
  234. Begin
  235. if prefix then
  236. RPNPush(-(RPNPop))
  237. else
  238. begin
  239. n1:=RPNPop;
  240. n2:=RPNPop;
  241. RPNPush(n2 - n1);
  242. end;
  243. end;
  244. '*' : RPNPush(RPNPop * RPNPop);
  245. '&' :
  246. begin
  247. n1:=RPNPop;
  248. n2:=RPNPop;
  249. RPNPush(n2 and n1);
  250. end;
  251. '|' :
  252. begin
  253. n1:=RPNPop;
  254. n2:=RPNPop;
  255. RPNPush(n2 or n1);
  256. end;
  257. '~' : RPNPush(NOT RPNPop);
  258. '<' :
  259. begin
  260. n1:=RPNPop;
  261. n2:=RPNPop;
  262. RPNPush(n2 SHL n1);
  263. end;
  264. '>' :
  265. begin
  266. n1:=RPNPop;
  267. n2:=RPNPop;
  268. RPNPush(n2 SHR n1);
  269. end;
  270. '%' :
  271. begin
  272. Temp:=RPNPop;
  273. if Temp <> 0 then
  274. RPNPush(RPNPop mod Temp)
  275. else
  276. begin
  277. Message(asmr_e_expr_zero_divide);
  278. { push 1 for error recovery }
  279. RPNPush(1);
  280. end;
  281. end;
  282. '^' : RPNPush(RPNPop XOR RPNPop);
  283. '/' :
  284. begin
  285. Temp:=RPNPop;
  286. if Temp <> 0 then
  287. RPNPush(RPNPop div Temp)
  288. else
  289. begin
  290. Message(asmr_e_expr_zero_divide);
  291. { push 1 for error recovery }
  292. RPNPush(1);
  293. end;
  294. end;
  295. end
  296. else
  297. begin
  298. { Convert String to number and add to stack }
  299. Val(Token, Temp, LocalError);
  300. if LocalError = 0 then
  301. RPNPush(Temp)
  302. else
  303. begin
  304. Message(asmr_e_expr_illegal);
  305. { push 1 for error recovery }
  306. RPNPush(1);
  307. end;
  308. end;
  309. end;
  310. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  311. { Add an operator onto top of the stack }
  312. begin
  313. if OpTop < OpMax then
  314. begin
  315. Inc(OpTop);
  316. OpStack[OpTop].ch:=_Operator;
  317. OpStack[OpTop].is_prefix:=prefix;
  318. end
  319. else
  320. Message(asmr_e_expr_illegal);
  321. end;
  322. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  323. { Get operator at the top of the stack }
  324. begin
  325. if OpTop > 0 then
  326. begin
  327. _Operator:=OpStack[OpTop];
  328. Dec(OpTop);
  329. end
  330. else
  331. Message(asmr_e_expr_illegal);
  332. end;
  333. Function TExprParse.Priority(_Operator : Char) : aint;
  334. { Return priority of operator }
  335. { The greater the priority, the higher the precedence }
  336. begin
  337. Case _Operator OF
  338. '(' :
  339. Priority:=0;
  340. '|','^','~' : // the lowest priority: OR, XOR, NOT
  341. Priority:=0;
  342. '&' : // bigger priority: AND
  343. Priority:=1;
  344. '+', '-' : // bigger priority: +, -
  345. Priority:=2;
  346. '*', '/','%','<','>' : // the highest priority: *, /, MOD, SHL, SHR
  347. Priority:=3;
  348. else
  349. Message(asmr_e_expr_illegal);
  350. end;
  351. end;
  352. Function TExprParse.Evaluate(Expr : String):aint;
  353. Var
  354. I : longint;
  355. Token : String;
  356. opr : TExprOperator;
  357. begin
  358. Evaluate:=0;
  359. { Reset stacks }
  360. OpTop :=0;
  361. RPNTop:=0;
  362. Token :='';
  363. { nothing to do ? }
  364. if Expr='' then
  365. exit;
  366. For I:=1 to Length(Expr) DO
  367. begin
  368. if Expr[I] in ['0'..'9'] then
  369. begin { Build multi-digit numbers }
  370. Token:=Token + Expr[I];
  371. if I = Length(Expr) then { Send last one to calculator }
  372. RPNCalc(Token,false);
  373. end
  374. else
  375. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  376. begin
  377. if Token <> '' then
  378. begin { Send last built number to calc. }
  379. RPNCalc(Token,false);
  380. Token:='';
  381. end;
  382. Case Expr[I] OF
  383. '(' : OpPush('(',false);
  384. ')' : begin
  385. While (OpTop>0) and (OpStack[OpTop].ch <> '(') DO
  386. Begin
  387. OpPop(opr);
  388. RPNCalc(opr.ch,opr.is_prefix);
  389. end;
  390. OpPop(opr); { Pop off and ignore the '(' }
  391. end;
  392. '+','-','~' : Begin
  393. { workaround for -2147483648 }
  394. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  395. begin
  396. token:='-';
  397. expr[i]:='+';
  398. end;
  399. { if start of expression then surely a prefix }
  400. { or if previous char was also an operator }
  401. if (I = 1) or (not (Expr[I-1] in ['0'..'9',')'])) then
  402. OpPush(Expr[I],true)
  403. else
  404. Begin
  405. { Evaluate all higher priority operators }
  406. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  407. Begin
  408. OpPop(opr);
  409. RPNCalc(opr.ch,opr.is_prefix);
  410. end;
  411. OpPush(Expr[I],false);
  412. End;
  413. end;
  414. '*', '/',
  415. '^','|','&',
  416. '%','<','>' : begin
  417. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  418. Begin
  419. OpPop(opr);
  420. RPNCalc(opr.ch,opr.is_prefix);
  421. end;
  422. OpPush(Expr[I],false);
  423. end;
  424. end; { Case }
  425. end
  426. else
  427. Message(asmr_e_expr_illegal); { Handle bad input error }
  428. end;
  429. { Pop off the remaining operators }
  430. While OpTop > 0 do
  431. Begin
  432. OpPop(opr);
  433. RPNCalc(opr.ch,opr.is_prefix);
  434. end;
  435. { The result is stored on the top of the stack }
  436. Evaluate:=RPNPop;
  437. end;
  438. Destructor TExprParse.Destroy;
  439. Begin
  440. end;
  441. Function CalculateExpression(const expression: string): aint;
  442. var
  443. expr: TExprParse;
  444. Begin
  445. expr:=TExprParse.create;
  446. CalculateExpression:=expr.Evaluate(expression);
  447. expr.Free;
  448. end;
  449. {*************************************************************************}
  450. { String conversions/utils }
  451. {*************************************************************************}
  452. Function EscapeToPascal(const s:string): string;
  453. { converts a C styled string - which contains escape }
  454. { characters to a pascal style string. }
  455. var
  456. i,len : aint;
  457. hs : string;
  458. temp : string;
  459. c : char;
  460. Begin
  461. hs:='';
  462. len:=0;
  463. i:=0;
  464. while (i<length(s)) and (len<255) do
  465. begin
  466. Inc(i);
  467. if (s[i]='\') and (i<length(s)) then
  468. Begin
  469. inc(i);
  470. case s[i] of
  471. '\' :
  472. c:='\';
  473. 'b':
  474. c:=#8;
  475. 'f':
  476. c:=#12;
  477. 'n':
  478. c:=#10;
  479. 'r':
  480. c:=#13;
  481. 't':
  482. c:=#9;
  483. '"':
  484. c:='"';
  485. '0'..'7':
  486. Begin
  487. temp:=s[i];
  488. temp:=temp+s[i+1];
  489. temp:=temp+s[i+2];
  490. inc(i,2);
  491. c:=chr(ParseVal(temp,8));
  492. end;
  493. 'x':
  494. Begin
  495. temp:=s[i+1];
  496. temp:=temp+s[i+2];
  497. inc(i,2);
  498. c:=chr(ParseVal(temp,16));
  499. end;
  500. else
  501. Begin
  502. Message1(asmr_e_escape_seq_ignored,s[i]);
  503. c:=s[i];
  504. end;
  505. end;
  506. end
  507. else
  508. c:=s[i];
  509. inc(len);
  510. hs[len]:=c;
  511. end;
  512. hs[0]:=chr(len);
  513. EscapeToPascal:=hs;
  514. end;
  515. Function ParseVal(const S:String;base:byte):aint;
  516. { Converts a decimal string to aint }
  517. var
  518. code : integer;
  519. errmsg : word;
  520. prefix : string[2];
  521. Begin
  522. case base of
  523. 2 :
  524. begin
  525. errmsg:=asmr_e_error_converting_binary;
  526. prefix:='%';
  527. end;
  528. 8 :
  529. begin
  530. errmsg:=asmr_e_error_converting_octal;
  531. prefix:='&';
  532. end;
  533. 10 :
  534. begin
  535. errmsg:=asmr_e_error_converting_decimal;
  536. prefix:='';
  537. end;
  538. 16 :
  539. begin
  540. errmsg:=asmr_e_error_converting_hexadecimal;
  541. prefix:='$';
  542. end;
  543. else
  544. internalerror(200501202);
  545. end;
  546. val(prefix+s,result,code);
  547. if code<>0 then
  548. begin
  549. val(prefix+s,aword(result),code);
  550. if code<>0 then
  551. begin
  552. Message1(errmsg,s);
  553. result:=0;
  554. end;
  555. end;
  556. end;
  557. Function PadZero(Var s: String; n: byte): Boolean;
  558. Begin
  559. PadZero:=TRUE;
  560. { Do some error checking first }
  561. if Length(s) = n then
  562. exit
  563. else
  564. if Length(s) > n then
  565. Begin
  566. PadZero:=FALSE;
  567. delete(s,n+1,length(s));
  568. exit;
  569. end
  570. else
  571. PadZero:=TRUE;
  572. { Fill it up with the specified character }
  573. fillchar(s[length(s)+1],n-1,#0);
  574. s[0]:=chr(n);
  575. end;
  576. {****************************************************************************
  577. TOperand
  578. ****************************************************************************}
  579. constructor TOperand.Create;
  580. begin
  581. size:=OS_NO;
  582. hastype:=false;
  583. hasvar:=false;
  584. FillChar(Opr,sizeof(Opr),0);
  585. end;
  586. destructor TOperand.destroy;
  587. begin
  588. end;
  589. Procedure TOperand.SetSize(_size:longint;force:boolean);
  590. begin
  591. if force or
  592. ((size = OS_NO) and (_size<=16)) then
  593. Begin
  594. case _size of
  595. 1 : size:=OS_8;
  596. 2 : size:=OS_16{ could be S_IS};
  597. 4 : size:=OS_32{ could be S_IL or S_FS};
  598. 8 : size:=OS_64{ could be S_D or S_FL};
  599. 10 : size:=OS_F80;
  600. 16 : size:=OS_128;
  601. end;
  602. end;
  603. end;
  604. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  605. begin
  606. end;
  607. function TOperand.SetupResult:boolean;
  608. begin
  609. SetupResult:=false;
  610. { replace by correct offset. }
  611. with current_procinfo.procdef do
  612. if (not is_void(returndef)) then
  613. begin
  614. if (m_tp7 in current_settings.modeswitches) and
  615. not (df_generic in defoptions) and
  616. (not paramanager.ret_in_param(returndef,current_procinfo.procdef)) then
  617. begin
  618. message(asmr_e_cannot_use_RESULT_here);
  619. exit;
  620. end;
  621. SetupResult:=setupvar('result',false)
  622. end
  623. else
  624. message(asmr_e_void_function);
  625. end;
  626. Function TOperand.SetupSelf:boolean;
  627. Begin
  628. SetupSelf:=false;
  629. if assigned(current_structdef) then
  630. SetupSelf:=setupvar('self',false)
  631. else
  632. Message(asmr_e_cannot_use_SELF_outside_a_method);
  633. end;
  634. Function TOperand.SetupOldEBP:boolean;
  635. Begin
  636. SetupOldEBP:=false;
  637. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  638. SetupOldEBP:=setupvar('parentframe',false)
  639. else
  640. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  641. end;
  642. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  643. function symtable_has_localvarsyms(st:TSymtable):boolean;
  644. var
  645. sym : tsym;
  646. i : longint;
  647. begin
  648. result:=false;
  649. for i:=0 to st.SymList.Count-1 do
  650. begin
  651. sym:=tsym(st.SymList[i]);
  652. if sym.typ=localvarsym then
  653. begin
  654. result:=true;
  655. exit;
  656. end;
  657. end;
  658. end;
  659. procedure setconst(l:aint);
  660. begin
  661. { We return the address of the field, just like Delphi/TP }
  662. case opr.typ of
  663. OPR_NONE :
  664. begin
  665. opr.typ:=OPR_CONSTANT;
  666. opr.val:=l;
  667. end;
  668. OPR_CONSTANT :
  669. inc(opr.val,l);
  670. OPR_REFERENCE :
  671. inc(opr.ref.offset,l);
  672. OPR_LOCAL :
  673. inc(opr.localsymofs,l);
  674. else
  675. Message(asmr_e_invalid_operand_type);
  676. end;
  677. end;
  678. { search and sets up the correct fields in the Instr record }
  679. { for the NON-constant identifier passed to the routine. }
  680. { if not found returns FALSE. }
  681. var
  682. sym : tsym;
  683. srsymtable : TSymtable;
  684. harrdef : tarraydef;
  685. indexreg : tregister;
  686. l : aint;
  687. plist : ppropaccesslistitem;
  688. Begin
  689. SetupVar:=false;
  690. asmsearchsym(s,sym,srsymtable);
  691. if sym = nil then
  692. exit;
  693. if sym.typ=absolutevarsym then
  694. begin
  695. if (tabsolutevarsym(sym).abstyp=tovar) then
  696. begin
  697. { Only support simple loads }
  698. plist:=tabsolutevarsym(sym).ref.firstsym;
  699. if assigned(plist) and
  700. (plist^.sltype=sl_load) then
  701. sym:=plist^.sym
  702. else
  703. begin
  704. Message(asmr_e_unsupported_symbol_type);
  705. exit;
  706. end;
  707. end
  708. else
  709. begin
  710. Message(asmr_e_unsupported_symbol_type);
  711. exit;
  712. end;
  713. end;
  714. case sym.typ of
  715. fieldvarsym :
  716. begin
  717. if not tabstractrecordsymtable(sym.owner).is_packed then
  718. setconst(tfieldvarsym(sym).fieldoffset)
  719. else if tfieldvarsym(sym).fieldoffset mod 8 = 0 then
  720. setconst(tfieldvarsym(sym).fieldoffset div 8)
  721. else
  722. Message(asmr_e_packed_element);
  723. hasvar:=true;
  724. SetupVar:=true;
  725. end;
  726. staticvarsym,
  727. localvarsym,
  728. paravarsym :
  729. begin
  730. { we always assume in asm statements that }
  731. { that the variable is valid. }
  732. tabstractvarsym(sym).varstate:=vs_readwritten;
  733. inc(tabstractvarsym(sym).refs);
  734. { variable can't be placed in a register }
  735. tabstractvarsym(sym).varregable:=vr_none;
  736. { and anything may happen with its address }
  737. tabstractvarsym(sym).addr_taken:=true;
  738. case sym.typ of
  739. staticvarsym :
  740. begin
  741. initref;
  742. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(sym).mangledname);
  743. end;
  744. paravarsym,
  745. localvarsym :
  746. begin
  747. if opr.typ=OPR_REFERENCE then
  748. begin
  749. indexreg:=opr.ref.base;
  750. if opr.ref.index<>NR_NO then
  751. begin
  752. if indexreg=NR_NO then
  753. indexreg:=opr.ref.index
  754. else
  755. Message(asmr_e_multiple_index);
  756. end;
  757. end
  758. else
  759. indexreg:=NR_NO;
  760. opr.typ:=OPR_LOCAL;
  761. if assigned(current_procinfo.parent) and
  762. not(po_inline in current_procinfo.procdef.procoptions) and
  763. (sym.owner<>current_procinfo.procdef.localst) and
  764. (sym.owner<>current_procinfo.procdef.parast) and
  765. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  766. symtable_has_localvarsyms(current_procinfo.procdef.localst) then
  767. message1(asmr_e_local_para_unreachable,s);
  768. opr.localsym:=tabstractnormalvarsym(sym);
  769. opr.localsymofs:=0;
  770. opr.localindexreg:=indexreg;
  771. opr.localscale:=0;
  772. opr.localgetoffset:=GetOffset;
  773. if paramanager.push_addr_param(tabstractvarsym(sym).varspez,tabstractvarsym(sym).vardef,current_procinfo.procdef.proccalloption) then
  774. SetSize(sizeof(pint),false);
  775. end;
  776. end;
  777. case tabstractvarsym(sym).vardef.typ of
  778. orddef,
  779. enumdef,
  780. pointerdef,
  781. floatdef :
  782. SetSize(tabstractvarsym(sym).getsize,false);
  783. arraydef :
  784. begin
  785. { for arrays try to get the element size, take care of
  786. multiple indexes }
  787. harrdef:=tarraydef(tabstractvarsym(sym).vardef);
  788. { calc array size }
  789. if is_special_array(harrdef) then
  790. l := -1
  791. else
  792. l := harrdef.size;
  793. case opr.typ of
  794. OPR_REFERENCE: opr.varsize := l;
  795. OPR_LOCAL: opr.localvarsize := l;
  796. end;
  797. while assigned(harrdef.elementdef) and
  798. (harrdef.elementdef.typ=arraydef) do
  799. harrdef:=tarraydef(harrdef.elementdef);
  800. if not is_packed_array(harrdef) then
  801. SetSize(harrdef.elesize,false)
  802. else
  803. if (harrdef.elepackedbitsize mod 8) = 0 then
  804. SetSize(harrdef.elepackedbitsize div 8,false);
  805. end;
  806. recorddef:
  807. case opr.typ of
  808. OPR_REFERENCE: opr.varsize := tabstractvarsym(sym).getsize;
  809. OPR_LOCAL: opr.localvarsize := tabstractvarsym(sym).getsize;
  810. end;
  811. end;
  812. hasvar:=true;
  813. SetupVar:=true;
  814. Exit;
  815. end;
  816. constsym :
  817. begin
  818. if tconstsym(sym).consttyp=constord then
  819. begin
  820. setconst(tconstsym(sym).value.valueord.svalue);
  821. SetupVar:=true;
  822. Exit;
  823. end;
  824. end;
  825. typesym :
  826. begin
  827. if ttypesym(sym).typedef.typ in [recorddef,objectdef] then
  828. begin
  829. setconst(0);
  830. SetupVar:=TRUE;
  831. Exit;
  832. end;
  833. end;
  834. procsym :
  835. begin
  836. if opr.typ<>OPR_NONE then
  837. Message(asmr_e_invalid_operand_type);
  838. if Tprocsym(sym).ProcdefList.Count>1 then
  839. Message(asmr_w_calling_overload_func);
  840. l:=opr.ref.offset;
  841. opr.typ:=OPR_SYMBOL;
  842. opr.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname);
  843. opr.symofs:=l;
  844. hasvar:=true;
  845. SetupVar:=TRUE;
  846. Exit;
  847. end;
  848. else
  849. begin
  850. Message(asmr_e_unsupported_symbol_type);
  851. exit;
  852. end;
  853. end;
  854. end;
  855. procedure TOperand.InitRef;
  856. {*********************************************************************}
  857. { Description: This routine first check if the opcode is of }
  858. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  859. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  860. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  861. { to point to the default segment. }
  862. {*********************************************************************}
  863. var
  864. l : aint;
  865. hsymofs : aint;
  866. hsymbol : tasmsymbol;
  867. reg : tregister;
  868. Begin
  869. case opr.typ of
  870. OPR_REFERENCE :
  871. exit;
  872. OPR_CONSTANT :
  873. begin
  874. l:=opr.val;
  875. opr.typ:=OPR_REFERENCE;
  876. Fillchar(opr.ref,sizeof(treference),0);
  877. opr.Ref.Offset:=l;
  878. opr.varsize:=0;
  879. opr.constoffset:=0;
  880. end;
  881. OPR_NONE :
  882. begin
  883. opr.typ:=OPR_REFERENCE;
  884. opr.varsize:=0;
  885. opr.constoffset:=0;
  886. Fillchar(opr.ref,sizeof(treference),0);
  887. end;
  888. OPR_REGISTER :
  889. begin
  890. reg:=opr.reg;
  891. opr.typ:=OPR_REFERENCE;
  892. opr.varsize:=0;
  893. opr.constoffset:=0;
  894. Fillchar(opr.ref,sizeof(treference),0);
  895. opr.Ref.base:=reg;
  896. end;
  897. OPR_SYMBOL :
  898. begin
  899. hsymbol:=opr.symbol;
  900. hsymofs:=opr.symofs;
  901. opr.typ:=OPR_REFERENCE;
  902. opr.varsize:=0;
  903. opr.constoffset:=0;
  904. Fillchar(opr.ref,sizeof(treference),0);
  905. opr.ref.symbol:=hsymbol;
  906. opr.ref.offset:=hsymofs;
  907. end;
  908. else
  909. begin
  910. Message(asmr_e_invalid_operand_type);
  911. { Recover }
  912. opr.typ:=OPR_REFERENCE;
  913. opr.varsize:=0;
  914. opr.constoffset:=0;
  915. Fillchar(opr.ref,sizeof(treference),0);
  916. end;
  917. end;
  918. end;
  919. Function TOperand.CheckOperand: boolean;
  920. {*********************************************************************}
  921. { Description: This routine checks if the operand is of }
  922. { valid, and returns false if it isn't. Does nothing by default. }
  923. {*********************************************************************}
  924. begin
  925. result:=true;
  926. end;
  927. {****************************************************************************
  928. TInstruction
  929. ****************************************************************************}
  930. constructor TInstruction.create(optype : tcoperand);
  931. var
  932. i : longint;
  933. Begin
  934. { these field are set to 0 anyways by the constructor helper (FK)
  935. Opcode:=A_NONE;
  936. Condition:=C_NONE;
  937. Ops:=0;
  938. }
  939. filepos:=current_filepos;
  940. for i:=1 to max_operands do
  941. Operands[i]:=optype.create;
  942. Labeled:=false;
  943. end;
  944. destructor TInstruction.destroy;
  945. var
  946. i : longint;
  947. Begin
  948. for i:=1 to max_operands do
  949. Operands[i].free;
  950. end;
  951. Procedure TInstruction.Swapoperands;
  952. Var
  953. p : toperand;
  954. Begin
  955. case ops of
  956. 0,1:
  957. ;
  958. 2 : begin
  959. { 0,1 -> 1,0 }
  960. p:=Operands[1];
  961. Operands[1]:=Operands[2];
  962. Operands[2]:=p;
  963. end;
  964. {$ifndef MAX_OPER_2}
  965. 3 : begin
  966. { 0,1,2 -> 2,1,0 }
  967. p:=Operands[1];
  968. Operands[1]:=Operands[3];
  969. Operands[3]:=p;
  970. end;
  971. {$ifndef MAX_OPER_3}
  972. 4 : begin
  973. { 0,1,2,3 -> 3,2,1,0 }
  974. p:=Operands[1];
  975. Operands[1]:=Operands[4];
  976. Operands[4]:=p;
  977. p:=Operands[2];
  978. Operands[2]:=Operands[3];
  979. Operands[3]:=p;
  980. end;
  981. {$endif}
  982. {$endif}
  983. else
  984. internalerror(201108142);
  985. end;
  986. end;
  987. function TInstruction.ConcatInstruction(p:TAsmList) : tai;
  988. var
  989. ai : taicpu;
  990. i : longint;
  991. begin
  992. for i:=1 to Ops do
  993. operands[i].CheckOperand;
  994. ai:=taicpu.op_none(opcode);
  995. ai.fileinfo:=filepos;
  996. ai.Ops:=Ops;
  997. ai.Allocate_oper(Ops);
  998. for i:=1 to Ops do
  999. with operands[i].opr do
  1000. begin
  1001. case typ of
  1002. OPR_CONSTANT :
  1003. ai.loadconst(i-1,val);
  1004. OPR_REGISTER:
  1005. ai.loadreg(i-1,reg);
  1006. OPR_SYMBOL:
  1007. ai.loadsymbol(i-1,symbol,symofs);
  1008. OPR_LOCAL :
  1009. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1010. localscale,localgetoffset,localforceref);
  1011. OPR_REFERENCE:
  1012. ai.loadref(i-1,ref);
  1013. {$ifdef m68k}
  1014. OPR_REGSET:
  1015. ai.loadregset(i-1,regsetdata,regsetaddr);
  1016. {$endif}
  1017. {$ifdef ARM}
  1018. OPR_REGSET:
  1019. ai.loadregset(i-1,regtype,subreg,regset,usermode);
  1020. OPR_SHIFTEROP:
  1021. ai.loadshifterop(i-1,shifterop);
  1022. OPR_COND:
  1023. ai.loadconditioncode(i-1,cc);
  1024. OPR_MODEFLAGS:
  1025. ai.loadmodeflags(i-1,flags);
  1026. OPR_SPECIALREG:
  1027. ai.loadspecialreg(i-1,specialreg,specialregflags);
  1028. {$endif ARM}
  1029. { ignore wrong operand }
  1030. OPR_NONE:
  1031. ;
  1032. else
  1033. internalerror(200501051);
  1034. end;
  1035. end;
  1036. ai.SetCondition(condition);
  1037. { Concat the opcode or give an error }
  1038. if assigned(ai) then
  1039. p.concat(ai)
  1040. else
  1041. Message(asmr_e_invalid_opcode_and_operand);
  1042. result:=ai;
  1043. end;
  1044. {***************************************************************************
  1045. TLocalLabel
  1046. ***************************************************************************}
  1047. constructor TLocalLabel.create(AList:TFPHashObjectList;const n:string);
  1048. begin
  1049. inherited Create(AList,n);
  1050. lab:=nil;
  1051. emitted:=false;
  1052. end;
  1053. function TLocalLabel.Gettasmlabel:tasmlabel;
  1054. begin
  1055. if not assigned(lab) then
  1056. begin
  1057. current_asmdata.getjumplabel(lab);
  1058. { this label is forced to be used so it's always written }
  1059. lab.increfs;
  1060. end;
  1061. Gettasmlabel:=lab;
  1062. end;
  1063. {***************************************************************************
  1064. TLocalLabelList
  1065. ***************************************************************************}
  1066. procedure TLocalLabelList.CheckEmitted;
  1067. var
  1068. i : longint;
  1069. lab : TLocalLabel;
  1070. begin
  1071. for i:=0 to LocalLabelList.Count-1 do
  1072. begin
  1073. lab:=TLocalLabel(LocalLabelList[i]);
  1074. if not lab.emitted then
  1075. Message1(asmr_e_unknown_label_identifier,lab.name);
  1076. end;
  1077. end;
  1078. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1079. var
  1080. lab : TLocalLabel;
  1081. Begin
  1082. CreateLocalLabel:=true;
  1083. { Check if it already is defined }
  1084. lab:=TLocalLabel(LocalLabellist.Find(s));
  1085. if not assigned(lab) then
  1086. lab:=TLocalLabel.Create(LocalLabellist,s);
  1087. { set emitted flag and check for dup syms }
  1088. if emit then
  1089. begin
  1090. if lab.Emitted then
  1091. begin
  1092. Message1(asmr_e_dup_local_sym,lab.Name);
  1093. CreateLocalLabel:=false;
  1094. end;
  1095. lab.Emitted:=true;
  1096. end;
  1097. hl:=lab.Gettasmlabel;
  1098. end;
  1099. {****************************************************************************
  1100. Symbol table helper routines
  1101. ****************************************************************************}
  1102. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:TSymtable);
  1103. var
  1104. i : integer;
  1105. begin
  1106. i:=pos('.',s);
  1107. { allow unit.identifier }
  1108. if i>0 then
  1109. begin
  1110. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1111. if assigned(srsym) then
  1112. begin
  1113. if (srsym.typ=unitsym) and
  1114. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  1115. srsym.owner.iscurrentunit then
  1116. searchsym_in_module(tunitsym(srsym).module,Copy(s,i+1,255),srsym,srsymtable)
  1117. else
  1118. begin
  1119. srsym:=nil;
  1120. srsymtable:=nil;
  1121. end;
  1122. end;
  1123. end
  1124. else
  1125. searchsym(s,srsym,srsymtable);
  1126. end;
  1127. Function SearchType(const hs:string;var size:aint): Boolean;
  1128. var
  1129. srsym : tsym;
  1130. srsymtable : TSymtable;
  1131. begin
  1132. result:=false;
  1133. size:=0;
  1134. asmsearchsym(hs,srsym,srsymtable);
  1135. if assigned(srsym) and
  1136. (srsym.typ=typesym) then
  1137. begin
  1138. size:=ttypesym(srsym).typedef.size;
  1139. result:=true;
  1140. end;
  1141. end;
  1142. Function SearchRecordType(const s:string): boolean;
  1143. var
  1144. srsym : tsym;
  1145. srsymtable : TSymtable;
  1146. Begin
  1147. SearchRecordType:=false;
  1148. { Check the constants in symtable }
  1149. asmsearchsym(s,srsym,srsymtable);
  1150. if srsym <> nil then
  1151. Begin
  1152. case srsym.typ of
  1153. typesym :
  1154. begin
  1155. if ttypesym(srsym).typedef.typ in [recorddef,objectdef] then
  1156. begin
  1157. SearchRecordType:=true;
  1158. exit;
  1159. end;
  1160. end;
  1161. fieldvarsym :
  1162. begin
  1163. if (tfieldvarsym(srsym).vardef.typ in [recorddef,objectdef]) then
  1164. begin
  1165. SearchRecordType:=true;
  1166. exit;
  1167. end;
  1168. end;
  1169. end;
  1170. end;
  1171. end;
  1172. Function SearchIConstant(const s:string; var l:aint): boolean;
  1173. {**********************************************************************}
  1174. { Description: Searches for a CONSTANT of name s in either the local }
  1175. { symbol list, then in the global symbol list, and returns the value }
  1176. { of that constant in l. Returns TRUE if successfull, if not found, }
  1177. { or if the constant is not of correct type, then returns FALSE }
  1178. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1179. { respectively. }
  1180. {**********************************************************************}
  1181. var
  1182. srsym : tsym;
  1183. srsymtable : TSymtable;
  1184. Begin
  1185. SearchIConstant:=false;
  1186. { check for TRUE or FALSE reserved words first }
  1187. if s = 'TRUE' then
  1188. Begin
  1189. SearchIConstant:=TRUE;
  1190. l:=1;
  1191. exit;
  1192. end;
  1193. if s = 'FALSE' then
  1194. Begin
  1195. SearchIConstant:=TRUE;
  1196. l:=0;
  1197. exit;
  1198. end;
  1199. { Check the constants in symtable }
  1200. asmsearchsym(s,srsym,srsymtable);
  1201. if srsym <> nil then
  1202. Begin
  1203. case srsym.typ of
  1204. constsym :
  1205. begin
  1206. if tconstsym(srsym).consttyp=constord then
  1207. Begin
  1208. l:=tconstsym(srsym).value.valueord.svalue;
  1209. SearchIConstant:=TRUE;
  1210. exit;
  1211. end;
  1212. end;
  1213. enumsym:
  1214. Begin
  1215. l:=tenumsym(srsym).value;
  1216. SearchIConstant:=TRUE;
  1217. exit;
  1218. end;
  1219. end;
  1220. end;
  1221. end;
  1222. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint; var mangledname: string; needvmtofs: boolean):boolean;
  1223. { search and returns the offset and size of records/objects of the base }
  1224. { with field name setup in field. }
  1225. { returns FALSE if not found. }
  1226. { used when base is a variable or a typed constant name. }
  1227. var
  1228. st : TSymtable;
  1229. harrdef : tarraydef;
  1230. sym : tsym;
  1231. srsymtable : TSymtable;
  1232. i : longint;
  1233. base : string;
  1234. procdef: tprocdef;
  1235. Begin
  1236. GetRecordOffsetSize:=FALSE;
  1237. Offset:=0;
  1238. Size:=0;
  1239. mangledname:='';
  1240. i:=pos('.',s);
  1241. if i=0 then
  1242. i:=255;
  1243. base:=Copy(s,1,i-1);
  1244. delete(s,1,i);
  1245. if base='SELF' then
  1246. st:=current_structdef.symtable
  1247. else
  1248. begin
  1249. asmsearchsym(base,sym,srsymtable);
  1250. st:=nil;
  1251. { we can start with a var,type,typedconst }
  1252. if assigned(sym) then
  1253. case sym.typ of
  1254. staticvarsym,
  1255. localvarsym,
  1256. paravarsym :
  1257. st:=Tabstractvarsym(sym).vardef.GetSymtable(gs_record);
  1258. typesym :
  1259. st:=Ttypesym(sym).typedef.GetSymtable(gs_record);
  1260. end
  1261. else
  1262. s:='';
  1263. end;
  1264. { now walk all recordsymtables }
  1265. while assigned(st) and (s<>'') do
  1266. begin
  1267. { load next field in base }
  1268. i:=pos('.',s);
  1269. if i=0 then
  1270. i:=255;
  1271. base:=Copy(s,1,i-1);
  1272. delete(s,1,i);
  1273. sym:=search_struct_member(tabstractrecorddef(st.defowner),base);
  1274. if not assigned(sym) then
  1275. begin
  1276. GetRecordOffsetSize:=false;
  1277. exit;
  1278. end;
  1279. st:=nil;
  1280. case sym.typ of
  1281. fieldvarsym :
  1282. with Tfieldvarsym(sym) do
  1283. begin
  1284. if not tabstractrecordsymtable(sym.owner).is_packed then
  1285. inc(Offset,fieldoffset)
  1286. else if tfieldvarsym(sym).fieldoffset mod 8 = 0 then
  1287. inc(Offset,fieldoffset div 8)
  1288. else
  1289. Message(asmr_e_packed_element);
  1290. size:=getsize;
  1291. case vardef.typ of
  1292. arraydef :
  1293. begin
  1294. { for arrays try to get the element size, take care of
  1295. multiple indexes }
  1296. harrdef:=tarraydef(vardef);
  1297. while assigned(harrdef.elementdef) and
  1298. (harrdef.elementdef.typ=arraydef) do
  1299. harrdef:=tarraydef(harrdef.elementdef);
  1300. if not is_packed_array(harrdef) then
  1301. size:=harrdef.elesize
  1302. else
  1303. begin
  1304. if (harrdef.elepackedbitsize mod 8) <> 0 then
  1305. Message(asmr_e_packed_element);
  1306. size := (harrdef.elepackedbitsize + 7) div 8;
  1307. end;
  1308. end;
  1309. recorddef :
  1310. st:=trecorddef(vardef).symtable;
  1311. objectdef :
  1312. st:=tobjectdef(vardef).symtable;
  1313. end;
  1314. end;
  1315. procsym:
  1316. begin
  1317. st:=nil;
  1318. if Tprocsym(sym).ProcdefList.Count>1 then
  1319. Message(asmr_w_calling_overload_func);
  1320. procdef:=tprocdef(tprocsym(sym).ProcdefList[0]);
  1321. if (not needvmtofs) then
  1322. begin
  1323. mangledname:=procdef.mangledname;
  1324. end
  1325. else
  1326. begin
  1327. { can only get the vmtoffset of virtual methods }
  1328. if not(po_virtualmethod in procdef.procoptions) or
  1329. is_objectpascal_helper(procdef.struct) then
  1330. Message1(asmr_e_no_vmtoffset_possible,FullTypeName(procdef,nil))
  1331. else
  1332. begin
  1333. { size = sizeof(target_system_pointer) }
  1334. size:=sizeof(pint);
  1335. offset:=tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber)
  1336. end;
  1337. end;
  1338. { if something comes after the procsym, it's invalid assembler syntax }
  1339. GetRecordOffsetSize:=(s='');
  1340. exit;
  1341. end;
  1342. end;
  1343. end;
  1344. { Support Field.Type as typecasting }
  1345. if (st=nil) and (s<>'') then
  1346. begin
  1347. asmsearchsym(s,sym,srsymtable);
  1348. if assigned(sym) and (sym.typ=typesym) then
  1349. begin
  1350. size:=ttypesym(sym).typedef.size;
  1351. s:=''
  1352. end;
  1353. end;
  1354. GetRecordOffsetSize:=(s='');
  1355. end;
  1356. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1357. var
  1358. sym : tsym;
  1359. srsymtable : TSymtable;
  1360. hs : string;
  1361. Begin
  1362. hl:=nil;
  1363. SearchLabel:=false;
  1364. { Check for pascal labels, which are case insensetive }
  1365. hs:=upper(s);
  1366. asmsearchsym(hs,sym,srsymtable);
  1367. if sym=nil then
  1368. exit;
  1369. case sym.typ of
  1370. labelsym :
  1371. begin
  1372. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1373. begin
  1374. Tlabelsym(sym).nonlocal:=true;
  1375. if emit then
  1376. exclude(current_procinfo.procdef.procoptions,po_inline);
  1377. end;
  1378. if not(assigned(tlabelsym(sym).asmblocklabel)) then
  1379. if Tlabelsym(sym).nonlocal then
  1380. current_asmdata.getglobaljumplabel(tlabelsym(sym).asmblocklabel)
  1381. else
  1382. current_asmdata.getjumplabel(tlabelsym(sym).asmblocklabel);
  1383. hl:=tlabelsym(sym).asmblocklabel;
  1384. if emit then
  1385. tlabelsym(sym).defined:=true
  1386. else
  1387. tlabelsym(sym).used:=true;
  1388. SearchLabel:=true;
  1389. end;
  1390. end;
  1391. end;
  1392. {*************************************************************************}
  1393. { Instruction Generation Utilities }
  1394. {*************************************************************************}
  1395. Procedure ConcatString(p : TAsmList;s:string);
  1396. {*********************************************************************}
  1397. { PROCEDURE ConcatString(s:string); }
  1398. { Description: This routine adds the character chain pointed to in }
  1399. { s to the instruction linked list. }
  1400. {*********************************************************************}
  1401. Begin
  1402. p.concat(Tai_string.Create(s));
  1403. end;
  1404. Procedure ConcatConstant(p: TAsmList; value: aint; constsize:byte);
  1405. {*********************************************************************}
  1406. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1407. { Description: This routine adds the value constant to the current }
  1408. { instruction linked list. }
  1409. { maxvalue -> indicates the size of the data to initialize: }
  1410. { $ff -> create a byte node. }
  1411. { $ffff -> create a word node. }
  1412. { $ffffffff -> create a dword node. }
  1413. {*********************************************************************}
  1414. var
  1415. rangelo,rangehi : int64;
  1416. Begin
  1417. case constsize of
  1418. 1 :
  1419. begin
  1420. p.concat(Tai_const.Create_8bit(byte(value)));
  1421. rangelo:=low(shortint);
  1422. rangehi:=high(byte);
  1423. end;
  1424. 2 :
  1425. begin
  1426. p.concat(Tai_const.Create_16bit(word(value)));
  1427. rangelo:=low(smallint);
  1428. rangehi:=high(word);
  1429. end;
  1430. 4 :
  1431. begin
  1432. p.concat(Tai_const.Create_32bit(longint(value)));
  1433. rangelo:=low(longint);
  1434. rangehi:=high(cardinal);
  1435. end;
  1436. 8 :
  1437. begin
  1438. p.concat(Tai_const.Create_64bit(int64(value)));
  1439. rangelo:=0;
  1440. rangehi:=0;
  1441. end;
  1442. else
  1443. internalerror(200405011);
  1444. end;
  1445. { check for out of bounds }
  1446. if (rangelo<>0) and
  1447. ((value>rangehi) or (value<rangelo)) then
  1448. Message(asmr_e_constant_out_of_bounds);
  1449. end;
  1450. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:aint);
  1451. begin
  1452. p.concat(Tai_const.Createname(sym,l));
  1453. end;
  1454. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  1455. {***********************************************************************}
  1456. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1457. { Description: This routine adds the value constant to the current }
  1458. { instruction linked list. }
  1459. { real_typ -> indicates the type of the real data to initialize: }
  1460. { s32real -> create a single node. }
  1461. { s64real -> create a double node. }
  1462. { s80real -> create an extended node. }
  1463. { s64bit -> create a comp node. }
  1464. { f32bit -> create a fixed node. (not used normally) }
  1465. {***********************************************************************}
  1466. Begin
  1467. case real_typ of
  1468. s32real : p.concat(Tai_real_32bit.Create(value));
  1469. s64real :
  1470. {$ifdef ARM}
  1471. if is_double_hilo_swapped then
  1472. p.concat(Tai_real_64bit.Create_hiloswapped(value))
  1473. else
  1474. {$endif ARM}
  1475. p.concat(Tai_real_64bit.Create(value));
  1476. s80real : p.concat(Tai_real_80bit.Create(value,s80floattype.size));
  1477. sc80real : p.concat(Tai_real_80bit.Create(value,sc80floattype.size));
  1478. s64comp : p.concat(Tai_comp_64bit.Create(trunc(value)));
  1479. end;
  1480. end;
  1481. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  1482. {*********************************************************************}
  1483. { PROCEDURE ConcatLabel }
  1484. { Description: This routine either emits a label or a labeled }
  1485. { instruction to the linked list of instructions. }
  1486. {*********************************************************************}
  1487. begin
  1488. p.concat(Tai_label.Create(l));
  1489. end;
  1490. procedure ConcatAlign(p:TAsmList;l:aint);
  1491. {*********************************************************************}
  1492. { PROCEDURE ConcatPublic }
  1493. { Description: This routine emits an global definition to the }
  1494. { linked list of instructions.(used by AT&T styled asm) }
  1495. {*********************************************************************}
  1496. begin
  1497. p.concat(Tai_align.Create(l));
  1498. end;
  1499. procedure ConcatPublic(p:TAsmList;const s : string);
  1500. {*********************************************************************}
  1501. { PROCEDURE ConcatPublic }
  1502. { Description: This routine emits an global definition to the }
  1503. { linked list of instructions.(used by AT&T styled asm) }
  1504. {*********************************************************************}
  1505. begin
  1506. p.concat(Tai_symbol.Createname_global(s,AT_LABEL,0));
  1507. end;
  1508. procedure ConcatLocal(p:TAsmList;const s : string);
  1509. {*********************************************************************}
  1510. { PROCEDURE ConcatLocal }
  1511. { Description: This routine emits an local definition to the }
  1512. { linked list of instructions. }
  1513. {*********************************************************************}
  1514. begin
  1515. p.concat(Tai_symbol.Createname(s,AT_LABEL,0));
  1516. end;
  1517. end.