rautils.pas 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. Interface
  20. Uses
  21. strings,
  22. cobjects,
  23. globtype,systems,verbose,globals,files,
  24. symconst,symtable,aasm,cpubase,cpuasm
  25. {$ifdef NEWCG}
  26. ,cgbase
  27. {$else}
  28. ,hcodegen
  29. {$endif}
  30. ;
  31. Const
  32. RPNMax = 10; { I think you only need 4, but just to be safe }
  33. OpMax = 25;
  34. maxoperands = 3; { Maximum operands for assembler instructions }
  35. {---------------------------------------------------------------------
  36. Local Label Management
  37. ---------------------------------------------------------------------}
  38. Type
  39. { Each local label has this structure associated with it }
  40. PLocalLabel = ^TLocalLabel;
  41. TLocalLabel = object(TNamedIndexObject)
  42. Emitted : boolean;
  43. constructor Init(const n:string);
  44. function Getpasmlabel:pasmlabel;
  45. private
  46. lab : pasmlabel;
  47. end;
  48. PLocalLabelList = ^TLocalLabelList;
  49. TLocalLabelList = Object(TDictionary)
  50. procedure CheckEmitted;
  51. end;
  52. var
  53. LocalLabelList : PLocalLabelList;
  54. function CreateLocalLabel(const s: string; var hl: pasmlabel; emit:boolean):boolean;
  55. Function SearchLabel(const s: string; var hl: pasmlabel;emit:boolean): boolean;
  56. {---------------------------------------------------------------------
  57. Instruction management
  58. ---------------------------------------------------------------------}
  59. type
  60. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_REFERENCE,OPR_REGISTER);
  61. TOprRec = record
  62. case typ:TOprType of
  63. OPR_NONE : ();
  64. OPR_CONSTANT : (val:longint);
  65. OPR_SYMBOL : (symbol:PAsmSymbol;symofs:longint);
  66. OPR_REFERENCE : (ref:treference);
  67. OPR_REGISTER : (reg:tregister);
  68. end;
  69. POperand = ^TOperand;
  70. TOperand = object
  71. size : topsize;
  72. hasvar : boolean; { if the operand is loaded with a variable }
  73. opr : TOprRec;
  74. constructor init;
  75. destructor done;virtual;
  76. Procedure BuildOperand;virtual;
  77. Procedure SetSize(_size:longint;force:boolean);
  78. Procedure SetCorrectSize(opcode:tasmop);virtual;
  79. Function SetupResult:boolean;
  80. Function SetupSelf:boolean;
  81. Function SetupOldEBP:boolean;
  82. Function SetupVar(const hs:string;GetOffset : boolean): Boolean;
  83. Function SetupDirectVar(const hs:string): Boolean;
  84. Procedure InitRef;
  85. end;
  86. PInstruction = ^TInstruction;
  87. TInstruction = object
  88. opcode : tasmop;
  89. opsize : topsize;
  90. condition : tasmcond;
  91. ops : byte;
  92. operands : array[1..maxoperands] of POperand;
  93. constructor init;
  94. destructor done;virtual;
  95. Procedure InitOperands;virtual;
  96. Procedure BuildOpcode;virtual;
  97. procedure ConcatInstruction(p:PAasmoutput);virtual;
  98. Procedure SwapOperands;
  99. end;
  100. {---------------------------------------------------------------------}
  101. { Expression parser types }
  102. {---------------------------------------------------------------------}
  103. TExprOperator = record
  104. ch: char; { operator }
  105. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  106. end;
  107. String15 = String[15];
  108. {**********************************************************************}
  109. { The following operators are supported: }
  110. { '+' : addition }
  111. { '-' : subtraction }
  112. { '*' : multiplication }
  113. { '/' : modulo division }
  114. { '^' : exclusive or }
  115. { '<' : shift left }
  116. { '>' : shift right }
  117. { '&' : bitwise and }
  118. { '|' : bitwise or }
  119. { '~' : bitwise complement }
  120. { '%' : modulo division }
  121. { nnn: longint numbers }
  122. { ( and ) parenthesis }
  123. {**********************************************************************}
  124. TExprParse = Object
  125. public
  126. Constructor Init;
  127. Destructor Done;
  128. Function Evaluate(Expr: String): longint;
  129. Function Priority(_Operator: Char): Integer; virtual;
  130. private
  131. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  132. RPNTop : Integer;
  133. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  134. OpTop : Integer;
  135. Procedure RPNPush(Num: Longint);
  136. Function RPNPop: Longint;
  137. Procedure RPNCalc(token: String15; prefix: boolean);
  138. Procedure OpPush(_Operator: char; prefix: boolean);
  139. { In reality returns TExprOperaotr }
  140. Procedure OpPop(var _Operator:TExprOperator);
  141. end;
  142. { Evaluate an expression string to a longint }
  143. Function CalculateExpression(const expression: string): longint;
  144. {---------------------------------------------------------------------}
  145. { String routines }
  146. {---------------------------------------------------------------------}
  147. Function ValDecimal(const S:String):longint;
  148. Function ValOctal(const S:String):longint;
  149. Function ValBinary(const S:String):longint;
  150. Function ValHexaDecimal(const S:String):longint;
  151. Function PadZero(Var s: String; n: byte): Boolean;
  152. Function EscapeToPascal(const s:string): string;
  153. {---------------------------------------------------------------------
  154. Symbol helper routines
  155. ---------------------------------------------------------------------}
  156. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  157. Function SearchRecordType(const s:string): boolean;
  158. Function SearchIConstant(const s:string; var l:longint): boolean;
  159. {---------------------------------------------------------------------
  160. Instruction generation routines
  161. ---------------------------------------------------------------------}
  162. Procedure ConcatPasString(p : paasmoutput;s:string);
  163. Procedure ConcatDirect(p : paasmoutput;s:string);
  164. Procedure ConcatLabel(p: paasmoutput;var l : pasmlabel);
  165. Procedure ConcatConstant(p : paasmoutput;value: longint; maxvalue: longint);
  166. Procedure ConcatConstSymbol(p : paasmoutput;const sym:string;l:longint);
  167. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  168. Procedure ConcatString(p : paasmoutput;s:string);
  169. procedure ConcatAlign(p:paasmoutput;l:longint);
  170. Procedure ConcatPublic(p:paasmoutput;const s : string);
  171. Procedure ConcatLocal(p:paasmoutput;const s : string);
  172. Procedure ConcatGlobalBss(const s : string;size : longint);
  173. Procedure ConcatLocalBss(const s : string;size : longint);
  174. Implementation
  175. {*************************************************************************
  176. TExprParse
  177. *************************************************************************}
  178. Constructor TExprParse.Init;
  179. Begin
  180. end;
  181. Procedure TExprParse.RPNPush(Num : longint);
  182. { Add an operand to the top of the RPN stack }
  183. begin
  184. if RPNTop < RPNMax then
  185. begin
  186. Inc(RPNTop);
  187. RPNStack[RPNTop]:=Num;
  188. end
  189. else
  190. Message(asmr_e_ev_stack_overflow);
  191. end;
  192. Function TExprParse.RPNPop : longint;
  193. { Get the operand at the top of the RPN stack }
  194. begin
  195. if RPNTop > 0 then
  196. begin
  197. RPNPop:=RPNStack[RPNTop];
  198. Dec(RPNTop);
  199. end
  200. else
  201. Message(asmr_e_ev_stack_underflow);
  202. end;
  203. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  204. Var
  205. Temp : longint;
  206. LocalError : Integer;
  207. begin
  208. { Handle operators }
  209. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  210. Case Token[1] of
  211. '+' :
  212. Begin
  213. if not prefix then
  214. RPNPush(RPNPop + RPNPop);
  215. end;
  216. '-' :
  217. Begin
  218. if prefix then
  219. RPNPush(-(RPNPop))
  220. else
  221. RPNPush(RPNPop - RPNPop);
  222. end;
  223. '*' : RPNPush(RPNPop * RPNPop);
  224. '&' : RPNPush(RPNPop AND RPNPop);
  225. '|' : RPNPush(RPNPop OR RPNPop);
  226. '~' : RPNPush(NOT RPNPop);
  227. '<' : RPNPush(RPNPop SHL RPNPop);
  228. '>' : RPNPush(RPNPop SHR RPNPop);
  229. '%' :
  230. begin
  231. Temp:=RPNPop;
  232. if Temp <> 0 then
  233. RPNPush(RPNPop mod Temp)
  234. else
  235. Message(asmr_e_ev_zero_divide);
  236. end;
  237. '^' : RPNPush(RPNPop XOR RPNPop);
  238. '/' :
  239. begin
  240. Temp:=RPNPop;
  241. if Temp <> 0 then
  242. RPNPush(RPNPop div Temp)
  243. else
  244. Message(asmr_e_ev_zero_divide);
  245. end;
  246. end
  247. else
  248. begin
  249. { Convert String to number and add to stack }
  250. if token='-2147483648' then
  251. begin
  252. temp:=$80000000;
  253. localerror:=0;
  254. end
  255. else
  256. Val(Token, Temp, LocalError);
  257. if LocalError = 0 then
  258. RPNPush(Temp)
  259. else
  260. Message(asmr_e_ev_invalid_number);
  261. end;
  262. end;
  263. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  264. { Add an operator onto top of the stack }
  265. begin
  266. if OpTop < OpMax then
  267. begin
  268. Inc(OpTop);
  269. OpStack[OpTop].ch:=_Operator;
  270. OpStack[OpTop].is_prefix:=prefix;
  271. end
  272. else
  273. Message(asmr_e_ev_stack_overflow);
  274. end;
  275. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  276. { Get operator at the top of the stack }
  277. begin
  278. if OpTop > 0 then
  279. begin
  280. _Operator:=OpStack[OpTop];
  281. Dec(OpTop);
  282. end
  283. else
  284. Message(asmr_e_ev_stack_underflow);
  285. end;
  286. Function TExprParse.Priority(_Operator : Char) : Integer;
  287. { Return priority of operator }
  288. { The greater the priority, the higher the precedence }
  289. begin
  290. Case _Operator OF
  291. '(' :
  292. Priority:=0;
  293. '+', '-' :
  294. Priority:=1;
  295. '*', '/','%','<','>' :
  296. Priority:=2;
  297. '|','&','^','~' :
  298. Priority:=0;
  299. else
  300. Message(asmr_e_ev_invalid_op);
  301. end;
  302. end;
  303. Function TExprParse.Evaluate(Expr : String):longint;
  304. Var
  305. I : Integer;
  306. Token : String15;
  307. opr : TExprOperator;
  308. begin
  309. Evaluate:=0;
  310. { Reset stacks }
  311. OpTop :=0;
  312. RPNTop:=0;
  313. Token :='';
  314. { nothing to do ? }
  315. if Expr='' then
  316. exit;
  317. For I:=1 to Length(Expr) DO
  318. begin
  319. if Expr[I] in ['0'..'9'] then
  320. begin { Build multi-digit numbers }
  321. Token:=Token + Expr[I];
  322. if I = Length(Expr) then { Send last one to calculator }
  323. RPNCalc(Token,false);
  324. end
  325. else
  326. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  327. begin
  328. if Token <> '' then
  329. begin { Send last built number to calc. }
  330. RPNCalc(Token,false);
  331. Token:='';
  332. end;
  333. Case Expr[I] OF
  334. '(' : OpPush('(',false);
  335. ')' : begin
  336. While OpStack[OpTop].ch <> '(' DO
  337. Begin
  338. OpPop(opr);
  339. RPNCalc(opr.ch,opr.is_prefix);
  340. end;
  341. OpPop(opr); { Pop off and ignore the '(' }
  342. end;
  343. '+','-','~' : Begin
  344. { workaround for -2147483648 }
  345. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  346. begin
  347. token:='-';
  348. expr[i]:='+';
  349. end;
  350. { if start of expression then surely a prefix }
  351. { or if previous char was also an operator }
  352. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  353. OpPush(Expr[I],true)
  354. else
  355. Begin
  356. { Evaluate all higher priority operators }
  357. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  358. Begin
  359. OpPop(opr);
  360. RPNCalc(opr.ch,opr.is_prefix);
  361. end;
  362. OpPush(Expr[I],false);
  363. End;
  364. end;
  365. '*', '/',
  366. '^','|','&',
  367. '%','<','>' : begin
  368. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  369. Begin
  370. OpPop(opr);
  371. RPNCalc(opr.ch,opr.is_prefix);
  372. end;
  373. OpPush(Expr[I],false);
  374. end;
  375. end; { Case }
  376. end
  377. else
  378. Message(asmr_e_ev_invalid_op); { Handle bad input error }
  379. end;
  380. { Pop off the remaining operators }
  381. While OpTop > 0 do
  382. Begin
  383. OpPop(opr);
  384. RPNCalc(opr.ch,opr.is_prefix);
  385. end;
  386. { The result is stored on the top of the stack }
  387. Evaluate:=RPNPop;
  388. end;
  389. Destructor TExprParse.Done;
  390. Begin
  391. end;
  392. Function CalculateExpression(const expression: string): longint;
  393. var
  394. expr: TExprParse;
  395. Begin
  396. expr.Init;
  397. CalculateExpression:=expr.Evaluate(expression);
  398. expr.Done;
  399. end;
  400. {*************************************************************************}
  401. { String conversions/utils }
  402. {*************************************************************************}
  403. Function EscapeToPascal(const s:string): string;
  404. { converts a C styled string - which contains escape }
  405. { characters to a pascal style string. }
  406. var
  407. i,len : longint;
  408. hs : string;
  409. temp : string;
  410. c : char;
  411. Begin
  412. hs:='';
  413. len:=0;
  414. i:=0;
  415. while (i<length(s)) and (len<255) do
  416. begin
  417. Inc(i);
  418. if (s[i]='\') and (i<length(s)) then
  419. Begin
  420. inc(i);
  421. case s[i] of
  422. '\' :
  423. c:='\';
  424. 'b':
  425. c:=#8;
  426. 'f':
  427. c:=#12;
  428. 'n':
  429. c:=#10;
  430. 'r':
  431. c:=#13;
  432. 't':
  433. c:=#9;
  434. '"':
  435. c:='"';
  436. '0'..'7':
  437. Begin
  438. temp:=s[i];
  439. temp:=temp+s[i+1];
  440. temp:=temp+s[i+2];
  441. inc(i,2);
  442. c:=chr(ValOctal(temp));
  443. end;
  444. 'x':
  445. Begin
  446. temp:=s[i+1];
  447. temp:=temp+s[i+2];
  448. inc(i,2);
  449. c:=chr(ValHexaDecimal(temp));
  450. end;
  451. else
  452. Begin
  453. Message1(asmr_e_escape_seq_ignored,s[i]);
  454. c:=s[i];
  455. end;
  456. end;
  457. end
  458. else
  459. c:=s[i];
  460. inc(len);
  461. hs[len]:=c;
  462. end;
  463. hs[0]:=chr(len);
  464. EscapeToPascal:=hs;
  465. end;
  466. Function ValDecimal(const S:String):longint;
  467. { Converts a decimal string to longint }
  468. var
  469. vs,c : longint;
  470. Begin
  471. vs:=0;
  472. for c:=1 to length(s) do
  473. begin
  474. vs:=vs*10;
  475. if s[c] in ['0'..'9'] then
  476. inc(vs,ord(s[c])-ord('0'))
  477. else
  478. begin
  479. Message1(asmr_e_error_converting_decimal,s);
  480. ValDecimal:=0;
  481. exit;
  482. end;
  483. end;
  484. ValDecimal:=vs;
  485. end;
  486. Function ValOctal(const S:String):longint;
  487. { Converts an octal string to longint }
  488. var
  489. vs,c : longint;
  490. Begin
  491. vs:=0;
  492. for c:=1 to length(s) do
  493. begin
  494. vs:=vs shl 3;
  495. if s[c] in ['0'..'7'] then
  496. inc(vs,ord(s[c])-ord('0'))
  497. else
  498. begin
  499. Message1(asmr_e_error_converting_octal,s);
  500. ValOctal:=0;
  501. exit;
  502. end;
  503. end;
  504. ValOctal:=vs;
  505. end;
  506. Function ValBinary(const S:String):longint;
  507. { Converts a binary string to longint }
  508. var
  509. vs,c : longint;
  510. Begin
  511. vs:=0;
  512. for c:=1 to length(s) do
  513. begin
  514. vs:=vs shl 1;
  515. if s[c] in ['0'..'1'] then
  516. inc(vs,ord(s[c])-ord('0'))
  517. else
  518. begin
  519. Message1(asmr_e_error_converting_binary,s);
  520. ValBinary:=0;
  521. exit;
  522. end;
  523. end;
  524. ValBinary:=vs;
  525. end;
  526. Function ValHexadecimal(const S:String):longint;
  527. { Converts a binary string to longint }
  528. var
  529. vs,c : longint;
  530. Begin
  531. vs:=0;
  532. for c:=1 to length(s) do
  533. begin
  534. vs:=vs shl 4;
  535. case s[c] of
  536. '0'..'9' :
  537. inc(vs,ord(s[c])-ord('0'));
  538. 'A'..'F' :
  539. inc(vs,ord(s[c])-ord('A')+10);
  540. 'a'..'f' :
  541. inc(vs,ord(s[c])-ord('a')+10);
  542. else
  543. begin
  544. Message1(asmr_e_error_converting_hexadecimal,s);
  545. ValHexadecimal:=0;
  546. exit;
  547. end;
  548. end;
  549. end;
  550. ValHexadecimal:=vs;
  551. end;
  552. Function PadZero(Var s: String; n: byte): Boolean;
  553. Begin
  554. PadZero:=TRUE;
  555. { Do some error checking first }
  556. if Length(s) = n then
  557. exit
  558. else
  559. if Length(s) > n then
  560. Begin
  561. PadZero:=FALSE;
  562. delete(s,n+1,length(s));
  563. exit;
  564. end
  565. else
  566. PadZero:=TRUE;
  567. { Fill it up with the specified character }
  568. fillchar(s[length(s)+1],n-1,#0);
  569. s[0]:=chr(n);
  570. end;
  571. {****************************************************************************
  572. TOperand
  573. ****************************************************************************}
  574. constructor TOperand.init;
  575. begin
  576. size:=S_NO;
  577. hasvar:=false;
  578. FillChar(Opr,sizeof(Opr),0);
  579. end;
  580. destructor TOperand.done;
  581. begin
  582. end;
  583. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  584. begin
  585. end;
  586. Procedure TOperand.SetSize(_size:longint;force:boolean);
  587. begin
  588. if force or
  589. ((size = S_NO) and (_size<=extended_size)) then
  590. Begin
  591. case _size of
  592. 1 : size:=S_B;
  593. 2 : size:=S_W{ could be S_IS};
  594. 4 : size:=S_L{ could be S_IL or S_FS};
  595. 8 : size:=S_IQ{ could be S_D or S_FL};
  596. extended_size : size:=S_FX;
  597. end;
  598. end;
  599. end;
  600. Function TOperand.SetupResult:boolean;
  601. Begin
  602. SetupResult:=false;
  603. { replace by correct offset. }
  604. if assigned(procinfo^.returntype.def) and
  605. (procinfo^.returntype.def<>pdef(voiddef)) then
  606. begin
  607. opr.ref.offset:=procinfo^.return_offset;
  608. opr.ref.base:= procinfo^.framepointer;
  609. { always assume that the result is valid. }
  610. procinfo^.funcret_state:=vs_assigned;
  611. SetupResult:=true;
  612. end
  613. else
  614. Message(asmr_e_void_function);
  615. end;
  616. Function TOperand.SetupSelf:boolean;
  617. Begin
  618. SetupSelf:=false;
  619. if assigned(procinfo^._class) then
  620. Begin
  621. opr.typ:=OPR_REFERENCE;
  622. opr.ref.offset:=procinfo^.selfpointer_offset;
  623. opr.ref.base:=procinfo^.framepointer;
  624. opr.ref.options:=ref_selffixup;
  625. SetupSelf:=true;
  626. end
  627. else
  628. Message(asmr_e_cannot_use_SELF_outside_a_method);
  629. end;
  630. Function TOperand.SetupOldEBP:boolean;
  631. Begin
  632. SetupOldEBP:=false;
  633. if lexlevel>normal_function_level then
  634. Begin
  635. opr.typ:=OPR_REFERENCE;
  636. opr.ref.offset:=procinfo^.framepointer_offset;
  637. opr.ref.base:=procinfo^.framepointer;
  638. SetupOldEBP:=true;
  639. end
  640. else
  641. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  642. end;
  643. Function TOperand.SetupVar(const hs:string;GetOffset : boolean): Boolean;
  644. { search and sets up the correct fields in the Instr record }
  645. { for the NON-constant identifier passed to the routine. }
  646. { if not found returns FALSE. }
  647. var
  648. sym : psym;
  649. harrdef : parraydef;
  650. Begin
  651. SetupVar:=false;
  652. { are we in a routine ? }
  653. getsym(hs,false);
  654. sym:=srsym;
  655. if sym=nil then
  656. exit;
  657. case sym^.typ of
  658. varsym :
  659. begin
  660. { we always assume in asm statements that }
  661. { that the variable is valid. }
  662. pvarsym(sym)^.varstate:=vs_used;
  663. inc(pvarsym(sym)^.refs);
  664. case pvarsym(sym)^.owner^.symtabletype of
  665. objectsymtable :
  666. begin
  667. { this is not allowed, because we don't know if the self
  668. register is still free, and loading it first is also
  669. not possible, because this could break code }
  670. { Be TP/Delphi compatible in Delphi or TP modes }
  671. if (m_tp in aktmodeswitches) then
  672. begin
  673. opr.typ:=OPR_CONSTANT;
  674. opr.val:=pvarsym(sym)^.address;
  675. end
  676. { I do not agree here people using method vars should ensure
  677. that %esi is valid there }
  678. else
  679. begin
  680. opr.ref.base:=self_pointer;
  681. opr.ref.offset:=pvarsym(sym)^.address;
  682. end;
  683. hasvar:=true;
  684. SetupVar:=true;
  685. Exit;
  686. end;
  687. unitsymtable,
  688. globalsymtable,
  689. staticsymtable :
  690. opr.ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname);
  691. parasymtable :
  692. begin
  693. { if we only want the offset we don't have to care
  694. the base will be zeroed after ! }
  695. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) or
  696. { this below is wrong because there are two parast
  697. for global functions one of interface the second of
  698. implementation
  699. if (pvarsym(sym)^.owner=procinfo^.def^.parast) or }
  700. GetOffset then
  701. begin
  702. opr.ref.base:=procinfo^.framepointer;
  703. end
  704. else
  705. begin
  706. if (procinfo^.framepointer=R_ESP) and
  707. assigned(procinfo^.parent) and
  708. (lexlevel=pvarsym(sym)^.owner^.symtablelevel+1) and
  709. { same problem as above !!
  710. (procinfo^.parent^.sym^.definition^.parast=pvarsym(sym)^.owner) and }
  711. (lexlevel>normal_function_level) then
  712. opr.ref.base:=procinfo^.parent^.framepointer
  713. else
  714. message1(asmr_e_local_para_unreachable,hs);
  715. end;
  716. opr.ref.offset:=pvarsym(sym)^.address;
  717. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) then
  718. begin
  719. opr.ref.offsetfixup:=aktprocsym^.definition^.parast^.address_fixup;
  720. opr.ref.options:=ref_parafixup;
  721. end
  722. else
  723. begin
  724. opr.ref.offsetfixup:=0;
  725. opr.ref.options:=ref_none;
  726. end;
  727. end;
  728. localsymtable :
  729. begin
  730. if (vo_is_external in pvarsym(sym)^.varoptions) then
  731. opr.ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname)
  732. else
  733. begin
  734. { if we only want the offset we don't have to care
  735. the base will be zeroed after ! }
  736. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) or
  737. {if (pvarsym(sym)^.owner=procinfo^.def^.localst) or}
  738. GetOffset then
  739. opr.ref.base:=procinfo^.framepointer
  740. else
  741. begin
  742. if (procinfo^.framepointer=R_ESP) and
  743. assigned(procinfo^.parent) and
  744. (lexlevel=pvarsym(sym)^.owner^.symtablelevel+1) and
  745. {(procinfo^.parent^.sym^.definition^.localst=pvarsym(sym)^.owner) and}
  746. (lexlevel>normal_function_level) then
  747. opr.ref.base:=procinfo^.parent^.framepointer
  748. else
  749. message1(asmr_e_local_para_unreachable,hs);
  750. end;
  751. opr.ref.offset:=-(pvarsym(sym)^.address);
  752. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) then
  753. begin
  754. opr.ref.offsetfixup:=aktprocsym^.definition^.localst^.address_fixup;
  755. opr.ref.options:=ref_localfixup;
  756. end
  757. else
  758. begin
  759. opr.ref.offsetfixup:=0;
  760. opr.ref.options:=ref_none;
  761. end;
  762. end;
  763. end;
  764. end;
  765. case pvarsym(sym)^.vartype.def^.deftype of
  766. orddef,
  767. enumdef,
  768. pointerdef,
  769. floatdef :
  770. SetSize(pvarsym(sym)^.getsize,false);
  771. arraydef :
  772. begin
  773. { for arrays try to get the element size, take care of
  774. multiple indexes }
  775. harrdef:=Parraydef(PVarsym(sym)^.vartype.def);
  776. while assigned(harrdef^.elementtype.def) and
  777. (harrdef^.elementtype.def^.deftype=arraydef) do
  778. harrdef:=parraydef(harrdef^.elementtype.def);
  779. SetSize(harrdef^.elesize,false);
  780. end;
  781. end;
  782. hasvar:=true;
  783. SetupVar:=true;
  784. Exit;
  785. end;
  786. typedconstsym :
  787. begin
  788. opr.ref.symbol:=newasmsymbol(ptypedconstsym(sym)^.mangledname);
  789. case ptypedconstsym(sym)^.typedconsttype.def^.deftype of
  790. orddef,
  791. enumdef,
  792. pointerdef,
  793. floatdef :
  794. SetSize(ptypedconstsym(sym)^.getsize,false);
  795. arraydef :
  796. begin
  797. { for arrays try to get the element size, take care of
  798. multiple indexes }
  799. harrdef:=Parraydef(PTypedConstSym(sym)^.typedconsttype.def);
  800. while assigned(harrdef^.elementtype.def) and
  801. (harrdef^.elementtype.def^.deftype=arraydef) do
  802. harrdef:=parraydef(harrdef^.elementtype.def);
  803. SetSize(harrdef^.elesize,false);
  804. end;
  805. end;
  806. hasvar:=true;
  807. SetupVar:=true;
  808. Exit;
  809. end;
  810. constsym :
  811. begin
  812. if pconstsym(sym)^.consttyp in [constint,constchar,constbool] then
  813. begin
  814. opr.typ:=OPR_CONSTANT;
  815. opr.val:=pconstsym(sym)^.value;
  816. SetupVar:=true;
  817. Exit;
  818. end;
  819. end;
  820. typesym :
  821. begin
  822. if ptypesym(sym)^.restype.def^.deftype in [recorddef,objectdef] then
  823. begin
  824. opr.typ:=OPR_CONSTANT;
  825. opr.val:=0;
  826. SetupVar:=TRUE;
  827. Exit;
  828. end;
  829. end;
  830. procsym :
  831. begin
  832. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  833. Message(asmr_w_calling_overload_func);
  834. opr.typ:=OPR_SYMBOL;
  835. opr.symbol:=newasmsymbol(pprocsym(sym)^.definition^.mangledname);
  836. hasvar:=true;
  837. SetupVar:=TRUE;
  838. Exit;
  839. end;
  840. else
  841. begin
  842. Message(asmr_e_unsupported_symbol_type);
  843. exit;
  844. end;
  845. end;
  846. end;
  847. { looks for internal names of variables and routines }
  848. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  849. {$ifndef OLDDIRECTVAR}
  850. var
  851. p : pasmsymbol;
  852. begin
  853. SetupDirectVar:=false;
  854. p:=getasmsymbol(hs);
  855. if assigned(p) then
  856. begin
  857. opr.ref.symbol:=p;
  858. hasvar:=true;
  859. SetupDirectVar:=true;
  860. end;
  861. end;
  862. {$else}
  863. var
  864. p : pai_external;
  865. Begin
  866. SearchDirectVar:=false;
  867. { search in the list of internals }
  868. p:=search_assembler_symbol(internals,hs,EXT_ANY);
  869. if p=nil then
  870. p:=search_assembler_symbol(externals,hs,EXT_ANY);
  871. if p<>nil then
  872. begin
  873. instr.operands[operandnum].opr.ref.symbol:=p^.sym;
  874. case p^.exttyp of
  875. EXT_BYTE : instr.operands[operandnum].size:=S_B;
  876. EXT_WORD : instr.operands[operandnum].size:=S_W;
  877. EXT_NEAR,EXT_FAR,EXT_PROC,EXT_DWORD,EXT_CODEPTR,EXT_DATAPTR:
  878. instr.operands[operandnum].size:=S_L;
  879. EXT_QWORD : instr.operands[operandnum].size:=S_FL;
  880. EXT_TBYTE : instr.operands[operandnum].size:=S_FX;
  881. else
  882. { this is in the case where the instruction is LEA }
  883. { or something like that, in that case size is not }
  884. { important. }
  885. instr.operands[operandnum].size:=S_NO;
  886. end;
  887. instr.operands[operandnum].hasvar:=true;
  888. SearchDirectVar:=TRUE;
  889. Exit;
  890. end;
  891. end;
  892. {$endif}
  893. procedure TOperand.InitRef;
  894. {*********************************************************************}
  895. { Description: This routine first check if the opcode is of }
  896. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  897. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  898. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  899. { to point to the default segment. }
  900. {*********************************************************************}
  901. Begin
  902. case opr.typ of
  903. OPR_REFERENCE:
  904. exit;
  905. OPR_NONE: ;
  906. else
  907. Message(asmr_e_invalid_operand_type);
  908. end;
  909. opr.typ := OPR_REFERENCE;
  910. reset_reference(opr.ref);
  911. end;
  912. procedure TOperand.BuildOperand;
  913. begin
  914. abstract;
  915. end;
  916. {****************************************************************************
  917. TInstruction
  918. ****************************************************************************}
  919. constructor TInstruction.init;
  920. Begin
  921. Opcode:=A_NONE;
  922. Opsize:=S_NO;
  923. Condition:=C_NONE;
  924. Ops:=0;
  925. InitOperands;
  926. end;
  927. destructor TInstruction.done;
  928. var
  929. i : longint;
  930. Begin
  931. for i:=1 to 3 do
  932. Dispose(Operands[i],Done);
  933. end;
  934. procedure TInstruction.InitOperands;
  935. var
  936. i : longint;
  937. begin
  938. for i:=1 to 3 do
  939. New(Operands[i],init);
  940. end;
  941. Procedure TInstruction.SwapOperands;
  942. Var
  943. p : POperand;
  944. Begin
  945. case Ops of
  946. 2 :
  947. begin
  948. p:=Operands[1];
  949. Operands[1]:=Operands[2];
  950. Operands[2]:=p;
  951. end;
  952. 3 :
  953. begin
  954. p:=Operands[1];
  955. Operands[1]:=Operands[3];
  956. Operands[3]:=p;
  957. end;
  958. end;
  959. end;
  960. procedure TInstruction.ConcatInstruction(p:PAasmOutput);
  961. begin
  962. abstract;
  963. end;
  964. procedure TInstruction.BuildOpcode;
  965. begin
  966. abstract;
  967. end;
  968. {***************************************************************************
  969. TLocalLabel
  970. ***************************************************************************}
  971. constructor TLocalLabel.Init(const n:string);
  972. begin
  973. inherited InitName(n);
  974. lab:=nil;
  975. emitted:=false;
  976. end;
  977. function TLocalLabel.Getpasmlabel:pasmlabel;
  978. begin
  979. if not assigned(lab) then
  980. begin
  981. getlabel(lab);
  982. { this label is forced to be used so it's always written }
  983. inc(lab^.refs);
  984. end;
  985. Getpasmlabel:=lab;
  986. end;
  987. {***************************************************************************
  988. TLocalLabelList
  989. ***************************************************************************}
  990. procedure LocalLabelEmitted(p:PNamedIndexObject);{$ifndef FPC}far;{$endif}
  991. begin
  992. if not PLocalLabel(p)^.emitted then
  993. Message1(asmr_e_unknown_label_identifier,p^.name);
  994. end;
  995. procedure TLocalLabelList.CheckEmitted;
  996. begin
  997. ForEach({$ifndef TP}@{$endif}LocalLabelEmitted)
  998. end;
  999. function CreateLocalLabel(const s: string; var hl: pasmlabel; emit:boolean):boolean;
  1000. var
  1001. lab : PLocalLabel;
  1002. Begin
  1003. CreateLocalLabel:=true;
  1004. { Check if it already is defined }
  1005. lab:=PLocalLabel(LocalLabelList^.Search(s));
  1006. if not assigned(lab) then
  1007. begin
  1008. new(lab,init(s));
  1009. LocalLabelList^.Insert(lab);
  1010. end;
  1011. { set emitted flag and check for dup syms }
  1012. if emit then
  1013. begin
  1014. if lab^.Emitted then
  1015. begin
  1016. Message1(asmr_e_dup_local_sym,lab^.Name);
  1017. CreateLocalLabel:=false;
  1018. end;
  1019. lab^.Emitted:=true;
  1020. end;
  1021. hl:=lab^.Getpasmlabel;
  1022. end;
  1023. {****************************************************************************
  1024. Symbol table helper routines
  1025. ****************************************************************************}
  1026. Function SearchRecordType(const s:string): boolean;
  1027. Begin
  1028. SearchRecordType:=false;
  1029. { Check the constants in symtable }
  1030. getsym(s,false);
  1031. if srsym <> nil then
  1032. Begin
  1033. case srsym^.typ of
  1034. typesym :
  1035. begin
  1036. if ptypesym(srsym)^.restype.def^.deftype in [recorddef,objectdef] then
  1037. begin
  1038. SearchRecordType:=true;
  1039. exit;
  1040. end;
  1041. end;
  1042. end;
  1043. end;
  1044. end;
  1045. Function SearchIConstant(const s:string; var l:longint): boolean;
  1046. {**********************************************************************}
  1047. { Description: Searches for a CONSTANT of name s in either the local }
  1048. { symbol list, then in the global symbol list, and returns the value }
  1049. { of that constant in l. Returns TRUE if successfull, if not found, }
  1050. { or if the constant is not of correct type, then returns FALSE }
  1051. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1052. { respectively. }
  1053. {**********************************************************************}
  1054. Begin
  1055. SearchIConstant:=false;
  1056. { check for TRUE or FALSE reserved words first }
  1057. if s = 'TRUE' then
  1058. Begin
  1059. SearchIConstant:=TRUE;
  1060. l:=1;
  1061. exit;
  1062. end;
  1063. if s = 'FALSE' then
  1064. Begin
  1065. SearchIConstant:=TRUE;
  1066. l:=0;
  1067. exit;
  1068. end;
  1069. { Check the constants in symtable }
  1070. getsym(s,false);
  1071. if srsym <> nil then
  1072. Begin
  1073. case srsym^.typ of
  1074. constsym :
  1075. begin
  1076. if (pconstsym(srsym)^.consttyp in [constord,constint,constchar,constbool]) then
  1077. Begin
  1078. l:=pconstsym(srsym)^.value;
  1079. SearchIConstant:=TRUE;
  1080. exit;
  1081. end;
  1082. end;
  1083. enumsym:
  1084. Begin
  1085. l:=penumsym(srsym)^.value;
  1086. SearchIConstant:=TRUE;
  1087. exit;
  1088. end;
  1089. end;
  1090. end;
  1091. end;
  1092. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1093. { search and returns the offset and size of records/objects of the base }
  1094. { with field name setup in field. }
  1095. { returns FALSE if not found. }
  1096. { used when base is a variable or a typed constant name. }
  1097. var
  1098. st : psymtable;
  1099. harrdef : parraydef;
  1100. sym : psym;
  1101. i : longint;
  1102. base : string;
  1103. Begin
  1104. GetRecordOffsetSize:=FALSE;
  1105. Offset:=0;
  1106. Size:=0;
  1107. i:=pos('.',s);
  1108. if i=0 then
  1109. i:=255;
  1110. base:=Copy(s,1,i-1);
  1111. delete(s,1,i);
  1112. if base='SELF' then
  1113. st:=procinfo^._class^.symtable
  1114. else
  1115. begin
  1116. getsym(base,false);
  1117. sym:=srsym;
  1118. st:=nil;
  1119. { we can start with a var,type,typedconst }
  1120. case sym^.typ of
  1121. varsym :
  1122. begin
  1123. case pvarsym(sym)^.vartype.def^.deftype of
  1124. recorddef :
  1125. st:=precorddef(pvarsym(sym)^.vartype.def)^.symtable;
  1126. objectdef :
  1127. st:=pobjectdef(pvarsym(sym)^.vartype.def)^.symtable;
  1128. end;
  1129. end;
  1130. typesym :
  1131. begin
  1132. case ptypesym(sym)^.restype.def^.deftype of
  1133. recorddef :
  1134. st:=precorddef(ptypesym(sym)^.restype.def)^.symtable;
  1135. objectdef :
  1136. st:=pobjectdef(ptypesym(sym)^.restype.def)^.symtable;
  1137. end;
  1138. end;
  1139. typedconstsym :
  1140. begin
  1141. case ptypedconstsym(sym)^.typedconsttype.def^.deftype of
  1142. recorddef :
  1143. st:=precorddef(ptypedconstsym(sym)^.typedconsttype.def)^.symtable;
  1144. objectdef :
  1145. st:=pobjectdef(ptypedconstsym(sym)^.typedconsttype.def)^.symtable;
  1146. end;
  1147. end;
  1148. end;
  1149. end;
  1150. { now walk all recordsymtables }
  1151. while assigned(st) and (s<>'') do
  1152. begin
  1153. { load next field in base }
  1154. i:=pos('.',s);
  1155. if i=0 then
  1156. i:=255;
  1157. base:=Copy(s,1,i-1);
  1158. delete(s,1,i);
  1159. if st^.symtabletype=objectsymtable then
  1160. sym:=search_class_member(pobjectdef(st^.defowner),base)
  1161. else
  1162. sym:=st^.search(base);
  1163. if not assigned(sym) then
  1164. begin
  1165. GetRecordOffsetSize:=false;
  1166. exit;
  1167. end;
  1168. st:=nil;
  1169. case sym^.typ of
  1170. varsym :
  1171. begin
  1172. inc(Offset,pvarsym(sym)^.address);
  1173. Size:=PVarsym(sym)^.getsize;
  1174. case pvarsym(sym)^.vartype.def^.deftype of
  1175. arraydef :
  1176. begin
  1177. { for arrays try to get the element size, take care of
  1178. multiple indexes }
  1179. harrdef:=Parraydef(PVarsym(sym)^.vartype.def);
  1180. while assigned(harrdef^.elementtype.def) and
  1181. (harrdef^.elementtype.def^.deftype=arraydef) do
  1182. harrdef:=parraydef(harrdef^.elementtype.def);
  1183. size:=harrdef^.elesize;
  1184. end;
  1185. recorddef :
  1186. st:=precorddef(pvarsym(sym)^.vartype.def)^.symtable;
  1187. objectdef :
  1188. st:=pobjectdef(pvarsym(sym)^.vartype.def)^.symtable;
  1189. end;
  1190. end;
  1191. end;
  1192. end;
  1193. GetRecordOffsetSize:=(s='');
  1194. end;
  1195. Function SearchLabel(const s: string; var hl: pasmlabel;emit:boolean): boolean;
  1196. var
  1197. sym : psym;
  1198. hs : string;
  1199. Begin
  1200. hl:=nil;
  1201. SearchLabel:=false;
  1202. { Check for pascal labels, which are case insensetive }
  1203. hs:=upper(s);
  1204. getsym(hs,false);
  1205. sym:=srsym;
  1206. if sym=nil then
  1207. exit;
  1208. case sym^.typ of
  1209. labelsym :
  1210. begin
  1211. hl:=plabelsym(sym)^.lab;
  1212. if emit then
  1213. plabelsym(sym)^.defined:=true
  1214. else
  1215. plabelsym(sym)^.used:=true;
  1216. SearchLabel:=true;
  1217. exit;
  1218. end;
  1219. end;
  1220. end;
  1221. {*************************************************************************}
  1222. { Instruction Generation Utilities }
  1223. {*************************************************************************}
  1224. Procedure ConcatString(p : paasmoutput;s:string);
  1225. {*********************************************************************}
  1226. { PROCEDURE ConcatString(s:string); }
  1227. { Description: This routine adds the character chain pointed to in }
  1228. { s to the instruction linked list. }
  1229. {*********************************************************************}
  1230. Var
  1231. pc: PChar;
  1232. Begin
  1233. getmem(pc,length(s)+1);
  1234. p^.concat(new(pai_string,init_length_pchar(strpcopy(pc,s),length(s))));
  1235. end;
  1236. Procedure ConcatPasString(p : paasmoutput;s:string);
  1237. {*********************************************************************}
  1238. { PROCEDURE ConcatPasString(s:string); }
  1239. { Description: This routine adds the character chain pointed to in }
  1240. { s to the instruction linked list, contrary to ConcatString it }
  1241. { uses a pascal style string, so it conserves null characters. }
  1242. {*********************************************************************}
  1243. Begin
  1244. p^.concat(new(pai_string,init(s)));
  1245. end;
  1246. Procedure ConcatDirect(p : paasmoutput;s:string);
  1247. {*********************************************************************}
  1248. { PROCEDURE ConcatDirect(s:string) }
  1249. { Description: This routine output the string directly to the asm }
  1250. { output, it is only sed when writing special labels in AT&T mode, }
  1251. { and should not be used without due consideration, since it may }
  1252. { cause problems. }
  1253. {*********************************************************************}
  1254. Var
  1255. pc: PChar;
  1256. Begin
  1257. getmem(pc,length(s)+1);
  1258. p^.concat(new(pai_direct,init(strpcopy(pc,s))));
  1259. end;
  1260. Procedure ConcatConstant(p: paasmoutput; value: longint; maxvalue: longint);
  1261. {*********************************************************************}
  1262. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1263. { Description: This routine adds the value constant to the current }
  1264. { instruction linked list. }
  1265. { maxvalue -> indicates the size of the data to initialize: }
  1266. { $ff -> create a byte node. }
  1267. { $ffff -> create a word node. }
  1268. { $ffffffff -> create a dword node. }
  1269. {*********************************************************************}
  1270. Begin
  1271. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1272. Begin
  1273. Message(asmr_e_constant_out_of_bounds);
  1274. { assuming a value of maxvalue }
  1275. value:=maxvalue;
  1276. end;
  1277. if maxvalue = $ff then
  1278. p^.concat(new(pai_const,init_8bit(byte(value))))
  1279. else
  1280. if maxvalue = $ffff then
  1281. p^.concat(new(pai_const,init_16bit(word(value))))
  1282. else
  1283. if maxvalue = longint($ffffffff) then
  1284. p^.concat(new(pai_const,init_32bit(longint(value))));
  1285. end;
  1286. Procedure ConcatConstSymbol(p : paasmoutput;const sym:string;l:longint);
  1287. begin
  1288. p^.concat(new(pai_const_symbol,initname_offset(sym,l)));
  1289. end;
  1290. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  1291. {***********************************************************************}
  1292. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1293. { Description: This routine adds the value constant to the current }
  1294. { instruction linked list. }
  1295. { real_typ -> indicates the type of the real data to initialize: }
  1296. { s32real -> create a single node. }
  1297. { s64real -> create a double node. }
  1298. { s80real -> create an extended node. }
  1299. { s64bit -> create a comp node. }
  1300. { f32bit -> create a fixed node. (not used normally) }
  1301. {***********************************************************************}
  1302. Begin
  1303. case real_typ of
  1304. s32real : p^.concat(new(pai_real_32bit,init(value)));
  1305. s64real : p^.concat(new(pai_real_64bit,init(value)));
  1306. s80real : p^.concat(new(pai_real_80bit,init(value)));
  1307. s64comp : p^.concat(new(pai_comp_64bit,init(value)));
  1308. f32bit : p^.concat(new(pai_const,init_32bit(trunc(value*$10000))));
  1309. end;
  1310. end;
  1311. Procedure ConcatLabel(p: paasmoutput;var l : pasmlabel);
  1312. {*********************************************************************}
  1313. { PROCEDURE ConcatLabel }
  1314. { Description: This routine either emits a label or a labeled }
  1315. { instruction to the linked list of instructions. }
  1316. {*********************************************************************}
  1317. begin
  1318. p^.concat(new(pai_label,init(l)));
  1319. end;
  1320. procedure ConcatAlign(p:paasmoutput;l:longint);
  1321. {*********************************************************************}
  1322. { PROCEDURE ConcatPublic }
  1323. { Description: This routine emits an global definition to the }
  1324. { linked list of instructions.(used by AT&T styled asm) }
  1325. {*********************************************************************}
  1326. begin
  1327. p^.concat(new(pai_align,init(l)));
  1328. end;
  1329. procedure ConcatPublic(p:paasmoutput;const s : string);
  1330. {*********************************************************************}
  1331. { PROCEDURE ConcatPublic }
  1332. { Description: This routine emits an global definition to the }
  1333. { linked list of instructions.(used by AT&T styled asm) }
  1334. {*********************************************************************}
  1335. begin
  1336. p^.concat(new(pai_symbol,initname_global(s,0)));
  1337. end;
  1338. procedure ConcatLocal(p:paasmoutput;const s : string);
  1339. {*********************************************************************}
  1340. { PROCEDURE ConcatLocal }
  1341. { Description: This routine emits an local definition to the }
  1342. { linked list of instructions. }
  1343. {*********************************************************************}
  1344. begin
  1345. p^.concat(new(pai_symbol,initname(s,0)));
  1346. end;
  1347. Procedure ConcatGlobalBss(const s : string;size : longint);
  1348. {*********************************************************************}
  1349. { PROCEDURE ConcatGlobalBss }
  1350. { Description: This routine emits an global datablock to the }
  1351. { linked list of instructions. }
  1352. {*********************************************************************}
  1353. begin
  1354. bsssegment^.concat(new(pai_datablock,init_global(s,size)));
  1355. end;
  1356. Procedure ConcatLocalBss(const s : string;size : longint);
  1357. {*********************************************************************}
  1358. { PROCEDURE ConcatLocalBss }
  1359. { Description: This routine emits a local datablcok to the }
  1360. { linked list of instructions. }
  1361. {*********************************************************************}
  1362. begin
  1363. bsssegment^.concat(new(pai_datablock,init(s,size)));
  1364. end;
  1365. end.
  1366. {
  1367. $Log$
  1368. Revision 1.42 2000-05-11 09:56:22 pierre
  1369. * fixed several compare problems between longints and
  1370. const > $80000000 that are treated as int64 constanst
  1371. by Delphi reported by Kovacs Attila Zoltan
  1372. Revision 1.41 2000/05/08 13:23:05 peter
  1373. * fixed reference parsing
  1374. Revision 1.40 2000/04/06 07:56:04 pierre
  1375. * bug in TOperand.SetSize corrected
  1376. Revision 1.39 2000/04/04 13:48:45 pierre
  1377. + TOperand.SetCorrectSize virtual method added
  1378. to be able to change the suffix according to the instruction
  1379. (FIADD word ptr w need a s as ATT suffix
  1380. wheras FILD word ptr w need a w suffix :( )
  1381. Revision 1.38 2000/03/28 22:10:12 pierre
  1382. * Object fields are simple offsets in TP/Delphi mode
  1383. Revision 1.37 2000/03/16 15:10:25 pierre
  1384. * correct the fixups for inlined assembler code
  1385. Revision 1.36 2000/03/15 23:10:01 pierre
  1386. * fix for bug 848 (that still genrated wrong code)
  1387. + better testing for variables used in assembler
  1388. (gives an error if variable is not directly reachable !)
  1389. Revision 1.35 2000/02/09 13:23:03 peter
  1390. * log truncated
  1391. Revision 1.34 2000/01/07 01:14:37 peter
  1392. * updated copyright to 2000
  1393. Revision 1.33 1999/12/22 00:57:30 peter
  1394. * label are set to used so an error is given if used but not defined
  1395. Revision 1.32 1999/12/17 10:43:34 florian
  1396. * 761 fixed
  1397. Revision 1.31 1999/11/30 10:40:54 peter
  1398. + ttype, tsymlist
  1399. Revision 1.30 1999/11/17 17:05:04 pierre
  1400. * Notes/hints changes
  1401. Revision 1.29 1999/11/09 23:06:46 peter
  1402. * esi_offset -> selfpointer_offset to be newcg compatible
  1403. * hcogegen -> cgbase fixes for newcg
  1404. Revision 1.28 1999/11/06 14:34:26 peter
  1405. * truncated log to 20 revs
  1406. Revision 1.27 1999/09/27 23:44:58 peter
  1407. * procinfo is now a pointer
  1408. * support for result setting in sub procedure
  1409. Revision 1.26 1999/09/08 16:04:04 peter
  1410. * better support for object fields and more error checks for
  1411. field accesses which create buggy code
  1412. Revision 1.25 1999/09/04 20:29:11 florian
  1413. * bug 577 fixed
  1414. Revision 1.24 1999/08/27 14:37:50 peter
  1415. * fixed crash with typedconst array
  1416. Revision 1.23 1999/08/13 21:28:38 peter
  1417. * more reference types support
  1418. * arraydef size returns elementsize, also for multiple indexing array
  1419. Revision 1.22 1999/08/04 00:23:28 florian
  1420. * renamed i386asm and i386base to cpuasm and cpubase
  1421. Revision 1.21 1999/08/03 22:03:12 peter
  1422. * moved bitmask constants to sets
  1423. * some other type/const renamings
  1424. Revision 1.20 1999/07/29 20:54:06 peter
  1425. * write .size also
  1426. }