rautils.pas 48 KB

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