PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
term.c
Go to the documentation of this file.
2
14
15#include <errno.h>
16#include <stdint.h>
17
19{
20 aml_token_t op;
21 aml_token_peek(ctx, &op);
22
23 aml_object_t* value = NULL;
24 switch (op.props->type)
25 {
27 case AML_TOKEN_TYPE_NAME: // MethodInvocation is a Name
28 value = aml_expression_opcode_read(ctx);
29 break;
31 value = aml_arg_obj_read(ctx);
32 break;
34 value = aml_local_obj_read(ctx);
35 break;
36 default:
37 value = aml_object_new();
38 if (value == NULL)
39 {
40 break;
41 }
42
43 if (aml_data_object_read(ctx, value) == ERR)
44 {
45 UNREF(value);
46 value = NULL;
47 break;
48 }
49 }
50
51 if (value == NULL)
52 {
53 AML_DEBUG_ERROR(ctx, "Failed to read %s", op.props->name);
54 return NULL;
55 }
56 UNREF_DEFER(value);
57
58 aml_object_t* out = NULL;
59 if (aml_convert_source(ctx->state, value, &out, allowedTypes) == ERR)
60 {
61 return NULL;
62 }
63
64 assert(out->type & allowedTypes);
65
66 return out; // Transfer ownership
67}
68
70{
72 if (temp == NULL)
73 {
74 AML_DEBUG_ERROR(ctx, "Failed to read TermArg");
75 return ERR;
76 }
77
78 assert(temp->type == AML_INTEGER);
79
80 *out = temp->integer.value;
81 UNREF(temp);
82 return 0;
83}
84
86{
88 if (temp == NULL)
89 {
90 AML_DEBUG_ERROR(ctx, "Failed to read TermArg");
91 return NULL;
92 }
93
94 assert(temp->type == AML_STRING);
95
96 return &temp->string; // Transfer ownership
97}
98
100{
102 if (temp == NULL)
103 {
104 AML_DEBUG_ERROR(ctx, "Failed to read TermArg");
105 return NULL;
106 }
107
108 assert(temp->type == AML_BUFFER);
109
110 return &temp->buffer; // Transfer ownership
111}
112
114{
116 if (temp == NULL)
117 {
118 AML_DEBUG_ERROR(ctx, "Failed to read TermArg");
119 return NULL;
120 }
121
122 assert(temp->type == AML_PACKAGE);
123
124 return &temp->package; // Transfer ownership
125}
126
128{
129 aml_token_t token;
130 aml_token_peek(ctx, &token);
131
132 switch (token.props->type)
133 {
137 return aml_named_obj_read(ctx);
138 default:
139 AML_DEBUG_ERROR(ctx, "Invalid token type '%s'", aml_token_type_to_string(token.props->type));
140 errno = EILSEQ;
141 return ERR;
142 }
143}
144
146{
147 aml_token_t token;
148 aml_token_peek(ctx, &token);
149
150 uint64_t result = 0;
151 switch (token.props->type)
152 {
154 result = aml_statement_opcode_read(ctx);
155 break;
156 case AML_TOKEN_TYPE_NAME: // MethodInvocation is a Name
158 {
159 aml_object_t* expression = aml_expression_opcode_read(ctx);
160 if (expression == NULL)
161 {
162 AML_DEBUG_ERROR(ctx, "Failed to read ExpressionOpcode");
163 result = ERR;
164 break;
165 }
166 // Set the result of the state to the last evaluated expression, check `aml_method_invoke()` for more details.
167 // We cant just do this in `aml_expression_opcode_read()` because predicates are not supposed to be considered
168 // for implicit return.
169 // aml_state_result_set(ctx->state, result);
170 UNREF(expression);
171 result = 0;
172 break;
173 }
174 default:
175 result = aml_object_read(ctx);
176 break;
177 }
178
179 if (result == ERR)
180 {
181 AML_DEBUG_ERROR(ctx, "Failed to read TermObj '%s' (0x%x)", token.props->name, token.num);
182 return ERR;
183 }
184
185 return 0;
186}
187
189 aml_term_list_ctx_t* parentCtx)
190{
191 if (state == NULL || scope == NULL || start == NULL || end == NULL || start > end)
192 {
193 errno = EINVAL;
194 return ERR;
195 }
196
197 aml_term_list_ctx_t ctx = {
198 .state = state,
199 .scope = scope,
200 .start = start,
201 .end = end,
202 .current = start,
203 .stopReason = AML_STOP_REASON_NONE,
204 };
205
206 while (ctx.end > ctx.current && ctx.stopReason == AML_STOP_REASON_NONE)
207 {
208 // End of buffer not reached => byte is not nothing => must be a termobj.
209 if (aml_term_obj_read(&ctx) == ERR)
210 {
211 return ERR;
212 }
213 }
214
215 if (parentCtx != NULL)
216 {
217 parentCtx->stopReason = ctx.stopReason;
218 }
219 return 0;
220}
#define assert(expression)
Definition assert.h:29
static void start()
Definition main.c:542
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:545
#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:18
uint64_t aml_term_arg_read_integer(aml_term_list_ctx_t *ctx, aml_uint_t *out)
Wrapper around aml_term_arg_read() that converts the result to an integer.
Definition term.c:69
aml_package_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:113
uint64_t aml_object_read(aml_term_list_ctx_t *ctx)
Reads an Object structure from the AML byte stream.
Definition term.c:127
uint64_t aml_term_obj_read(aml_term_list_ctx_t *ctx)
Reads a TermObj structure from the AML byte stream.
Definition term.c:145
aml_stioring_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:85
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:188
aml_buffer_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:99
@ AML_STOP_REASON_NONE
No stop reason, continue execution or has reached the end of the TermList.
Definition term.h:25
uint64_t aml_uint_t
AML Integer type.
Definition integer.h:20
aml_type_t
ACPI data types.
Definition object.h:59
aml_object_t * aml_object_new(void)
Allocate a new ACPI object.
Definition object.c:51
@ AML_PACKAGE
Definition object.h:81
@ AML_STRING
Definition object.h:85
@ AML_INTEGER
Definition object.h:67
@ AML_BUFFER
Definition object.h:61
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 UNREF_DEFER(ptr)
RAII-style cleanup for scoped references.
Definition ref.h:122
#define UNREF(ptr)
Decrement reference count.
Definition ref.h:109
#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:25
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
Data for a buffer object.
Definition object.h:213
aml_uint_t value
Definition object.h:288
ACPI object.
Definition object.h:447
aml_stioring_t string
Definition object.h:467
aml_package_t package
Definition object.h:464
aml_integer_t integer
Definition object.h:458
aml_buffer_t buffer
Definition object.h:453
Data for a package object.
Definition object.h:359
AML State.
Definition state.h:25
Data for a string object.
Definition object.h:390
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