Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / libraries / PyMite / vm / frame.h
blob60706f9770738283c96c1fc96b9e7e1d2c41b6c9
1 /*
2 # This file is Copyright 2003, 2006, 2007, 2009, 2010 Dean Hall.
4 # This file is part of the PyMite VM.
5 # The PyMite VM is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU GENERAL PUBLIC LICENSE Version 2.
8 # The PyMite VM is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 # A copy of the GNU GENERAL PUBLIC LICENSE Version 2
12 # is seen in the file COPYING in this directory.
16 #ifndef __FRAME_H__
17 #define __FRAME_H__
20 /**
21 * \file
22 * \brief VM Frame
24 * VM frame header.
28 /**
29 * The maximum number of local variables a native function can have.
30 * This defines the length of the locals array in the native frame struct.
32 #define NATIVE_MAX_NUM_LOCALS 8
35 /**
36 * Block Type
38 * Numerical values to put in the 'b_type' field of the tPmBlockType struct.
40 typedef enum PmBlockType_e
42 /** Invalid block type */
43 B_INVALID = 0,
45 /** Loop type */
46 B_LOOP,
48 /** Try type */
49 B_TRY
50 } PmBlockType_t, *pPmBlockType_t;
53 /**
54 * Block
56 * Extra info for loops and trys (others?)
57 * Frames use linked list of blocks to handle
58 * nested loops and try-catch blocks.
60 typedef struct PmBlock_s
62 /** Obligatory obj descriptor */
63 PmObjDesc_t od;
65 /** Ptr to backup stack ptr */
66 pPmObj_t *b_sp;
68 /** Handler fxn obj */
69 uint8_t const *b_handler;
71 /** Block type */
72 PmBlockType_t b_type:8;
74 /** Next block in stack */
75 struct PmBlock_s *next;
76 } PmBlock_t,
77 *pPmBlock_t;
80 /**
81 * Frame
83 * A struct that holds the execution frame of a function, including the stack,
84 * local vars and pointer to the code object.
86 * This struct doesn't declare the stack.
87 * frame_new() is responsible for allocating the extra memory
88 * at the tail of fo_locals[] to hold both the locals and stack.
90 typedef struct PmFrame_s
92 /** Obligatory obj descriptor */
93 PmObjDesc_t od;
95 /** Ptr to previous frame obj */
96 struct PmFrame_s *fo_back;
98 /** Ptr to fxn obj */
99 pPmFunc_t fo_func;
101 /** Mem space where func's CO comes from */
102 PmMemSpace_t fo_memspace:8;
104 /** Instrxn ptr (pts into memspace) */
105 uint8_t const *fo_ip;
107 /** Linked list of blocks */
108 pPmBlock_t fo_blockstack;
110 /** Local attributes dict (non-fast locals) */
111 pPmDict_t fo_attrs;
113 /** Global attributes dict (pts to root frame's globals */
114 pPmDict_t fo_globals;
116 /** Points to next empty slot in fo_locals (1 past TOS) */
117 pPmObj_t *fo_sp;
119 /** Frame can be an import-frame that handles RETURN differently */
120 uint8_t fo_isImport:1;
122 #ifdef HAVE_CLASSES
123 /** Flag to indicate class initailzer frame; handle RETURN differently */
124 uint8_t fo_isInit:1;
125 #endif /* HAVE_CLASSES */
127 /** Array of local vars and stack (space appended at alloc) */
128 pPmObj_t fo_locals[1];
129 /* WARNING: Do not put new fields below fo_locals */
130 } PmFrame_t,
131 *pPmFrame_t;
135 * Native Frame
137 * A struct that holds the execution frame of a native function,
138 * including the args and single stack slot, and pointer to the code object.
140 * This struct doesn't need an OD because it is only used statically in the
141 * globals struct. There's only one native frame, the global one.
142 * This happens because a native function is a leaf node
143 * in the call tree (a native func can't call python funcs).
145 typedef struct PmNativeFrame_s
147 /** Object descriptor */
148 PmObjDesc_t od;
150 /** Ptr to previous frame obj */
151 struct PmFrame_s *nf_back;
153 /** Ptr to fxn obj */
154 pPmFunc_t nf_func;
156 /** Single stack slot */
157 pPmObj_t nf_stack;
159 /** Boolean to indicate if the native frame is active */
160 uint8_t nf_active;
162 /** Number of args passed to the native function */
163 uint8_t nf_numlocals;
165 /** Local vars */
166 pPmObj_t nf_locals[NATIVE_MAX_NUM_LOCALS];
167 } PmNativeFrame_t,
168 *pPmNativeFrame_t;
172 * Allocate space for a new frame, fill its fields
173 * with respect to the given function object.
174 * Return pointer to the new frame.
176 * @param pfunc ptr to Function object.
177 * @param r_pobj Return value; the new frame.
178 * @return Return status.
180 PmReturn_t frame_new(pPmObj_t pfunc, pPmObj_t *r_pobj);
182 #endif /* __FRAME_H__ */