datex_core/decompiler/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
mod constants;


use std::borrow::Cow;
use std::cell::Cell;
use std::collections::HashMap;
use std::collections::HashSet;
use std::vec;

use constants::tokens::get_code_token;
use crate::datex_values::SlotIdentifier;
use crate::parser::header::has_dxb_magic_number;
use crate::utils::color::AnsiCodes;
use crate::utils::color::Color;
use crate::utils::logger::LoggerContext;
use lazy_static::lazy_static;
use regex::Regex;
use crate::datex_values::Value;

use crate::global::binary_codes::BinaryCode;
use crate::parser::header;
use crate::parser::body;

use self::constants::tokens::get_code_color;

lazy_static!{
	static ref NEW_LINE:Regex = Regex::new(r"\r\n").unwrap();
	static ref LAST_LINE:Regex = Regex::new(r"   (.)$").unwrap();
	static ref INDENT:String ="\r\n   ".to_string();

}


/**
 * Converts DXB (with or without header) to DATEX Script
 */
pub fn decompile(ctx: &LoggerContext, dxb:&[u8], formatted:bool, colorized:bool, resolve_slots:bool) -> String {

	let mut body = dxb;

	// header?
	if has_dxb_magic_number(dxb) {
		let (_header, _body) = header::parse_dxb_header(dxb);
		body = _body;
	}

	return decompile_body(ctx, body, formatted, colorized, resolve_slots);
}


pub fn decompile_body(ctx: &LoggerContext, dxb_body:&[u8], formatted:bool, colorized:bool, resolve_slots:bool) -> String {
	
	let mut initial_state = DecompilerGlobalState {
		ctx,
		dxb_body,
		index: &Cell::from(0),
		is_end_instruction: &Cell::from(false),

		formatted, 
		colorized,
		resolve_slots,

		current_label: 0,
		labels: HashMap::new(),
		inserted_labels: HashSet::new(),
		variables: HashMap::new(),
	};

	return decompile_loop(&mut initial_state);
}

fn int_to_label(n: i32) -> String {
	// Convert the integer to a base-26 number, with 'a' being the 0th digit
	let mut label = String::new();
	let mut n = n;

	while n > 0 {
		// Get the remainder when n is divided by 26
		let r = n % 26;

		// Add the corresponding character (a-z) to the label
		label.insert(0, (r as u8 + b'a') as char);

		// Divide n by 26 and continue
		n /= 26;
	}

	// If the label is empty, it means the input integer was 0, so return "a"
	if label.is_empty() {
		label = "a".to_string();
	}

	label
}


struct DecompilerGlobalState<'a> {
	// ctx
	ctx: &'a LoggerContext,

	// dxb
	dxb_body:&'a [u8], 
	index: &'a Cell<usize>,
	is_end_instruction: &'a Cell<bool>,

	// options
	formatted: bool,
	colorized: bool,
	resolve_slots: bool, // display slots with generated variable names

	// state
	current_label: i32,
	labels: HashMap<usize, String>,
	inserted_labels: HashSet<usize>,
	variables: HashMap<u16, String>
}

impl DecompilerGlobalState<'_> {
	fn get_insert_label(&mut self, index:usize) -> String {
		// existing
		if self.labels.contains_key(&index) {
			return self.labels.get(&index).or(Some(&"?invalid?".to_string())).unwrap().to_string();
		}
		// new
		else {
			let name = self.current_label.to_string();
			self.current_label += 1;
			self.labels.insert(index, name.clone());
			return name;
		}
	}


	// returns variable name and variable type if initialization
	fn get_variable_name(&mut self, slot:&SlotIdentifier) -> (String, String) {
		// return slot name
		if slot.is_reserved() || slot.is_object_slot() || !self.resolve_slots {
			return (slot.to_string(), "".to_string());
		}
		// existing variable
		if self.variables.contains_key(&slot.index) {
			return (self.variables.get(&slot.index).or(Some(&"?invalid?".to_string())).unwrap().to_string(), "".to_string())
		}
		// init variable
		else {
			let name = int_to_label(self.current_label);
			self.current_label += 1;
			self.variables.insert(slot.index, name.clone());
			return (name, "var".to_string());
		}
	}
}



