Normalise line endings.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of SourcePawn.
6 * SourcePawn is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * SourcePawn. If not, see http://www.gnu.org/licenses/.
18 #ifndef _include_sp2_hir_h_
19 #define _include_sp2_hir_h_
22 #include "../PoolAllocator.h"
23 #include "../Opcodes.h"
38 _(NewDependentArray) \
39 /* Special LValue types */ \
50 // Forward declarations.
51 #define _(name) class H##name;
60 class HIR : public PoolObject
64 #define _(name) k##name,
71 HIR(AstNode *node, Type *type)
80 AstNode *node() const {
84 virtual bool isLValue() const {
88 virtual HOp op() const = 0;
89 virtual void accept(HIRVisitor *visitor) = 0;
92 bool is##name() const { \
93 return op() == k##name; \
95 H##name *to##name() { \
97 return reinterpret_cast<H##name *>(this); \
102 HLValue *toLValue() {
104 return reinterpret_cast<HLValue *>(this);
109 ScopedRoot<Type> type_;
112 class BytecodeEmitter;
118 virtual void visit##name(H##name *hir) { \
125 typedef PoolList<HIR *> HIRList;
127 #define DEFINE_HIR(name) \
128 virtual HOp op() const { \
131 virtual void accept(HIRVisitor *visitor) { \
132 visitor->visit##name(this); \
135 class HBoolean : public HIR
138 HBoolean(AstNode *node, Type *type, bool value)
153 class HInteger : public HIR
156 HInteger(AstNode *node, Type *type, int value)
171 class HString : public HIR
174 HString(AstNode *node, Type *type, String *string)
181 Handle<String> string() const {
186 ScopedRoot<String> string_;
189 class HImport : public HIR
192 HImport(AstNode *node, Type *type, unsigned index)
199 unsigned index() const {
207 class HArray : public HIR
210 HArray(AstNode *node, Type *type, Array *array, bool duplicate)
213 duplicate_(duplicate)
218 Handle<Array> array() const {
221 bool duplicate() const {
226 ScopedRoot<Array> array_;
230 class HConvert : public HIR
233 HConvert(AstNode *node, Type *type, Opcode op, HIR *in)
241 Opcode opcode() const {
253 class HLValue : public HIR
256 HLValue(AstNode *node, Type *type)
261 // Pushes a reference to the LValue on the stack.
262 virtual void bind(HIRVisitor *visitor, BytecodeEmitter &emitter_) = 0;
264 // Duplicates a stack reference.
265 virtual void hold(BytecodeEmitter &emitter_) = 0;
267 // Reorders the stack for a post-increment operation:
268 // REF VAL -> VAL REF VAL
269 virtual void swapAndPick(BytecodeEmitter &emitter_) = 0;
271 // Given a stack layout of the form:
275 // Emits a store to the reference.
276 virtual void store(BytecodeEmitter &emitter_) = 0;
277 virtual void load(BytecodeEmitter &emitter_) = 0;
279 // Try to make a direct reference, if semantics allow it. False if the
280 // caller must allocate a reference to the stack.
281 virtual bool ref(BytecodeEmitter &emitter_) = 0;
283 bool isLValue() const {
288 class HLocal : public HLValue
291 HLocal(AstNode *node, VariableSymbol *sym);
295 // Pushes a reference to the LValue on the stack.
296 void bind(HIRVisitor *visitor, BytecodeEmitter &emitter_);
297 void hold(BytecodeEmitter &emitter_);
298 void swapAndPick(BytecodeEmitter &emitter_);
299 void store(BytecodeEmitter &emitter_);
300 void load(BytecodeEmitter &emitter_);
301 bool ref(BytecodeEmitter &emitter_);
303 VariableSymbol *sym() const {
308 VariableSymbol *sym_;
311 class HIndex : public HLValue
314 HIndex(AstNode *node, Type *type, HIR *left, HIR *right)
315 : HLValue(node, type),
323 void bind(HIRVisitor *visitor, BytecodeEmitter &emitter_);
324 void hold(BytecodeEmitter &emitter_);
325 void swapAndPick(BytecodeEmitter &emitter_);
326 void store(BytecodeEmitter &emitter_);
327 void load(BytecodeEmitter &emitter_);
328 bool ref(BytecodeEmitter &emitter_);
342 class HField : public HLValue
345 HField(AstNode *node, HIR *left, Type *type, unsigned index)
346 : HLValue(node, type),
354 void bind(HIRVisitor *visitor, BytecodeEmitter &emitter_);
355 void hold(BytecodeEmitter &emitter_);
356 void swapAndPick(BytecodeEmitter &emitter_);
357 void store(BytecodeEmitter &emitter_);
358 void load(BytecodeEmitter &emitter_);
359 bool ref(BytecodeEmitter &emitter_);
364 unsigned index() const {
373 class HGlobal : public HLValue
376 HGlobal(AstNode *node, Symbol *sym)
377 : HLValue(node, sym->type()),
384 void bind(HIRVisitor *visitor, BytecodeEmitter &emitter_);
385 void hold(BytecodeEmitter &emitter_);
386 void swapAndPick(BytecodeEmitter &emitter_);
387 void store(BytecodeEmitter &emitter_);
388 void load(BytecodeEmitter &emitter_);
389 bool ref(BytecodeEmitter &emitter_);
391 Symbol *sym() const {
399 class HBinary : public HIR
402 HBinary(AstNode *node, Type *type, TokenKind token, HIR *left, HIR *right)
411 TokenKind token() const {
427 class HFloat : public HIR
430 HFloat(AstNode *node, Type *type, float value)
437 float value() const {
445 class HUnary : public HIR
448 HUnary(AstNode *node, Type *type, TokenKind tok, HIR *in)
459 TokenKind token() const {
468 class HStore : public HIR
471 HStore(AstNode *node, HLValue *lval, HIR *rval)
472 : HIR(node, lval->type()),
479 HStore(AstNode *node, TokenKind kind, HLValue *lval, HIR *rval)
480 : HIR(node, lval->type()),
489 HLValue *lval() const {
495 TokenKind kind() const {
505 class HPostIncDec : public HIR
508 HPostIncDec(AstNode *node, TokenKind kind, HLValue *lval, HIR *rval)
509 : HIR(node, lval->type()),
516 DEFINE_HIR(PostIncDec);
518 HLValue *lval() const {
524 TokenKind kind() const {
534 class HCall : public HIR
537 HCall(AstNode *node, Type *type, HIR *callee, HIRList *args)
546 HIR *callee() const {
549 HIRList *args() const {
558 class HToRef : public HIR
561 HToRef(AstNode *node, Type *type, HIR *in)
577 class HNewDependentArray : public HIR
580 HNewDependentArray(AstNode *node, Type *type, HIR *base, HIR *index)
587 DEFINE_HIR(NewDependentArray);
603 void EmitHIR(Zone *zone, BytecodeEmitter &emitter, HIR *hir);
607 #endif // _include_sp2_hir_h_