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