fn decompile_loop(state: &mut DecompilerGlobalState) -> String {
	let mut out:String = "".to_string();

	// let logger = Logger::new_for_development(&state.ctx, "Decompiler");

	let instruction_iterator = body::iterate_instructions(state.dxb_body, state.index, state.is_end_instruction);

	// flags - initial values
	let mut open_element_comma = false;
	let mut last_was_value = false;
	let mut last_was_property_access = false;
	let mut is_indexed_element = false;

	let mut next_assign_action: Option<u8> = None;
	let mut connective_size_stack: Vec<usize> = vec![];
	let mut connective_type_stack:Vec<BinaryCode> = vec![];

	for instruction in instruction_iterator {
		
		let code = instruction.code;

		// is element instruction (in arrays, tuples, ..)
		let is_new_element =  match code {
			BinaryCode::ELEMENT => true,
			BinaryCode::ELEMENT_WITH_KEY => true,
			BinaryCode::ELEMENT_WITH_DYNAMIC_KEY => true,
			BinaryCode::ELEMENT_WITH_INT_KEY => true,
			BinaryCode::INTERNAL_OBJECT_SLOT => true,
			_ => false
		};

		// closing array, object, ...
		let is_closing = match code {
			BinaryCode::CLOSE_AND_STORE => true,
			BinaryCode::SUBSCOPE_END => true,
			BinaryCode::ARRAY_END => true,
			BinaryCode::OBJECT_END => true,
			BinaryCode::TUPLE_END => true,
			_ => false
		};

		// binary codes around which there is no space required
		let no_space_around = match code {
			BinaryCode::CLOSE_AND_STORE => true,
			BinaryCode::CHILD_ACTION => true,
			BinaryCode::CHILD_GET => true,
			BinaryCode::CHILD_GET_REF => true,
			BinaryCode::CHILD_SET => true,
			BinaryCode::CHILD_SET_REFERENCE => true,
			_ => false
		};

		let add_comma = open_element_comma && is_new_element; // comma still has to be closed, possible when the next code starts a new element

		// space between
		if state.formatted && last_was_value && !add_comma && !no_space_around && !is_indexed_element && !is_closing {
			out += " ";
		}
		last_was_value = true;
		is_indexed_element = false; // reset

		// check flags:
		// comma
		if add_comma {
			open_element_comma = false;
			if state.colorized {out += &Color::DEFAULT.as_ansi_rgb();} // light grey color for property keys
			out += if state.formatted {",\r\n"} else {","}
		}

		
		let has_slot = instruction.slot.is_some();
		let slot = instruction.slot.unwrap_or_default();

		let has_primitive_value = instruction.primitive_value.is_some();
		let primitive_value = instruction.primitive_value.unwrap_or_default();
		let mut custom_primitive_color = false;

		// slot to variable mapping
		let variable_info = if has_slot { state.get_variable_name(&slot)} else {("".to_string(),"".to_string())};
		let variable_name = variable_info.0;
		let variable_prefix = variable_info.1;

		// coloring
		if state.colorized {
			// handle property key strings
			if last_was_property_access && (code == BinaryCode::TEXT || code == BinaryCode::SHORT_TEXT) && primitive_value.can_omit_quotes() {
				out += &get_code_color(&BinaryCode::ELEMENT_WITH_KEY).as_ansi_rgb(); // light grey color for property keys
			}
			// normal coloring
			else if code != BinaryCode::CLOSE_AND_STORE { // color is added later for CLOSE_AND_STORE
				let color = get_code_color(&code);
				if color == Color::_UNKNOWN && has_primitive_value {
					custom_primitive_color = true;
				}
				else {
					out += &color.as_ansi_rgb();
				}
			}
		}


		// token to string

	
		match code {
			// slot based
			BinaryCode::INTERNAL_VAR 			    => out += &format!("{variable_name}"),
			// only for backwards compatibility
			BinaryCode::LABEL 			    => out += &format!("$_{variable_name}"),
			BinaryCode::SET_INTERNAL_VAR => {
				if state.colorized {out += &Color::RESERVED.as_ansi_rgb();}
				out += &variable_prefix;
				if variable_prefix.len()!=0 {out += " "};
				if state.colorized {out += &get_code_color(&code).as_ansi_rgb();}
				out += &variable_name;
				if state.colorized {out += &Color::DEFAULT.as_ansi_rgb();}
				out += " = ";
			},
			BinaryCode::INIT_INTERNAL_VAR => {
				if state.colorized {out += &Color::RESERVED.as_ansi_rgb();}
				out += &variable_prefix;
				if variable_prefix.len()!=0 {out += " "};
				if state.colorized {out += &get_code_color(&code).as_ansi_rgb();}
				out += &variable_name;
				if state.colorized {out += &Color::DEFAULT.as_ansi_rgb();}
				out += " := ";
			},
			BinaryCode::SET_INTERNAL_VAR_REFERENCE 	=> {
				if state.colorized {out += &Color::RESERVED.as_ansi_rgb();}
				out += &variable_prefix;
				if variable_prefix.len()!=0 {out += " "};
				if state.colorized {out += &get_code_color(&code).as_ansi_rgb();}
				out += &variable_name;
				if state.colorized {out += &Color::DEFAULT.as_ansi_rgb();}
				out += " $= ";
			},

			// pointer
			BinaryCode::INIT_POINTER => {
				if state.colorized {out += &Color::RESERVED.as_ansi_rgb();}
				out += &instruction.value.unwrap().to_string();
				if state.colorized {out += &Color::DEFAULT.as_ansi_rgb();}
				out += " := ";
			},
			BinaryCode::SET_POINTER => {
				if state.colorized {out += &Color::RESERVED.as_ansi_rgb();}
				out += &instruction.value.unwrap().to_string();
				if state.colorized {out += &Color::DEFAULT.as_ansi_rgb();}
				out += " =";
			},

			// assign actions (override primitive value default behaviour)
			BinaryCode::CHILD_ACTION => out += &get_code_token(&BinaryCode::CHILD_ACTION, state.formatted),
		
			// special primitive value formatting
			BinaryCode::ELEMENT_WITH_KEY            => out += &format!("{}:", primitive_value.to_key_string()),
			BinaryCode::ELEMENT_WITH_INT_KEY        => out += &format!("{}:", primitive_value.to_key_string()),             
			BinaryCode::INTERNAL_OBJECT_SLOT        => out += &format!("{}:", SlotIdentifier::new(primitive_value.get_as_unsigned_integer() as u16)),        

			// resolve relativ path, path is stored in text primitive
			BinaryCode::RESOLVE_RELATIVE_PATH        => out += primitive_value.get_as_text(),

			// indexed element without key
			BinaryCode::ELEMENT	=> {
				is_indexed_element = true; // don't add whitespace in front of next value for correct indentation
			},

			// logical connectives
			BinaryCode::CONJUNCTION	=> {
				out += "(";
				connective_type_stack.push(BinaryCode::CONJUNCTION);
				connective_size_stack.push(primitive_value.get_as_unsigned_integer());
			},
			BinaryCode::DISJUNCTION	=> {
				out += "(";
				connective_type_stack.push(BinaryCode::DISJUNCTION);
				connective_size_stack.push(primitive_value.get_as_unsigned_integer());
			},

			// jmp
			BinaryCode::JMP	=> {
				let label = state.get_insert_label(primitive_value.get_as_unsigned_integer());
				out += &format!("jmp {}", label);
				if state.colorized {out += &Color::DEFAULT.as_ansi_rgb();}
				out += ";";
			},
			BinaryCode::JTR	=> {
				let label = state.get_insert_label(primitive_value.get_as_unsigned_integer());
				out += &format!("jtr {}", label)
			},
			BinaryCode::JFA	=> {
				let label = state.get_insert_label(primitive_value.get_as_unsigned_integer());
				out += &format!("jfa {}", label)
			},

			// scope
			BinaryCode::SCOPE_BLOCK_START => {
				let scope = &mut decompile_body(&state.ctx, &primitive_value.get_as_buffer(), state.formatted, state.colorized, state.resolve_slots);
				
				// multi line scope TODO: check multiline (problem cannot check scope.contains(";"), because escape codes can contain ";")
				if true {
					*scope += ")";
					out += "(";
					// ----------
					if state.formatted {
						out += &INDENT;
						out += &NEW_LINE.replace_all(   // add spaces to every new line
							&scope, 
							&INDENT.to_string()
						);
					};
					// ----------
				}
				else {
					scope.pop(); // remove last character (;)
					scope.pop();
					scope.pop();
					out += scope;
				}
			},

			BinaryCode::CLOSE_AND_STORE => {
				// newline+spaces before, remove, add ';' and add newline afterwards
				let empty: &[_] = &['\r', '\n', ' '];
				out = out.trim_end_matches(empty).to_string();
				if state.colorized {out += &get_code_color(&code).as_ansi_rgb()}
				out += &get_code_token(&code, state.formatted);
				// newline if not end of file
				if state.formatted && state.index.get() < state.dxb_body.len() {
					out += "\r\n";
				}
			}

			_ => {
				// primitive value default
				if has_primitive_value {
					if last_was_property_access {out += &primitive_value.to_key_string()}
					else if custom_primitive_color {out += &primitive_value.to_string_colorized()}
					else {out += &Value::to_string(&primitive_value)}
				}
				// complex value
				else if instruction.value.is_some() {
					out += &instruction.value.unwrap().to_string();
				}
				// fallback if no string representation possible [hex code]
				else {
					out += &get_code_token(&code, state.formatted)
				}
			}
		}


		// enter new subscope - continue at index?
		if instruction.subscope_continue {
			let inner = Cow::from(decompile_loop(state));
			let is_empty = inner.len() == 0;
			let newline_count = inner.chars().filter(|c| *c == '\n').count();

			// only if content inside brackets, and multiple lines
			if state.formatted && !is_empty && newline_count>0 {
				out += &INDENT;
				out += &NEW_LINE.replace_all(   // add spaces to every new line
					&inner, 
					&INDENT.to_string()
				);
				out += "\r\n";
			}

			// no content inside brackets or single line
			else {
				out += &NEW_LINE.replace_all(&inner, "").trim_end(); // remove remaining new line + spaces in last line
			}
		}


		// after value insert : finish assign action?
		if next_assign_action.is_some() {
			// coloring
			if state.colorized {
				out += &Color::DEFAULT.as_ansi_rgb();
			}
			// +=, -=, ...
			out += " ";
			let assign_type = next_assign_action.unwrap();

			match assign_type {
				1 => out += "$",
				2 => out += "",
				_ => out += &get_code_token(&BinaryCode::try_from(assign_type).expect("enum conversion error"), false)
			}
			out += "= ";
			last_was_value = false; // no additional space afterwards
			next_assign_action = None; // reset
		}

		// check for new assign actions
		match code {
			BinaryCode::CHILD_ACTION => next_assign_action = Some(primitive_value.get_as_integer() as u8),
			BinaryCode::CHILD_SET_REFERENCE => next_assign_action = Some(1),
			BinaryCode::CHILD_SET => next_assign_action = Some(2),
			_ => ()
		}


		// reset flags
		last_was_property_access = false;

		// set flags
		if is_new_element {open_element_comma = true} // remember to add comma after element

		// ) ] } end
		if is_closing {
			// open_element_comma = false; // no more commas required 
			last_was_value = false; // no space afterwards
		} 

		if no_space_around {
			last_was_value = false // no space afterwards
		}

		match code {
			BinaryCode::SET_INTERNAL_VAR => {last_was_value = false}, // no space afterwards
			BinaryCode::SET_INTERNAL_VAR_REFERENCE => {last_was_value = false}, // no space afterwards
			BinaryCode::INIT_INTERNAL_VAR => {last_was_value = false}, // no space afterwards
			BinaryCode::INIT_POINTER => {last_was_value = false}, // no space afterwards
			BinaryCode::NOT => {last_was_value = false}, // no space afterwards
			BinaryCode::CHILD_GET => {last_was_property_access = true}, // enable property key formatting for next
			BinaryCode::CHILD_GET_REF => {last_was_property_access = true}, // enable property key formatting for next
			BinaryCode::CHILD_ACTION => {last_was_property_access = true}, // enable property key formatting for next
			BinaryCode::CHILD_SET => {last_was_property_access = true}, // enable property key formatting for next
			BinaryCode::CHILD_SET_REFERENCE => {last_was_property_access = true}, // enable property key formatting for next
			
			BinaryCode::CONJUNCTION => {last_was_value = false}, // no space afterwards
			BinaryCode::DISJUNCTION => {last_was_value = false}, // no space afterwards

			_ => ()
		}
		


		// insert label
		for label in &mut state.labels {
			// only add if at right index and not yet inserted
			if *label.0 == state.index.get() && !state.inserted_labels.contains(label.0) {
				if state.colorized {out += &Color::RESERVED.as_ansi_rgb();}
				out += "\r\nlbl ";
				out += &label.1;
				if state.colorized {out += &Color::DEFAULT.as_ansi_rgb();}
				out += ";";
				state.inserted_labels.insert(*label.0);
			}
		}

		// TODO: improve this, last_was_value and stack behaviour is not correct all the time.
		// This tries to reconstruct the runtime behaviour of inserting values to the stack, which fails e.g for function calls and many other usecases that are not
		// handled in the decompiler - only permanent 100% fix would be to evaluate the conjunction/disjunction in the runtime and stringify the resulting value, but this
		// is a big overhead for the decompiler and also might create unintended sideffects...

		// update connective_size and add &/| syntax
		while last_was_value && connective_size_stack.len()!=0 {
			let len = connective_size_stack.len()-1;
			connective_size_stack[len] -= 1;

			if state.colorized  {out += &Color::DEFAULT.as_ansi_rgb()};

			// connective_size_stack finished
			if connective_size_stack[len] == 0 {
				connective_size_stack.pop();
				connective_type_stack.pop();
				out += ")";
				// possible new loop iteration for next element in stack
			}
			// add new connective element
			else {
				out += if connective_type_stack[connective_type_stack.len()-1] == BinaryCode::CONJUNCTION {" &"} else {" |"};
				break; // no further iteration, still in same stack
			}
		}
	
	
	}

	if state.colorized {out += AnsiCodes::RESET};

	return out;
}