rautils.pas 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  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,
  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);
  53. TOprRec = record
  54. case typ:TOprType of
  55. OPR_NONE : ();
  56. OPR_CONSTANT : (val:longint);
  57. OPR_SYMBOL : (symbol:tasmsymbol;symofs:longint);
  58. OPR_REFERENCE : (ref:treference);
  59. OPR_LOCAL : (localsym:tvarsym;localsymofs:longint;localindexreg:tregister;localscale:byte;localgetoffset:boolean);
  60. OPR_REGISTER : (reg:tregister);
  61. {$ifdef m68k}
  62. OPR_REGLIST : (reglist:Tsupregset);
  63. {$endif m68k}
  64. {$ifdef powerpc}
  65. OPR_COND : (cond : tasmcond);
  66. {$endif m68k}
  67. end;
  68. TOperand = class
  69. hastype, { if the operand has typecasted variable }
  70. hasvar : boolean; { if the operand is loaded with a variable }
  71. size : TCGSize;
  72. opr : TOprRec;
  73. constructor create;virtual;
  74. destructor destroy;override;
  75. Procedure SetSize(_size:longint;force:boolean);virtual;
  76. Procedure SetCorrectSize(opcode:tasmop);virtual;
  77. Function SetupResult:boolean;virtual;
  78. Function SetupSelf:boolean;
  79. Function SetupOldEBP:boolean;
  80. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  81. Function SetupDirectVar(const hs:string): Boolean;
  82. Procedure InitRef;
  83. end;
  84. TCOperand = class of TOperand;
  85. TInstruction = class
  86. opcode : tasmop;
  87. condition : tasmcond;
  88. ops : byte;
  89. labeled : boolean;
  90. operands : array[1..max_operands] of toperand;
  91. constructor create(optype : tcoperand);virtual;
  92. destructor destroy;override;
  93. Procedure BuildOpcode;virtual;abstract;
  94. { converts the instruction to an instruction how it's used by the assembler writer
  95. and concats it to the passed list, the newly created item is returned }
  96. function ConcatInstruction(p:TAAsmoutput) : tai;virtual;
  97. Procedure Swapoperands;
  98. end;
  99. tstr2opentry = class(Tnamedindexitem)
  100. op: TAsmOp;
  101. end;
  102. {---------------------------------------------------------------------}
  103. { Expression parser types }
  104. {---------------------------------------------------------------------}
  105. TExprOperator = record
  106. ch: char; { operator }
  107. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  108. end;
  109. String15 = String[15];
  110. {**********************************************************************}
  111. { The following operators are supported: }
  112. { '+' : addition }
  113. { '-' : subtraction }
  114. { '*' : multiplication }
  115. { '/' : modulo division }
  116. { '^' : exclusive or }
  117. { '<' : shift left }
  118. { '>' : shift right }
  119. { '&' : bitwise and }
  120. { '|' : bitwise or }
  121. { '~' : bitwise complement }
  122. { '%' : modulo division }
  123. { nnn: longint numbers }
  124. { ( and ) parenthesis }
  125. {**********************************************************************}
  126. TExprParse = class
  127. public
  128. Constructor create;
  129. Destructor Destroy;override;
  130. Function Evaluate(Expr: String): longint;
  131. Function Priority(_Operator: Char): longint;
  132. private
  133. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  134. RPNTop : longint;
  135. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  136. OpTop : longint;
  137. Procedure RPNPush(Num: Longint);
  138. Function RPNPop: Longint;
  139. Procedure RPNCalc(token: String15; prefix: boolean);
  140. Procedure OpPush(_Operator: char; prefix: boolean);
  141. { In reality returns TExprOperaotr }
  142. Procedure OpPop(var _Operator:TExprOperator);
  143. end;
  144. { Evaluate an expression string to a longint }
  145. Function CalculateExpression(const expression: string): longint;
  146. {---------------------------------------------------------------------}
  147. { String routines }
  148. {---------------------------------------------------------------------}
  149. Function ValDecimal(const S:String):longint;
  150. Function ValOctal(const S:String):longint;
  151. Function ValBinary(const S:String):longint;
  152. Function ValHexaDecimal(const S:String):longint;
  153. Function PadZero(Var s: String; n: byte): Boolean;
  154. Function EscapeToPascal(const s:string): string;
  155. {---------------------------------------------------------------------
  156. Symbol helper routines
  157. ---------------------------------------------------------------------}
  158. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  159. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  160. Function SearchType(const hs:string;var size:longint): Boolean;
  161. Function SearchRecordType(const s:string): boolean;
  162. Function SearchIConstant(const s:string; var l:longint): boolean;
  163. {---------------------------------------------------------------------
  164. Instruction generation routines
  165. ---------------------------------------------------------------------}
  166. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  167. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  168. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  169. Procedure ConcatConstant(p : TAAsmoutput;value: longint; maxvalue: longint);
  170. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  171. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  172. Procedure ConcatString(p : TAAsmoutput;s:string);
  173. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  174. Procedure ConcatPublic(p:TAAsmoutput;const s : string);
  175. Procedure ConcatLocal(p:TAAsmoutput;const s : string);
  176. Procedure ConcatGlobalBss(const s : string;size : longint);
  177. Procedure ConcatLocalBss(const s : string;size : longint);
  178. Implementation
  179. uses
  180. {$ifdef delphi}
  181. sysutils,
  182. {$else}
  183. strings,
  184. {$endif}
  185. defutil,systems,verbose,globals,
  186. symtable,paramgr,
  187. aasmcpu,
  188. procinfo;
  189. {*************************************************************************
  190. TExprParse
  191. *************************************************************************}
  192. Constructor TExprParse.create;
  193. Begin
  194. end;
  195. Procedure TExprParse.RPNPush(Num : longint);
  196. { Add an operand to the top of the RPN stack }
  197. begin
  198. if RPNTop < RPNMax then
  199. begin
  200. Inc(RPNTop);
  201. RPNStack[RPNTop]:=Num;
  202. end
  203. else
  204. Message(asmr_e_expr_illegal);
  205. end;
  206. Function TExprParse.RPNPop : longint;
  207. { Get the operand at the top of the RPN stack }
  208. begin
  209. if RPNTop > 0 then
  210. begin
  211. RPNPop:=RPNStack[RPNTop];
  212. Dec(RPNTop);
  213. end
  214. else
  215. Message(asmr_e_expr_illegal);
  216. end;
  217. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  218. Var
  219. Temp : longint;
  220. n1,n2 : longint;
  221. LocalError : Integer;
  222. begin
  223. { Handle operators }
  224. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  225. Case Token[1] of
  226. '+' :
  227. Begin
  228. if not prefix then
  229. RPNPush(RPNPop + RPNPop);
  230. end;
  231. '-' :
  232. Begin
  233. if prefix then
  234. RPNPush(-(RPNPop))
  235. else
  236. begin
  237. n1:=RPNPop;
  238. n2:=RPNPop;
  239. RPNPush(n2 - n1);
  240. end;
  241. end;
  242. '*' : RPNPush(RPNPop * RPNPop);
  243. '&' :
  244. begin
  245. n1:=RPNPop;
  246. n2:=RPNPop;
  247. RPNPush(n2 and n1);
  248. end;
  249. '|' :
  250. begin
  251. n1:=RPNPop;
  252. n2:=RPNPop;
  253. RPNPush(n2 or n1);
  254. end;
  255. '~' : RPNPush(NOT RPNPop);
  256. '<' :
  257. begin
  258. n1:=RPNPop;
  259. n2:=RPNPop;
  260. RPNPush(n2 SHL n1);
  261. end;
  262. '>' :
  263. begin
  264. n1:=RPNPop;
  265. n2:=RPNPop;
  266. RPNPush(n2 SHR n1);
  267. end;
  268. '%' :
  269. begin
  270. Temp:=RPNPop;
  271. if Temp <> 0 then
  272. RPNPush(RPNPop mod Temp)
  273. else
  274. begin
  275. Message(asmr_e_expr_zero_divide);
  276. { push 1 for error recovery }
  277. RPNPush(1);
  278. end;
  279. end;
  280. '^' : RPNPush(RPNPop XOR RPNPop);
  281. '/' :
  282. begin
  283. Temp:=RPNPop;
  284. if Temp <> 0 then
  285. RPNPush(RPNPop div Temp)
  286. else
  287. begin
  288. Message(asmr_e_expr_zero_divide);
  289. { push 1 for error recovery }
  290. RPNPush(1);
  291. end;
  292. end;
  293. end
  294. else
  295. begin
  296. { Convert String to number and add to stack }
  297. Val(Token, Temp, LocalError);
  298. if LocalError = 0 then
  299. RPNPush(Temp)
  300. else
  301. begin
  302. Message(asmr_e_expr_illegal);
  303. { push 1 for error recovery }
  304. RPNPush(1);
  305. end;
  306. end;
  307. end;
  308. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  309. { Add an operator onto top of the stack }
  310. begin
  311. if OpTop < OpMax then
  312. begin
  313. Inc(OpTop);
  314. OpStack[OpTop].ch:=_Operator;
  315. OpStack[OpTop].is_prefix:=prefix;
  316. end
  317. else
  318. Message(asmr_e_expr_illegal);
  319. end;
  320. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  321. { Get operator at the top of the stack }
  322. begin
  323. if OpTop > 0 then
  324. begin
  325. _Operator:=OpStack[OpTop];
  326. Dec(OpTop);
  327. end
  328. else
  329. Message(asmr_e_expr_illegal);
  330. end;
  331. Function TExprParse.Priority(_Operator : Char) : longint;
  332. { Return priority of operator }
  333. { The greater the priority, the higher the precedence }
  334. begin
  335. Case _Operator OF
  336. '(' :
  337. Priority:=0;
  338. '+', '-' :
  339. Priority:=1;
  340. '*', '/','%','<','>' :
  341. Priority:=2;
  342. '|','&','^','~' :
  343. Priority:=0;
  344. else
  345. Message(asmr_e_expr_illegal);
  346. end;
  347. end;
  348. Function TExprParse.Evaluate(Expr : String):longint;
  349. Var
  350. I : LongInt;
  351. Token : String15;
  352. opr : TExprOperator;
  353. begin
  354. Evaluate:=0;
  355. { Reset stacks }
  356. OpTop :=0;
  357. RPNTop:=0;
  358. Token :='';
  359. { nothing to do ? }
  360. if Expr='' then
  361. exit;
  362. For I:=1 to Length(Expr) DO
  363. begin
  364. if Expr[I] in ['0'..'9'] then
  365. begin { Build multi-digit numbers }
  366. Token:=Token + Expr[I];
  367. if I = Length(Expr) then { Send last one to calculator }
  368. RPNCalc(Token,false);
  369. end
  370. else
  371. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  372. begin
  373. if Token <> '' then
  374. begin { Send last built number to calc. }
  375. RPNCalc(Token,false);
  376. Token:='';
  377. end;
  378. Case Expr[I] OF
  379. '(' : OpPush('(',false);
  380. ')' : begin
  381. While OpStack[OpTop].ch <> '(' DO
  382. Begin
  383. OpPop(opr);
  384. RPNCalc(opr.ch,opr.is_prefix);
  385. end;
  386. OpPop(opr); { Pop off and ignore the '(' }
  387. end;
  388. '+','-','~' : Begin
  389. { workaround for -2147483648 }
  390. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  391. begin
  392. token:='-';
  393. expr[i]:='+';
  394. end;
  395. { if start of expression then surely a prefix }
  396. { or if previous char was also an operator }
  397. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  398. OpPush(Expr[I],true)
  399. else
  400. Begin
  401. { Evaluate all higher priority operators }
  402. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  403. Begin
  404. OpPop(opr);
  405. RPNCalc(opr.ch,opr.is_prefix);
  406. end;
  407. OpPush(Expr[I],false);
  408. End;
  409. end;
  410. '*', '/',
  411. '^','|','&',
  412. '%','<','>' : begin
  413. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  414. Begin
  415. OpPop(opr);
  416. RPNCalc(opr.ch,opr.is_prefix);
  417. end;
  418. OpPush(Expr[I],false);
  419. end;
  420. end; { Case }
  421. end
  422. else
  423. Message(asmr_e_expr_illegal); { Handle bad input error }
  424. end;
  425. { Pop off the remaining operators }
  426. While OpTop > 0 do
  427. Begin
  428. OpPop(opr);
  429. RPNCalc(opr.ch,opr.is_prefix);
  430. end;
  431. { The result is stored on the top of the stack }
  432. Evaluate:=RPNPop;
  433. end;
  434. Destructor TExprParse.Destroy;
  435. Begin
  436. end;
  437. Function CalculateExpression(const expression: string): longint;
  438. var
  439. expr: TExprParse;
  440. Begin
  441. expr:=TExprParse.create;
  442. CalculateExpression:=expr.Evaluate(expression);
  443. expr.Free;
  444. end;
  445. {*************************************************************************}
  446. { String conversions/utils }
  447. {*************************************************************************}
  448. Function EscapeToPascal(const s:string): string;
  449. { converts a C styled string - which contains escape }
  450. { characters to a pascal style string. }
  451. var
  452. i,len : longint;
  453. hs : string;
  454. temp : string;
  455. c : char;
  456. Begin
  457. hs:='';
  458. len:=0;
  459. i:=0;
  460. while (i<length(s)) and (len<255) do
  461. begin
  462. Inc(i);
  463. if (s[i]='\') and (i<length(s)) then
  464. Begin
  465. inc(i);
  466. case s[i] of
  467. '\' :
  468. c:='\';
  469. 'b':
  470. c:=#8;
  471. 'f':
  472. c:=#12;
  473. 'n':
  474. c:=#10;
  475. 'r':
  476. c:=#13;
  477. 't':
  478. c:=#9;
  479. '"':
  480. c:='"';
  481. '0'..'7':
  482. Begin
  483. temp:=s[i];
  484. temp:=temp+s[i+1];
  485. temp:=temp+s[i+2];
  486. inc(i,2);
  487. c:=chr(ValOctal(temp));
  488. end;
  489. 'x':
  490. Begin
  491. temp:=s[i+1];
  492. temp:=temp+s[i+2];
  493. inc(i,2);
  494. c:=chr(ValHexaDecimal(temp));
  495. end;
  496. else
  497. Begin
  498. Message1(asmr_e_escape_seq_ignored,s[i]);
  499. c:=s[i];
  500. end;
  501. end;
  502. end
  503. else
  504. c:=s[i];
  505. inc(len);
  506. hs[len]:=c;
  507. end;
  508. hs[0]:=chr(len);
  509. EscapeToPascal:=hs;
  510. end;
  511. Function ValDecimal(const S:String):longint;
  512. { Converts a decimal string to longint }
  513. var
  514. vs,c : longint;
  515. Begin
  516. vs:=0;
  517. for c:=1 to length(s) do
  518. begin
  519. vs:=vs*10;
  520. if s[c] in ['0'..'9'] then
  521. inc(vs,ord(s[c])-ord('0'))
  522. else
  523. begin
  524. Message1(asmr_e_error_converting_decimal,s);
  525. ValDecimal:=0;
  526. exit;
  527. end;
  528. end;
  529. ValDecimal:=vs;
  530. end;
  531. Function ValOctal(const S:String):longint;
  532. { Converts an octal string to longint }
  533. var
  534. vs,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):longint;
  552. { Converts a binary string to longint }
  553. var
  554. vs,c : longint;
  555. Begin
  556. vs:=0;
  557. for c:=1 to length(s) do
  558. begin
  559. vs:=vs shl 1;
  560. if s[c] in ['0'..'1'] then
  561. inc(vs,ord(s[c])-ord('0'))
  562. else
  563. begin
  564. Message1(asmr_e_error_converting_binary,s);
  565. ValBinary:=0;
  566. exit;
  567. end;
  568. end;
  569. ValBinary:=vs;
  570. end;
  571. Function ValHexadecimal(const S:String):longint;
  572. { Converts a binary string to longint }
  573. var
  574. vs,c : longint;
  575. Begin
  576. vs:=0;
  577. for c:=1 to length(s) do
  578. begin
  579. vs:=vs shl 4;
  580. case s[c] of
  581. '0'..'9' :
  582. inc(vs,ord(s[c])-ord('0'));
  583. 'A'..'F' :
  584. inc(vs,ord(s[c])-ord('A')+10);
  585. 'a'..'f' :
  586. inc(vs,ord(s[c])-ord('a')+10);
  587. else
  588. begin
  589. Message1(asmr_e_error_converting_hexadecimal,s);
  590. ValHexadecimal:=0;
  591. exit;
  592. end;
  593. end;
  594. end;
  595. ValHexadecimal:=vs;
  596. end;
  597. Function PadZero(Var s: String; n: byte): Boolean;
  598. Begin
  599. PadZero:=TRUE;
  600. { Do some error checking first }
  601. if Length(s) = n then
  602. exit
  603. else
  604. if Length(s) > n then
  605. Begin
  606. PadZero:=FALSE;
  607. delete(s,n+1,length(s));
  608. exit;
  609. end
  610. else
  611. PadZero:=TRUE;
  612. { Fill it up with the specified character }
  613. fillchar(s[length(s)+1],n-1,#0);
  614. s[0]:=chr(n);
  615. end;
  616. {****************************************************************************
  617. TOperand
  618. ****************************************************************************}
  619. constructor TOperand.Create;
  620. begin
  621. size:=OS_NO;
  622. hastype:=false;
  623. hasvar:=false;
  624. FillChar(Opr,sizeof(Opr),0);
  625. end;
  626. destructor TOperand.destroy;
  627. begin
  628. end;
  629. Procedure TOperand.SetSize(_size:longint;force:boolean);
  630. begin
  631. if force or
  632. ((size = OS_NO) and (_size<=extended_size)) then
  633. Begin
  634. case _size of
  635. 1 : size:=OS_8;
  636. 2 : size:=OS_16{ could be S_IS};
  637. 4 : size:=OS_32{ could be S_IL or S_FS};
  638. 8 : size:=OS_64{ could be S_D or S_FL};
  639. else
  640. begin
  641. { extended_size can also be 8, resulting in a
  642. duplicate label }
  643. if _size=extended_size then
  644. size:=OS_F80;
  645. end;
  646. end;
  647. end;
  648. end;
  649. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  650. begin
  651. end;
  652. Function TOperand.SetupResult:boolean;
  653. Begin
  654. SetupResult:=false;
  655. { replace by correct offset. }
  656. if (not is_void(current_procinfo.procdef.rettype.def)) then
  657. begin
  658. if (m_tp7 in aktmodeswitches) and
  659. (not paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption)) then
  660. begin
  661. Message(asmr_e_cannot_use_RESULT_here);
  662. exit;
  663. end;
  664. SetupResult:=setupvar('result',false)
  665. end
  666. else
  667. Message(asmr_e_void_function);
  668. end;
  669. Function TOperand.SetupSelf:boolean;
  670. Begin
  671. SetupSelf:=false;
  672. if assigned(current_procinfo.procdef._class) then
  673. SetupSelf:=setupvar('self',false)
  674. else
  675. Message(asmr_e_cannot_use_SELF_outside_a_method);
  676. end;
  677. Function TOperand.SetupOldEBP:boolean;
  678. Begin
  679. SetupOldEBP:=false;
  680. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  681. SetupOldEBP:=setupvar('parentframe',false)
  682. else
  683. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  684. end;
  685. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  686. function symtable_has_varsyms(st:tsymtable):boolean;
  687. var
  688. sym : tsym;
  689. begin
  690. result:=false;
  691. sym:=tsym(st.symindex.first);
  692. while assigned(sym) do
  693. begin
  694. if sym.typ=varsym then
  695. begin
  696. result:=true;
  697. exit;
  698. end;
  699. sym:=tsym(sym.indexnext);
  700. end;
  701. end;
  702. procedure setconst(l:longint);
  703. begin
  704. { We return the address of the field, just like Delphi/TP }
  705. case opr.typ of
  706. OPR_NONE :
  707. begin
  708. opr.typ:=OPR_CONSTANT;
  709. opr.val:=l;
  710. end;
  711. OPR_CONSTANT :
  712. inc(opr.val,l);
  713. OPR_REFERENCE :
  714. inc(opr.ref.offset,l);
  715. OPR_LOCAL :
  716. inc(opr.localsymofs,l);
  717. else
  718. Message(asmr_e_invalid_operand_type);
  719. end;
  720. end;
  721. { search and sets up the correct fields in the Instr record }
  722. { for the NON-constant identifier passed to the routine. }
  723. { if not found returns FALSE. }
  724. var
  725. sym : tsym;
  726. srsymtable : tsymtable;
  727. harrdef : tarraydef;
  728. indexreg : tregister;
  729. l : longint;
  730. plist : psymlistitem;
  731. Begin
  732. SetupVar:=false;
  733. asmsearchsym(s,sym,srsymtable);
  734. if sym = nil then
  735. exit;
  736. if sym.typ=absolutesym then
  737. begin
  738. if (tabsolutesym(sym).abstyp=tovar) then
  739. begin
  740. { Only support simple loads }
  741. plist:=tabsolutesym(sym).ref.firstsym;
  742. if assigned(plist) and
  743. (plist^.sltype=sl_load) then
  744. sym:=plist^.sym
  745. else
  746. begin
  747. Message(asmr_e_unsupported_symbol_type);
  748. exit;
  749. end;
  750. end
  751. else
  752. begin
  753. Message(asmr_e_unsupported_symbol_type);
  754. exit;
  755. end;
  756. end;
  757. case sym.typ of
  758. varsym :
  759. begin
  760. { we always assume in asm statements that }
  761. { that the variable is valid. }
  762. tvarsym(sym).varstate:=vs_used;
  763. inc(tvarsym(sym).refs);
  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.newasmsymboldata(tvarsym(sym).mangledname);
  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.newasmsymboldata(tvarsym(sym).mangledname)
  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(pointer_size,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.newasmsymboldata(ttypedconstsym(sym).mangledname);
  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 in [constint,constchar,constbool] 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);
  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 : longint;
  929. Begin
  930. case opr.typ of
  931. OPR_REFERENCE :
  932. exit;
  933. OPR_CONSTANT :
  934. begin
  935. l:=opr.val;
  936. opr.typ:=OPR_REFERENCE;
  937. Fillchar(opr.ref,sizeof(treference),0);
  938. opr.Ref.Offset:=l;
  939. end;
  940. OPR_NONE :
  941. begin
  942. opr.typ:=OPR_REFERENCE;
  943. Fillchar(opr.ref,sizeof(treference),0);
  944. end;
  945. else
  946. begin
  947. Message(asmr_e_invalid_operand_type);
  948. { Recover }
  949. opr.typ:=OPR_REFERENCE;
  950. Fillchar(opr.ref,sizeof(treference),0);
  951. end;
  952. end;
  953. end;
  954. {****************************************************************************
  955. TInstruction
  956. ****************************************************************************}
  957. constructor TInstruction.create(optype : tcoperand);
  958. var
  959. i : longint;
  960. Begin
  961. { these field are set to 0 anyways by the constructor helper (FK)
  962. Opcode:=A_NONE;
  963. Condition:=C_NONE;
  964. Ops:=0;
  965. }
  966. for i:=1 to max_operands do
  967. Operands[i]:=optype.create;
  968. Labeled:=false;
  969. end;
  970. destructor TInstruction.destroy;
  971. var
  972. i : longint;
  973. Begin
  974. for i:=1 to max_operands do
  975. Operands[i].free;
  976. end;
  977. Procedure TInstruction.Swapoperands;
  978. Var
  979. p : toperand;
  980. Begin
  981. case Ops of
  982. 2 :
  983. begin
  984. p:=Operands[1];
  985. Operands[1]:=Operands[2];
  986. Operands[2]:=p;
  987. end;
  988. 3 :
  989. begin
  990. p:=Operands[1];
  991. Operands[1]:=Operands[3];
  992. Operands[3]:=p;
  993. end;
  994. end;
  995. end;
  996. function TInstruction.ConcatInstruction(p:TAAsmoutput) : tai;
  997. var
  998. ai : taicpu;
  999. i : longint;
  1000. begin
  1001. ai:=taicpu.op_none(opcode);
  1002. ai.Ops:=Ops;
  1003. ai.Allocate_oper(Ops);
  1004. for i:=1 to Ops do
  1005. begin
  1006. case operands[i].opr.typ of
  1007. OPR_CONSTANT :
  1008. ai.loadconst(i-1,aword(operands[i].opr.val));
  1009. OPR_REGISTER:
  1010. ai.loadreg(i-1,operands[i].opr.reg);
  1011. OPR_SYMBOL:
  1012. ai.loadsymbol(i-1,operands[i].opr.symbol,operands[i].opr.symofs);
  1013. OPR_LOCAL :
  1014. ai.loadlocal(i-1,operands[i].opr.localsym,operands[i].opr.localsymofs,operands[i].opr.localindexreg,
  1015. operands[i].opr.localscale,operands[i].opr.localgetoffset);
  1016. OPR_REFERENCE:
  1017. ai.loadref(i-1,operands[i].opr.ref);
  1018. end;
  1019. end;
  1020. ai.SetCondition(condition);
  1021. { Concat the opcode or give an error }
  1022. if assigned(ai) then
  1023. p.concat(ai)
  1024. else
  1025. Message(asmr_e_invalid_opcode_and_operand);
  1026. result:=ai;
  1027. end;
  1028. {***************************************************************************
  1029. TLocalLabel
  1030. ***************************************************************************}
  1031. constructor TLocalLabel.create(const n:string);
  1032. begin
  1033. inherited CreateName(n);
  1034. lab:=nil;
  1035. emitted:=false;
  1036. end;
  1037. function TLocalLabel.Gettasmlabel:tasmlabel;
  1038. begin
  1039. if not assigned(lab) then
  1040. begin
  1041. objectlibrary.getlabel(lab);
  1042. { this label is forced to be used so it's always written }
  1043. lab.increfs;
  1044. end;
  1045. Gettasmlabel:=lab;
  1046. end;
  1047. {***************************************************************************
  1048. TLocalLabelList
  1049. ***************************************************************************}
  1050. procedure LocalLabelEmitted(p:tnamedindexitem;arg:pointer);
  1051. begin
  1052. if not TLocalLabel(p).emitted then
  1053. Message1(asmr_e_unknown_label_identifier,p.name);
  1054. end;
  1055. procedure TLocalLabelList.CheckEmitted;
  1056. begin
  1057. ForEach_Static({$ifdef FPCPROCVAR}@{$endif}LocalLabelEmitted,nil)
  1058. end;
  1059. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1060. var
  1061. lab : TLocalLabel;
  1062. Begin
  1063. CreateLocalLabel:=true;
  1064. { Check if it already is defined }
  1065. lab:=TLocalLabel(LocalLabellist.Search(s));
  1066. if not assigned(lab) then
  1067. begin
  1068. lab:=TLocalLabel.Create(s);
  1069. LocalLabellist.Insert(lab);
  1070. end;
  1071. { set emitted flag and check for dup syms }
  1072. if emit then
  1073. begin
  1074. if lab.Emitted then
  1075. begin
  1076. Message1(asmr_e_dup_local_sym,lab.Name);
  1077. CreateLocalLabel:=false;
  1078. end;
  1079. lab.Emitted:=true;
  1080. end;
  1081. hl:=lab.Gettasmlabel;
  1082. end;
  1083. {****************************************************************************
  1084. Symbol table helper routines
  1085. ****************************************************************************}
  1086. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1087. var
  1088. i : integer;
  1089. begin
  1090. i:=pos('.',s);
  1091. { allow unit.identifier }
  1092. if i>0 then
  1093. begin
  1094. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1095. if assigned(srsym) then
  1096. begin
  1097. if (srsym.typ=unitsym) and
  1098. (srsym.owner.unitid=0) then
  1099. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1100. else
  1101. srsym:=nil;
  1102. end;
  1103. end
  1104. else
  1105. searchsym(s,srsym,srsymtable);
  1106. end;
  1107. Function SearchType(const hs:string;var size:longint): Boolean;
  1108. var
  1109. srsym : tsym;
  1110. srsymtable : tsymtable;
  1111. begin
  1112. result:=false;
  1113. size:=0;
  1114. asmsearchsym(hs,srsym,srsymtable);
  1115. if assigned(srsym) and
  1116. (srsym.typ=typesym) then
  1117. begin
  1118. size:=ttypesym(srsym).restype.def.size;
  1119. result:=true;
  1120. end;
  1121. end;
  1122. Function SearchRecordType(const s:string): boolean;
  1123. var
  1124. srsym : tsym;
  1125. srsymtable : tsymtable;
  1126. Begin
  1127. SearchRecordType:=false;
  1128. { Check the constants in symtable }
  1129. asmsearchsym(s,srsym,srsymtable);
  1130. if srsym <> nil then
  1131. Begin
  1132. case srsym.typ of
  1133. typesym :
  1134. begin
  1135. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1136. begin
  1137. SearchRecordType:=true;
  1138. exit;
  1139. end;
  1140. end;
  1141. end;
  1142. end;
  1143. end;
  1144. Function SearchIConstant(const s:string; var l:longint): boolean;
  1145. {**********************************************************************}
  1146. { Description: Searches for a CONSTANT of name s in either the local }
  1147. { symbol list, then in the global symbol list, and returns the value }
  1148. { of that constant in l. Returns TRUE if successfull, if not found, }
  1149. { or if the constant is not of correct type, then returns FALSE }
  1150. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1151. { respectively. }
  1152. {**********************************************************************}
  1153. var
  1154. srsym : tsym;
  1155. srsymtable : tsymtable;
  1156. Begin
  1157. SearchIConstant:=false;
  1158. { check for TRUE or FALSE reserved words first }
  1159. if s = 'TRUE' then
  1160. Begin
  1161. SearchIConstant:=TRUE;
  1162. l:=1;
  1163. exit;
  1164. end;
  1165. if s = 'FALSE' then
  1166. Begin
  1167. SearchIConstant:=TRUE;
  1168. l:=0;
  1169. exit;
  1170. end;
  1171. { Check the constants in symtable }
  1172. asmsearchsym(s,srsym,srsymtable);
  1173. if srsym <> nil then
  1174. Begin
  1175. case srsym.typ of
  1176. constsym :
  1177. begin
  1178. if (tconstsym(srsym).consttyp in [constord,constint,constchar,constbool]) then
  1179. Begin
  1180. l:=tconstsym(srsym).value.valueord;
  1181. SearchIConstant:=TRUE;
  1182. exit;
  1183. end;
  1184. end;
  1185. enumsym:
  1186. Begin
  1187. l:=tenumsym(srsym).value;
  1188. SearchIConstant:=TRUE;
  1189. exit;
  1190. end;
  1191. end;
  1192. end;
  1193. end;
  1194. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1195. { search and returns the offset and size of records/objects of the base }
  1196. { with field name setup in field. }
  1197. { returns FALSE if not found. }
  1198. { used when base is a variable or a typed constant name. }
  1199. var
  1200. st : tsymtable;
  1201. harrdef : tarraydef;
  1202. sym : tsym;
  1203. srsymtable : tsymtable;
  1204. i : longint;
  1205. base : string;
  1206. Begin
  1207. GetRecordOffsetSize:=FALSE;
  1208. Offset:=0;
  1209. Size:=0;
  1210. i:=pos('.',s);
  1211. if i=0 then
  1212. i:=255;
  1213. base:=Copy(s,1,i-1);
  1214. delete(s,1,i);
  1215. if base='SELF' then
  1216. st:=current_procinfo.procdef._class.symtable
  1217. else
  1218. begin
  1219. asmsearchsym(base,sym,srsymtable);
  1220. st:=nil;
  1221. { we can start with a var,type,typedconst }
  1222. case sym.typ of
  1223. varsym :
  1224. begin
  1225. case tvarsym(sym).vartype.def.deftype of
  1226. recorddef :
  1227. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1228. objectdef :
  1229. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1230. end;
  1231. end;
  1232. typesym :
  1233. begin
  1234. case ttypesym(sym).restype.def.deftype of
  1235. recorddef :
  1236. st:=trecorddef(ttypesym(sym).restype.def).symtable;
  1237. objectdef :
  1238. st:=tobjectdef(ttypesym(sym).restype.def).symtable;
  1239. end;
  1240. end;
  1241. typedconstsym :
  1242. begin
  1243. case ttypedconstsym(sym).typedconsttype.def.deftype of
  1244. recorddef :
  1245. st:=trecorddef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1246. objectdef :
  1247. st:=tobjectdef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1248. end;
  1249. end;
  1250. end;
  1251. end;
  1252. { now walk all recordsymtables }
  1253. while assigned(st) and (s<>'') do
  1254. begin
  1255. { load next field in base }
  1256. i:=pos('.',s);
  1257. if i=0 then
  1258. i:=255;
  1259. base:=Copy(s,1,i-1);
  1260. delete(s,1,i);
  1261. if st.symtabletype=objectsymtable then
  1262. sym:=search_class_member(tobjectdef(st.defowner),base)
  1263. else
  1264. sym:=tsym(st.search(base));
  1265. if not assigned(sym) then
  1266. begin
  1267. GetRecordOffsetSize:=false;
  1268. exit;
  1269. end;
  1270. st:=nil;
  1271. case sym.typ of
  1272. varsym :
  1273. begin
  1274. inc(Offset,tvarsym(sym).fieldoffset);
  1275. Size:=tvarsym(sym).getsize;
  1276. case tvarsym(sym).vartype.def.deftype of
  1277. arraydef :
  1278. begin
  1279. { for arrays try to get the element size, take care of
  1280. multiple indexes }
  1281. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  1282. while assigned(harrdef.elementtype.def) and
  1283. (harrdef.elementtype.def.deftype=arraydef) do
  1284. harrdef:=tarraydef(harrdef.elementtype.def);
  1285. size:=harrdef.elesize;
  1286. end;
  1287. recorddef :
  1288. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1289. objectdef :
  1290. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1291. end;
  1292. end;
  1293. end;
  1294. end;
  1295. GetRecordOffsetSize:=(s='');
  1296. end;
  1297. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1298. var
  1299. sym : tsym;
  1300. srsymtable : tsymtable;
  1301. hs : string;
  1302. Begin
  1303. hl:=nil;
  1304. SearchLabel:=false;
  1305. { Check for pascal labels, which are case insensetive }
  1306. hs:=upper(s);
  1307. asmsearchsym(hs,sym,srsymtable);
  1308. if sym=nil then
  1309. exit;
  1310. case sym.typ of
  1311. labelsym :
  1312. begin
  1313. hl:=tlabelsym(sym).lab;
  1314. if emit then
  1315. tlabelsym(sym).defined:=true
  1316. else
  1317. tlabelsym(sym).used:=true;
  1318. SearchLabel:=true;
  1319. exit;
  1320. end;
  1321. end;
  1322. end;
  1323. {*************************************************************************}
  1324. { Instruction Generation Utilities }
  1325. {*************************************************************************}
  1326. Procedure ConcatString(p : TAAsmoutput;s:string);
  1327. {*********************************************************************}
  1328. { PROCEDURE ConcatString(s:string); }
  1329. { Description: This routine adds the character chain pointed to in }
  1330. { s to the instruction linked list. }
  1331. {*********************************************************************}
  1332. Var
  1333. pc: PChar;
  1334. Begin
  1335. getmem(pc,length(s)+1);
  1336. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1337. end;
  1338. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1339. {*********************************************************************}
  1340. { PROCEDURE ConcatPasString(s:string); }
  1341. { Description: This routine adds the character chain pointed to in }
  1342. { s to the instruction linked list, contrary to ConcatString it }
  1343. { uses a pascal style string, so it conserves null characters. }
  1344. {*********************************************************************}
  1345. Begin
  1346. p.concat(Tai_string.Create(s));
  1347. end;
  1348. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1349. {*********************************************************************}
  1350. { PROCEDURE ConcatDirect(s:string) }
  1351. { Description: This routine output the string directly to the asm }
  1352. { output, it is only sed when writing special labels in AT&T mode, }
  1353. { and should not be used without due consideration, since it may }
  1354. { cause problems. }
  1355. {*********************************************************************}
  1356. Var
  1357. pc: PChar;
  1358. Begin
  1359. getmem(pc,length(s)+1);
  1360. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1361. end;
  1362. Procedure ConcatConstant(p: TAAsmoutput; value: longint; maxvalue: longint);
  1363. {*********************************************************************}
  1364. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1365. { Description: This routine adds the value constant to the current }
  1366. { instruction linked list. }
  1367. { maxvalue -> indicates the size of the data to initialize: }
  1368. { $ff -> create a byte node. }
  1369. { $ffff -> create a word node. }
  1370. { $ffffffff -> create a dword node. }
  1371. {*********************************************************************}
  1372. Begin
  1373. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1374. Begin
  1375. Message(asmr_e_constant_out_of_bounds);
  1376. { assuming a value of maxvalue }
  1377. value:=maxvalue;
  1378. end;
  1379. if maxvalue = $ff then
  1380. p.concat(Tai_const.Create_8bit(byte(value)))
  1381. else
  1382. if maxvalue = $ffff then
  1383. p.concat(Tai_const.Create_16bit(word(value)))
  1384. else
  1385. if maxvalue = longint($ffffffff) then
  1386. p.concat(Tai_const.Create_32bit(longint(value)));
  1387. end;
  1388. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  1389. begin
  1390. p.concat(Tai_const_symbol.Createname_offset(sym,l));
  1391. end;
  1392. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1393. {***********************************************************************}
  1394. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1395. { Description: This routine adds the value constant to the current }
  1396. { instruction linked list. }
  1397. { real_typ -> indicates the type of the real data to initialize: }
  1398. { s32real -> create a single node. }
  1399. { s64real -> create a double node. }
  1400. { s80real -> create an extended node. }
  1401. { s64bit -> create a comp node. }
  1402. { f32bit -> create a fixed node. (not used normally) }
  1403. {***********************************************************************}
  1404. Begin
  1405. case real_typ of
  1406. s32real : p.concat(Tai_real_32bit.Create(value));
  1407. s64real : p.concat(Tai_real_64bit.Create(value));
  1408. s80real : p.concat(Tai_real_80bit.Create(value));
  1409. s64comp : p.concat(Tai_comp_64bit.Create(value));
  1410. end;
  1411. end;
  1412. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1413. {*********************************************************************}
  1414. { PROCEDURE ConcatLabel }
  1415. { Description: This routine either emits a label or a labeled }
  1416. { instruction to the linked list of instructions. }
  1417. {*********************************************************************}
  1418. begin
  1419. p.concat(Tai_label.Create(l));
  1420. end;
  1421. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  1422. {*********************************************************************}
  1423. { PROCEDURE ConcatPublic }
  1424. { Description: This routine emits an global definition to the }
  1425. { linked list of instructions.(used by AT&T styled asm) }
  1426. {*********************************************************************}
  1427. begin
  1428. p.concat(Tai_align.Create(l));
  1429. end;
  1430. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1431. {*********************************************************************}
  1432. { PROCEDURE ConcatPublic }
  1433. { Description: This routine emits an global definition to the }
  1434. { linked list of instructions.(used by AT&T styled asm) }
  1435. {*********************************************************************}
  1436. begin
  1437. p.concat(Tai_symbol.Createname_global(s,0));
  1438. end;
  1439. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1440. {*********************************************************************}
  1441. { PROCEDURE ConcatLocal }
  1442. { Description: This routine emits an local definition to the }
  1443. { linked list of instructions. }
  1444. {*********************************************************************}
  1445. begin
  1446. p.concat(Tai_symbol.Createname(s,0));
  1447. end;
  1448. Procedure ConcatGlobalBss(const s : string;size : longint);
  1449. {*********************************************************************}
  1450. { PROCEDURE ConcatGlobalBss }
  1451. { Description: This routine emits an global datablock to the }
  1452. { linked list of instructions. }
  1453. {*********************************************************************}
  1454. begin
  1455. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1456. end;
  1457. Procedure ConcatLocalBss(const s : string;size : longint);
  1458. {*********************************************************************}
  1459. { PROCEDURE ConcatLocalBss }
  1460. { Description: This routine emits a local datablcok to the }
  1461. { linked list of instructions. }
  1462. {*********************************************************************}
  1463. begin
  1464. bssSegment.concat(Tai_datablock.Create(s,size));
  1465. end;
  1466. end.
  1467. {
  1468. $Log$
  1469. Revision 1.78 2003-11-17 23:23:47 florian
  1470. + first part of arm assembler reader
  1471. Revision 1.77 2003/11/12 16:05:39 florian
  1472. * assembler readers OOPed
  1473. + typed currency constants
  1474. + typed 128 bit float constants if the CPU supports it
  1475. Revision 1.76 2003/10/30 19:59:00 peter
  1476. * support scalefactor for opr_local
  1477. * support reference with opr_local set, fixes tw2631
  1478. Revision 1.75 2003/10/29 16:47:18 peter
  1479. * fix field offset in reference
  1480. Revision 1.74 2003/10/29 15:40:20 peter
  1481. * support indexing and offset retrieval for locals
  1482. Revision 1.73 2003/10/28 15:36:01 peter
  1483. * absolute to object field supported, fixes tb0458
  1484. Revision 1.72 2003/10/24 17:39:03 peter
  1485. * more intel parser updates
  1486. Revision 1.71 2003/10/23 17:19:11 peter
  1487. * SearchType returns also the size
  1488. Revision 1.70 2003/10/08 19:39:58 peter
  1489. * allow access to parent locals when the currnet localst has no
  1490. varsyms
  1491. Revision 1.69 2003/10/01 20:34:49 peter
  1492. * procinfo unit contains tprocinfo
  1493. * cginfo renamed to cgbase
  1494. * moved cgmessage to verbose
  1495. * fixed ppc and sparc compiles
  1496. Revision 1.68 2003/09/25 14:57:36 peter
  1497. * fix check for unreachable locals
  1498. Revision 1.67 2003/09/23 17:56:06 peter
  1499. * locals and paras are allocated in the code generation
  1500. * tvarsym.localloc contains the location of para/local when
  1501. generating code for the current procedure
  1502. Revision 1.66 2003/09/16 16:17:01 peter
  1503. * varspez in calls to push_addr_param
  1504. Revision 1.65 2003/09/03 15:55:01 peter
  1505. * NEWRA branch merged
  1506. Revision 1.64.2.1 2003/08/27 19:55:54 peter
  1507. * first tregister patch
  1508. Revision 1.64 2003/06/13 21:19:31 peter
  1509. * current_procdef removed, use current_procinfo.procdef instead
  1510. Revision 1.63 2003/06/06 14:43:29 peter
  1511. * absolutesym support
  1512. Revision 1.62 2003/05/30 23:57:08 peter
  1513. * more sparc cleanup
  1514. * accumulator removed, splitted in function_return_reg (called) and
  1515. function_result_reg (caller)
  1516. Revision 1.61 2003/05/25 08:55:49 peter
  1517. * load result using hidden parameter
  1518. Revision 1.60 2003/05/15 18:58:53 peter
  1519. * removed selfpointer_offset, vmtpointer_offset
  1520. * tvarsym.adjusted_address
  1521. * address in localsymtable is now in the real direction
  1522. * removed some obsolete globals
  1523. Revision 1.59 2003/05/12 17:22:00 jonas
  1524. * fixed (last?) remaining -tvarsym(X).address to
  1525. tg.direction*tvarsym(X).address...
  1526. Revision 1.58 2003/04/27 11:21:34 peter
  1527. * aktprocdef renamed to current_procinfo.procdef
  1528. * procinfo renamed to current_procinfo
  1529. * procinfo will now be stored in current_module so it can be
  1530. cleaned up properly
  1531. * gen_main_procsym changed to create_main_proc and release_main_proc
  1532. to also generate a tprocinfo structure
  1533. * fixed unit implicit initfinal
  1534. Revision 1.57 2003/04/27 07:29:51 peter
  1535. * current_procinfo.procdef cleanup, current_procinfo.procdef is now always nil when parsing
  1536. a new procdef declaration
  1537. * aktprocsym removed
  1538. * lexlevel removed, use symtable.symtablelevel instead
  1539. * implicit init/final code uses the normal genentry/genexit
  1540. * funcret state checking updated for new funcret handling
  1541. Revision 1.56 2003/04/25 20:59:34 peter
  1542. * removed funcretn,funcretsym, function result is now in varsym
  1543. and aliases for result and function name are added using absolutesym
  1544. * vs_hidden parameter for funcret passed in parameter
  1545. * vs_hidden fixes
  1546. * writenode changed to printnode and released from extdebug
  1547. * -vp option added to generate a tree.log with the nodetree
  1548. * nicer printnode for statements, callnode
  1549. Revision 1.55 2003/04/06 21:11:23 olle
  1550. * changed newasmsymbol to newasmsymboldata for data symbols
  1551. Revision 1.54 2003/03/28 19:16:57 peter
  1552. * generic constructor working for i386
  1553. * remove fixed self register
  1554. * esi added as address register for i386
  1555. Revision 1.53 2003/02/19 22:00:14 daniel
  1556. * Code generator converted to new register notation
  1557. - Horribily outdated todo.txt removed
  1558. Revision 1.52 2003/01/08 18:43:56 daniel
  1559. * Tregister changed into a record
  1560. Revision 1.51 2002/12/14 15:02:03 carl
  1561. * maxoperands -> max_operands (for portability in rautils.pas)
  1562. * fix some range-check errors with loadconst
  1563. + add ncgadd unit to m68k
  1564. * some bugfix of a_param_reg with LOC_CREFERENCE
  1565. Revision 1.50 2002/11/25 17:43:23 peter
  1566. * splitted defbase in defutil,symutil,defcmp
  1567. * merged isconvertable and is_equal into compare_defs(_ext)
  1568. * made operator search faster by walking the list only once
  1569. Revision 1.49 2002/11/22 22:48:10 carl
  1570. * memory optimization with tconstsym (1.5%)
  1571. Revision 1.48 2002/11/18 17:31:59 peter
  1572. * pass proccalloption to ret_in_xxx and push_xxx functions
  1573. Revision 1.47 2002/11/15 16:29:31 peter
  1574. * made tasmsymbol.refs private (merged)
  1575. Revision 1.46 2002/09/03 16:26:27 daniel
  1576. * Make Tprocdef.defs protected
  1577. Revision 1.45 2002/08/25 19:25:20 peter
  1578. * sym.insert_in_data removed
  1579. * symtable.insertvardata/insertconstdata added
  1580. * removed insert_in_data call from symtable.insert, it needs to be
  1581. called separatly. This allows to deref the address calculation
  1582. * procedures now calculate the parast addresses after the procedure
  1583. directives are parsed. This fixes the cdecl parast problem
  1584. * push_addr_param has an extra argument that specifies if cdecl is used
  1585. or not
  1586. Revision 1.44 2002/08/17 09:23:41 florian
  1587. * first part of procinfo rewrite
  1588. Revision 1.43 2002/08/16 14:24:59 carl
  1589. * issameref() to test if two references are the same (then emit no opcodes)
  1590. + ret_in_reg to replace ret_in_acc
  1591. (fix some register allocation bugs at the same time)
  1592. + save_std_register now has an extra parameter which is the
  1593. usedinproc registers
  1594. Revision 1.42 2002/08/13 18:01:52 carl
  1595. * rename swatoperands to swapoperands
  1596. + m68k first compilable version (still needs a lot of testing):
  1597. assembler generator, system information , inline
  1598. assembler reader.
  1599. Revision 1.41 2002/08/12 15:08:40 carl
  1600. + stab register indexes for powerpc (moved from gdb to cpubase)
  1601. + tprocessor enumeration moved to cpuinfo
  1602. + linker in target_info is now a class
  1603. * many many updates for m68k (will soon start to compile)
  1604. - removed some ifdef or correct them for correct cpu
  1605. Revision 1.40 2002/08/11 14:32:27 peter
  1606. * renamed current_library to objectlibrary
  1607. Revision 1.39 2002/08/11 13:24:13 peter
  1608. * saving of asmsymbols in ppu supported
  1609. * asmsymbollist global is removed and moved into a new class
  1610. tasmlibrarydata that will hold the info of a .a file which
  1611. corresponds with a single module. Added librarydata to tmodule
  1612. to keep the library info stored for the module. In the future the
  1613. objectfiles will also be stored to the tasmlibrarydata class
  1614. * all getlabel/newasmsymbol and friends are moved to the new class
  1615. Revision 1.38 2002/07/20 11:57:57 florian
  1616. * types.pas renamed to defbase.pas because D6 contains a types
  1617. unit so this would conflicts if D6 programms are compiled
  1618. + Willamette/SSE2 instructions to assembler added
  1619. Revision 1.37 2002/07/11 14:41:28 florian
  1620. * start of the new generic parameter handling
  1621. Revision 1.36 2002/07/01 18:46:25 peter
  1622. * internal linker
  1623. * reorganized aasm layer
  1624. Revision 1.35 2002/05/18 13:34:17 peter
  1625. * readded missing revisions
  1626. Revision 1.34 2002/05/16 19:46:44 carl
  1627. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1628. + try to fix temp allocation (still in ifdef)
  1629. + generic constructor calls
  1630. + start of tassembler / tmodulebase class cleanup
  1631. Revision 1.31 2002/05/12 16:53:10 peter
  1632. * moved entry and exitcode to ncgutil and cgobj
  1633. * foreach gets extra argument for passing local data to the
  1634. iterator function
  1635. * -CR checks also class typecasts at runtime by changing them
  1636. into as
  1637. * fixed compiler to cycle with the -CR option
  1638. * fixed stabs with elf writer, finally the global variables can
  1639. be watched
  1640. * removed a lot of routines from cga unit and replaced them by
  1641. calls to cgobj
  1642. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1643. u32bit then the other is typecasted also to u32bit without giving
  1644. a rangecheck warning/error.
  1645. * fixed pascal calling method with reversing also the high tree in
  1646. the parast, detected by tcalcst3 test
  1647. Revision 1.30 2002/04/20 21:32:24 carl
  1648. + generic FPC_CHECKPOINTER
  1649. + first parameter offset in stack now portable
  1650. * rename some constants
  1651. + move some cpu stuff to other units
  1652. - remove unused constents
  1653. * fix stacksize for some targets
  1654. * fix generic size problems which depend now on EXTEND_SIZE constant
  1655. Revision 1.29 2002/04/15 19:02:35 carl
  1656. + target_info.size_of_pointer -> pointer_Size
  1657. Revision 1.28 2002/04/02 17:11:29 peter
  1658. * tlocation,treference update
  1659. * LOC_CONSTANT added for better constant handling
  1660. * secondadd splitted in multiple routines
  1661. * location_force_reg added for loading a location to a register
  1662. of a specified size
  1663. * secondassignment parses now first the right and then the left node
  1664. (this is compatible with Kylix). This saves a lot of push/pop especially
  1665. with string operations
  1666. * adapted some routines to use the new cg methods
  1667. Revision 1.27 2002/01/29 21:32:03 peter
  1668. * allow accessing locals in other lexlevel when the current assembler
  1669. routine doesn't have locals.
  1670. Revision 1.26 2002/01/24 18:25:50 peter
  1671. * implicit result variable generation for assembler routines
  1672. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1673. }