PatchworkOS
Loading...
Searching...
No Matches
term.c
Go to the documentation of this file.
2
14#include <kernel/sched/sched.h>
15#include <kernel/sched/thread.h>
16
17#include <errno.h>
18#include <stdint.h>
19
21{
22 aml_token_t op;
23 aml_token_peek(ctx, &op);
24
25#ifdef TESTING
26 aml_tests_perf_start(&op);
27#endif
28
29 aml_object_t* value = NULL;
30 switch (op.props->type)
31 {
33 case AML_TOKEN_TYPE_NAME: // MethodInvocation is a Name
34 value = aml_expression_opcode_read(ctx);
35 if (value != NULL)
36 {
38 }
39 break;
41 value = aml_arg_obj_read(ctx);
42 break;
44 value = aml_local_obj_read(ctx);
45 break;
46 default:
47 value = aml_object_new();
48 if (value == NULL)
49 {
50 break;
51 }
52
53 if (aml_data_object_read(ctx, value) == ERR)
54 {
55 DEREF(value);
56 value = NULL;
57 break;
58 }
59 }
60
61 if (value == NULL)
62 {
63#ifdef TESTING
64 aml_tests_perf_end();
65#endif
66 AML_DEBUG_ERROR(ctx, "Failed to read %s", op.props->name);
67 return NULL;
68 }
69 DEREF_DEFER(value);
70
71 aml_object_t* out = NULL;
72 if (aml_convert_source(ctx->state, value, &out, allowedTypes) == ERR)
73 {
74#ifdef TESTING
75 aml_tests_perf_end();
76#endif
77 return NULL;
78 }
79
80#ifdef TESTING
81 aml_tests_perf_end();
82#endif
83
84 assert(out->type & allowedTypes);
85
86 return out; // Transfer ownership
87}
88
90{
92 if (temp == NULL)
93 {
94 AML_DEBUG_ERROR(ctx, "Failed to read TermArg");
95 return ERR;
96 }
97
98 assert(temp->type == AML_INTEGER);
99
100 *out = temp->integer.value;
101 DEREF(temp);
102 return 0;
103}
104
106{
108 if (temp == NULL)
109 {
110 AML_DEBUG_ERROR(ctx, "Failed to read TermArg");
111 return NULL;
112 }
113
114 assert(temp->type == AML_STRING);
115
116 return &temp->string; // Transfer ownership
117}
118
120{
122 if (temp == NULL)
123 {
124 AML_DEBUG_ERROR(ctx, "Failed to read TermArg");
125 return NULL;
126 }
127
128 assert(temp->type == AML_BUFFER);
129
130 return &temp->buffer; // Transfer ownership
131}
132
134{
136 if (temp == NULL)
137 {
138 AML_DEBUG_ERROR(ctx, "Failed to read TermArg");
139 return NULL;
140 }
141
142 assert(temp->type == AML_PACKAGE);
143
144 return &temp->package; // Transfer ownership
145}
146
148{
149 aml_token_t token;
150 aml_token_peek(ctx, &token);
151
152 switch (token.props->type)
153 {
157 return aml_named_obj_read(ctx);
158 default:
159 AML_DEBUG_ERROR(ctx, "Invalid token type '%s'", aml_token_type_to_string(token.props->type));
160 errno = EILSEQ;
161 return ERR;
162 }
163}
164
166{
167 aml_token_t token;
168 aml_token_peek(ctx, &token);
169
170#ifdef TESTING
171 aml_tests_perf_start(&token);
172#endif
173
174 uint64_t result = 0;
175 switch (token.props->type)
176 {
178 result = aml_statement_opcode_read(ctx);
179 break;
180 case AML_TOKEN_TYPE_NAME: // MethodInvocation is a Name
182 {
183 aml_object_t* expression = aml_expression_opcode_read(ctx);
184 if (expression == NULL)
185 {
186 AML_DEBUG_ERROR(ctx, "Failed to read ExpressionOpcode");
187 result = ERR;
188 break;
189 }
190 // Set the result of the state to the last evaluated expression, check `aml_method_evaluate()` for more details.
191 // We cant just do this in `aml_expression_opcode_read()` because predicates are not supposed to be considered
192 // for implicit return.
193 // aml_state_result_set(ctx->state, result);
194 DEREF(expression);
195 result = 0;
196 break;
197 }
198 default:
199 result = aml_object_read(ctx);
200 break;
201 }
202
203#ifdef TESTING
204 aml_tests_perf_end();
205#endif
206
207 if (result == ERR)
208 {
209 AML_DEBUG_ERROR(ctx, "Failed to read TermObj '%s' (0x%x)", token.props->name, token.num);
210 return ERR;
211 }
212
213 return 0;
214}
215
217 aml_term_list_ctx_t* parentCtx)
218{
219 if (state == NULL || scope == NULL || start == NULL || end == NULL || start > end)
220 {
221 errno = EINVAL;
222 return ERR;
223 }
224
225 aml_term_list_ctx_t ctx = {
226 .state = state,
227 .scope = scope,
228 .start = start,
229 .end = end,
230 .current = start,
231 .stopReason = AML_STOP_REASON_NONE,
232 };
233
234 while (ctx.end > ctx.current && ctx.stopReason == AML_STOP_REASON_NONE)
235 {
236 // End of buffer not reached => byte is not nothing => must be a termobj.
237 if (aml_term_obj_read(&ctx) == ERR)
238 {
239 return ERR;
240 }
241 }
242
243 if (parentCtx != NULL)
244 {
245 parentCtx->stopReason = ctx.stopReason;
246 }
247 return 0;
248}
#define assert(expression)
Definition assert.h:29
uint64_t aml_convert_source(aml_state_t *state, aml_object_t *src, aml_object_t **dest, aml_type_t allowedTypes)
Performs a "Implicit Source Operand Conversion" acording to the rules in section 19....
Definition convert.c:541
#define AML_DEBUG_ERROR(ctx, format,...)
Macro to simplify calling aml_debug_error() with the current function name.
Definition debug.h:30
aml_object_t * aml_arg_obj_read(aml_term_list_ctx_t *ctx)
Reads a ArgObj structure from the AML byte stream.
Definition arg.c:8
uint64_t aml_data_object_read(aml_term_list_ctx_t *ctx, aml_object_t *out)
Read a DataObject structure from the AML stream.
Definition data.c:501
aml_object_t * aml_expression_opcode_read(aml_term_list_ctx_t *ctx)
Reads an ExpressionOpcode structure from the AML byte stream.
aml_object_t * aml_local_obj_read(aml_term_list_ctx_t *ctx)
Reads a LocalObj structure from the AML byte stream.
Definition local.c:8
uint64_t aml_named_obj_read(aml_term_list_ctx_t *ctx)
Reads a NamedObj structure from the AML byte stream.
Definition named.c:1296
uint64_t aml_namespace_modifier_obj_read(aml_term_list_ctx_t *ctx)
Reads a NameSpaceModifierObj structure from the AML byte stream.
uint64_t aml_statement_opcode_read(aml_term_list_ctx_t *ctx)
Reads an StatementOpcode structure from the AML byte stream.
Definition statement.c:285
aml_object_t * aml_term_arg_read(aml_term_list_ctx_t *ctx, aml_type_t allowedTypes)
Reads an TermArg structure from the AML byte stream.
Definition term.c:20
uint64_t aml_term_arg_read_integer(aml_term_list_ctx_t *ctx, aml_integer_t *out)
Wrapper around aml_term_arg_read() that converts the result to an integer.
Definition term.c:89
uint64_t aml_object_read(aml_term_list_ctx_t *ctx)
Reads an Object structure from the AML byte stream.
Definition term.c:147
uint64_t aml_term_obj_read(aml_term_list_ctx_t *ctx)
Reads a TermObj structure from the AML byte stream.
Definition term.c:165
aml_package_obj_t * aml_term_arg_read_package(aml_term_list_ctx_t *ctx)
Wrapper around aml_term_arg_read() that converts the result to a package.
Definition term.c:133
uint64_t aml_term_list_read(aml_state_t *state, aml_object_t *scope, const uint8_t *start, const uint8_t *end, aml_term_list_ctx_t *parentCtx)
Reads a TermList structure from the AML byte stream.
Definition term.c:216
aml_buffer_obj_t * aml_term_arg_read_buffer(aml_term_list_ctx_t *ctx)
Wrapper around aml_term_arg_read() that converts the result to a buffer.
Definition term.c:119
aml_string_obj_t * aml_term_arg_read_string(aml_term_list_ctx_t *ctx)
Wrapper around aml_term_arg_read() that converts the result to a string.
Definition term.c:105
@ AML_STOP_REASON_NONE
No stop reason, continue execution or has reached the end of the TermList.
Definition term.h:25
uint64_t aml_integer_t
AML Integer type.
Definition integer.h:20
void aml_object_exception_check(aml_object_t *object, aml_state_t *state)
Check if a object has the AML_OBJECT_EXCEPTION_ON_USE flag set and raise an exception if it is.
Definition object.c:461
aml_type_t
ACPI data types.
Definition object.h:57
aml_object_t * aml_object_new(void)
Allocate a new ACPI object.
Definition object.c:54
@ AML_PACKAGE
Definition object.h:79
@ AML_STRING
Definition object.h:83
@ AML_INTEGER
Definition object.h:65
@ AML_BUFFER
Definition object.h:59
const char * aml_token_type_to_string(aml_token_type_t type)
Convert a token type to a string.
Definition token.c:139
static void aml_token_peek(aml_term_list_ctx_t *ctx, aml_token_t *out)
Attempt to read a token from the AML stream, without advancing the instruction pointer.
Definition token.h:301
@ AML_TOKEN_TYPE_NAMED
Is a NamedObj (section 20.2.5.2).
Definition token.h:228
@ AML_TOKEN_TYPE_NAMESPACE_MODIFIER
Is a Namespace Modifier Object (section 20.2.5.1).
Definition token.h:227
@ AML_TOKEN_TYPE_EXPRESSION
Is an Expression Opcode (section 20.2.5.4).
Definition token.h:230
@ AML_TOKEN_TYPE_NAME
Is a Name Object (section 20.2.2).
Definition token.h:226
@ AML_TOKEN_TYPE_STATEMENT
Is a Statement Opcode (section 20.2.5.3).
Definition token.h:229
@ AML_TOKEN_TYPE_ARG
Is an Arg Object (section 20.2.6.1).
Definition token.h:231
@ AML_TOKEN_TYPE_LOCAL
Is a Local Object (section 20.2.6.2).
Definition token.h:232
#define DEREF_DEFER(ptr)
RAII-style cleanup for scoped references.
Definition ref.h:54
#define DEREF(ptr)
Decrement reference count.
Definition ref.h:80
#define EINVAL
Invalid argument.
Definition errno.h:142
#define errno
Error number variable.
Definition errno.h:27
#define EILSEQ
Illegal byte sequence.
Definition errno.h:447
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
static void start()
Definition main.c:542
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
Data for a buffer object.
Definition object.h:211
aml_integer_t value
Definition object.h:266
ACPI object.
Definition object.h:425
aml_buffer_obj_t buffer
Definition object.h:431
aml_integer_obj_t integer
Definition object.h:435
aml_package_obj_t package
Definition object.h:441
aml_string_obj_t string
Definition object.h:444
Data for a package object.
Definition object.h:337
AML State.
Definition state.h:25
Data for a string object.
Definition object.h:368
Context for reading a TermList.
Definition term.h:37
aml_state_t * state
Definition term.h:38
const uint8_t * end
Definition term.h:41
const uint8_t * current
Definition term.h:42
aml_stop_reason_t stopReason
Definition term.h:43
const char * name
Definition token.h:243
aml_token_type_t type
Definition token.h:245
Token.
Definition token.h:253
const aml_token_props_t * props
Definition token.h:256
aml_token_num_t num
Definition token.h:254