Word Unperfect
public
Read
Owner: themaster
Branch: main
Commits: 0
Git CLI clone URL
git clone https://www.xt-emporium.com/git/word-unperfect.git
Fullscreen desktop URL
Code
Commits
History
Branches
Bug Reports
Discussions
Compare
Settings
word-unperfect
/
rev
/
wp_layout_flow.c
File editor
#include "wp_layout_flow.h" #include "wp_control_codes.h" #include "wp_layout_metrics.h" #include "wp_layout_status.h" #include "wp_parser_gate.h" #include "wp_record_stream.h" #include "wp_span_metrics.h" #include <string.h> extern void __cdecl16near layout_update_variant_anchor_state(WpLayoutGlobals *wl); static uint16_t wp_flow_u16(uint value) { return (uint16_t)(value & 0xffffU); } static uint16_t wp_flow_record_metric(const WpLayoutGlobals *wl, const WpRecord *rec) { if (rec == NULL) { return 1U; } if (rec->code == 0x20U && wl != NULL && wl->span_metric_delta_4bd8 > 0) { return (uint16_t)wl->span_metric_delta_4bd8; } if (rec->code >= 0x21U && rec->code < 0x80U && wl != NULL && wl->span_metric_delta_4bda > 0) { return (uint16_t)wl->span_metric_delta_4bda; } if (rec->code == 0xC0U) { return 1U; } return 1U; } static void wp_flow_count_record_status(const WpRecord *rec, WpLayoutFlowStats *stats) { if (rec == NULL || stats == NULL) { return; } if (!rec->is_complete && !rec->trailer_present) { stats->incomplete_records++; } if (rec->trailer_present && !rec->trailer_matches) { stats->mismatched_trailers++; } } void wp_layout_flow_stats_clear(WpLayoutFlowStats *stats) { if (stats != NULL) { memset(stats, 0, sizeof(*stats)); } } bool wp_layout_flow_variant_balance_check(WpLayoutGlobals *wl, const WpRecord *rec, WpLayoutFlowVariantCheckResult *out_result) { WpLayoutFlowVariantCheckResult result; if (out_result != NULL) { memset(out_result, 0, sizeof(*out_result)); } if (wl == NULL || rec == NULL || out_result == NULL) { return false; } memset(&result, 0, sizeof(result)); result.token = rec->code; result.record_length = rec->length; result.secondary_used_before = wl->secondary_record_used_bytes; /* layout_variant_balance_check() consumes one input byte and immediately * prepends it back into the replay stream. The net byte stream is * unchanged, but the path is important because C0+ packet tokens normalize * through the fixed/variable byte-count logic before the secondary-space * probe. */ result.token_consumed_and_prepended = true; result.required_secondary_bytes = rec->length == 0U ? 1U : rec->length; if (rec->code > 0xBFU) { result.packet_token = true; if (rec->code > 0xCFU) { result.variable_packet_token = true; result.d0_plus_secondary_space_check = true; result.secondary_probe_would_run = true; } else { result.fixed_packet_token = true; result.normalized_fixed_packet = true; } } if (wl->secondary_record_used_bytes < (int)result.required_secondary_bytes) { result.secondary_probe_would_run = true; } result.secondary_used_after = wl->secondary_record_used_bytes; *out_result = result; return true; } bool wp_layout_flow_emit_measure_iteration(WpLayoutGlobals *wl, const WpRecord *rec, WpLayoutFlowEmitMeasureResult *out_result) { WpLayoutFlowEmitMeasureResult result; uint16_t metric; uint16_t cursor_after; if (out_result != NULL) { memset(out_result, 0, sizeof(*out_result)); } if (wl == NULL || rec == NULL || out_result == NULL) { return false; } memset(&result, 0, sizeof(result)); result.token = rec->code; result.metric = metric = wp_flow_record_metric(wl, rec); result.old_cursor = (uint16_t)wl->cursor; result.old_measure_bound = (uint16_t)wl->measure_extent_bound_50c6; result.old_measure_extent = (uint16_t)wl->measure_extent_50c8; result.old_line_extent = (uint16_t)wl->emit_record_line_extent; result.old_emit_flags = wl->emit_flags; result.old_parse_dirty_flags = wl->parse_dirty_flags; result.old_status_extend = wl->status_extend_4976; wl->cursor = wp_flow_u16(wl->cursor + metric); wl->record_aux_4b34 += 1; wl->span_aux_word_53f6 = wp_flow_u16(wl->span_aux_word_53f6 + 1U); result.incremented_cursor = true; cursor_after = (uint16_t)wl->cursor; if (cursor_after < (uint16_t)wl->measure_extent_bound_50c6) { wl->processed_segments += 1; result.below_measure_bound_return = true; } else if ((wl->emit_flags & 0x02U) == 0U) { if (cursor_after <= (uint16_t)wl->emit_record_line_extent) { wl->emit_flags |= 0x01U; wl->processed_segments += 1; result.cursor_within_line_extent = true; } else if ((uint16_t)wl->measure_extent_50c8 < cursor_after) { result.extent_overflow = true; if ((wl->heap_control_flags_4bf1 & 0x04U) == 0U) { result.line_window_prepare_would_run = true; wl->emit_flags |= 0x04U; } else { result.heap_escape_would_run = true; if (wl->cursor >= metric) { wl->cursor = wp_flow_u16(wl->cursor - metric); } if (wl->record_aux_4b34 > 0) { wl->record_aux_4b34 -= 1; } } } else if (wl->postprocess_mode >= 1) { WpLayoutReconcileResult reconcile; result.postprocess_reconcile_would_run = true; (void)wp_layout_reconcile_line_build_span_words(wl, metric, &reconcile); wl->emit_flags |= 0x02U; wl->processed_segments += 1; } else { wl->emit_flags |= 0x02U; wl->processed_segments += 1; } } else if ((wl->heap_control_flags_4bf1 & 0x04U) != 0U) { result.heap_escape_would_run = true; } else { wl->parse_dirty_flags |= 0x02U; wl->status_extend_4976 = 0x04U; result.parse_dirty_fallback = true; } result.new_cursor = (uint16_t)wl->cursor; result.new_emit_flags = wl->emit_flags; result.new_parse_dirty_flags = wl->parse_dirty_flags; result.new_status_extend = wl->status_extend_4976; *out_result = result; return true; } bool wp_layout_flow_aux_word_increment(WpLayoutGlobals *wl, uint8_t in_al, WpLayoutFlowAuxResult *out_result) { WpLayoutFlowAuxResult result; if (out_result != NULL) { memset(out_result, 0, sizeof(*out_result)); } if (wl == NULL || out_result == NULL) { return false; } memset(&result, 0, sizeof(result)); result.input_al = in_al; result.old_cursor = (uint16_t)wl->cursor; result.old_span_aux = (uint16_t)wl->span_aux_word_53f6; result.old_record_aux = wl->record_aux_4b34; if (in_al != 0U) { wl->cursor = wp_flow_u16(wl->cursor + 2U); wl->record_aux_4b34 += 2; wl->span_aux_word_53f6 = wp_flow_u16(wl->span_aux_word_53f6 + 2U); result.double_increment = true; } else { wl->parser_runtime_flags |= 0x08U; result.command_parser_fallback = true; } result.new_cursor = (uint16_t)wl->cursor; result.new_span_aux = (uint16_t)wl->span_aux_word_53f6; result.new_record_aux = wl->record_aux_4b34; *out_result = result; return true; } bool wp_layout_flow_record_flags_merge(WpLayoutGlobals *wl, uint16_t input_word, WpLayoutFlowRecordFlagsResult *out_result) { WpLayoutFlowRecordFlagsResult result; if (out_result != NULL) { memset(out_result, 0, sizeof(*out_result)); } if (wl == NULL || out_result == NULL) { return false; } memset(&result, 0, sizeof(result)); result.input_word = input_word; result.checkpoint_word = (uint16_t)wl->span_compare_word_51fb; result.old_record_mode_flags = wl->record_mode_flags; result.old_status_dirty_flags = wl->status_dirty_flags; result.consumed_and_prepended_word = true; result.checkpoint_matched = input_word == result.checkpoint_word || input_word == (uint16_t)wl->span_compare_word_51ff; if (result.checkpoint_matched) { wl->status_dirty_flags |= 0x02U; } else { result.parser_iteration_would_run = true; } wl->record_mode_flags = (uint8_t)(wl->record_mode_flags | (uint8_t)input_word); result.new_record_mode_flags = wl->record_mode_flags; result.new_status_dirty_flags = wl->status_dirty_flags; *out_result = result; return true; } bool wp_layout_flow_stream_offset_bump(WpLayoutGlobals *wl, uint16_t input_ax, uint8_t loop_result_code, WpLayoutFlowStreamOffsetResult *out_result) { WpLayoutFlowStreamOffsetResult result; bool checkpoint_equal; if (out_result != NULL) { memset(out_result, 0, sizeof(*out_result)); } if (wl == NULL || out_result == NULL) { return false; } memset(&result, 0, sizeof(result)); result.input_ax = input_ax; result.checkpoint_word = (uint16_t)wl->record_offset_checkpoint_4b4c; result.loop_result_code = loop_result_code; result.old_postprocess_mode = (uint8_t)wl->postprocess_mode; result.old_variant_anchor = (uint16_t)wl->variant_anchor; result.old_variant_total = (int16_t)(uint16_t)wl->variant_total; if (((uint8_t)wl->postprocess_mode & 0x28U) != 0U) { result.postprocess_bits_blocked = true; } else { checkpoint_equal = (input_ax == (uint16_t)wl->record_offset_checkpoint_4b4c); result.checkpoint_equal = checkpoint_equal; if (!checkpoint_equal) { result.loop_iteration_would_run = true; if (loop_result_code == 0x29U) { result.loop_code_allowed_update = true; layout_update_variant_anchor_state(wl); result.variant_anchor_updated = true; } } else if (wl->stream_update_gate_5205 == 0) { result.update_gate_blocked_return = true; } else { layout_update_variant_anchor_state(wl); result.variant_anchor_updated = true; } } result.new_variant_anchor = (uint16_t)wl->variant_anchor; result.new_variant_total = (int16_t)(uint16_t)wl->variant_total; *out_result = result; return true; } bool wp_layout_flow_apply_variant_carry_flow(WpLayoutGlobals *wl, uint8_t pass_count, uint8_t dh_subcommand, bool loop_zero_gate_passed, WpLayoutFlowCarryResult *out_result) { WpLayoutFlowCarryResult result; WpParserGateResult gate_result; WpParserGateResult free_result; WpParserGateResult mode_result; uint8_t saved_runtime; uint16_t threshold_base; uint16_t remaining; if (out_result != NULL) { memset(out_result, 0, sizeof(*out_result)); } if (wl == NULL || out_result == NULL) { return false; } memset(&result, 0, sizeof(result)); result.pass_count = pass_count; result.dh_subcommand = dh_subcommand; result.loop_zero_gate_passed = loop_zero_gate_passed; result.parser_runtime_before = wl->parser_runtime_flags; result.status_dirty_before = wl->status_dirty_flags; result.carry_width = (uint16_t)wl->carry_width; if (!loop_zero_gate_passed) { result.parser_runtime_after = wl->parser_runtime_flags; result.status_dirty_after = wl->status_dirty_flags; *out_result = result; return true; } saved_runtime = wl->parser_runtime_flags; wl->parser_runtime_flags |= 0x10U; result.gate_mask = wp_parser_gate_allocate_mask_bit(wl, &gate_result); result.gate_allocated = result.gate_mask != 0U; result.parse_until_gate_would_run = true; result.aligned_start = wp_layout_line_start_align(wl); threshold_base = result.aligned_start; if (wl->variant_count != 0) { threshold_base = (uint16_t)wl->variant_limit; result.use_variant_limit = true; } result.threshold = (uint16_t)(threshold_base + 1U); remaining = pass_count == 0U ? 1U : pass_count; if (remaining == 2U || result.threshold <= (uint16_t)wl->carry_width) { result.threshold_allows_parse_fields = true; do { result.parse_field_passes++; remaining--; } while (remaining != 0U); if (dh_subcommand == 0x06U && wl->feature_enabled != 0 && result.threshold <= (uint16_t)wl->carry_width) { result.parse_field_passes++; result.feature_extra_pass = true; } } wl->status_dirty_flags |= 0x48U; (void)wp_parser_gate_set_mode_or_status_bits(wl, &mode_result); result.mode_status_update_would_run = true; wl->state_flags = 1U; result.state_flags_after = wl->state_flags; if (result.gate_mask != 0U) { result.gate_freed = wp_parser_gate_free_mask_bit(wl, result.gate_mask, &free_result); } wl->parser_runtime_flags = saved_runtime; if (dh_subcommand != 0U && pass_count > 0U) { wl->stream_resume_token = 1U; result.stream_resume_set = true; } result.parser_runtime_after = wl->parser_runtime_flags; result.status_dirty_after = wl->status_dirty_flags; result.stream_resume_token_after = (uint8_t)wl->stream_resume_token; *out_result = result; return true; } bool wp_layout_flow_apply_record(WpLayoutGlobals *runtime, const WpRecord *rec, WpLayoutFlowStats *stats) { WpLayoutFlowVariantCheckResult variant; WpLayoutFlowEmitMeasureResult emit; WpLayoutFlowAuxResult aux; WpLayoutFlowRecordFlagsResult flags; WpLayoutFlowStreamOffsetResult stream; WpLayoutFlowCarryResult carry; uint16_t token_word; if (runtime == NULL || rec == NULL || stats == NULL) { return false; } stats->records_seen++; stats->bytes_consumed += rec->length; wp_flow_count_record_status(rec, stats); token_word = rec->type == WP_CODE_FIXED_LENGTH && rec->data_length >= 2U && rec->data != NULL ? (uint16_t)(rec->data[0] | ((uint16_t)rec->data[1] << 8U)) : rec->code; if (rec->code >= 0xC0U) { if (wp_layout_flow_variant_balance_check(runtime, rec, &variant)) { stats->variant_balance_checks++; if (variant.packet_token) stats->variant_packet_tokens++; if (variant.fixed_packet_token) stats->variant_fixed_packets++; if (variant.variable_packet_token) stats->variant_variable_packets++; if (variant.normalized_fixed_packet) stats->normalized_fixed_packets++; if (variant.secondary_probe_would_run) stats->secondary_probe_hints++; } } if (rec->code < 0x20U) { if (wp_layout_flow_aux_word_increment(runtime, rec->code, &aux)) { if (aux.double_increment) stats->aux_double_increments++; if (aux.command_parser_fallback) stats->aux_command_fallbacks++; } } else if (rec->code < 0x80U || rec->code == 0xC0U) { if (wp_layout_flow_emit_measure_iteration(runtime, rec, &emit)) { stats->emit_measure_iterations++; if (emit.below_measure_bound_return) stats->emit_measure_below_bound++; if (emit.cursor_within_line_extent) stats->emit_measure_line_extent_hits++; if (emit.line_window_prepare_would_run) stats->emit_measure_window_prepares++; if (emit.heap_escape_would_run) stats->emit_measure_heap_escapes++; if (emit.parse_dirty_fallback) stats->emit_measure_parse_dirty_fallbacks++; } } else if (rec->code >= 0x80U && rec->code < 0xC0U) { if (wp_layout_flow_record_flags_merge(runtime, token_word, &flags)) { stats->record_flags_merges++; if (flags.checkpoint_matched) stats->record_flags_checkpoint_hits++; } } if (runtime->postprocess_mode != 0 || rec->code == 0x20U || rec->code == 0xD4U) { if (wp_layout_flow_stream_offset_bump(runtime, token_word, 0x29U, &stream)) { stats->stream_offset_bumps++; if (stream.variant_anchor_updated) stats->stream_offset_anchor_updates++; if (stream.postprocess_bits_blocked) stats->stream_offset_postprocess_blocks++; } } if (rec->code == 0xD4U && rec->sub_code == 0x04U) { if (wp_layout_flow_apply_variant_carry_flow(runtime, 2U, rec->sub_code, true, &carry)) { stats->carry_flow_calls++; if (carry.gate_allocated) stats->carry_flow_gate_allocs++; stats->carry_flow_parse_field_passes += carry.parse_field_passes; if (carry.feature_extra_pass) stats->carry_flow_feature_extra_passes++; if (carry.stream_resume_set) stats->carry_flow_stream_resume_sets++; } } return true; } bool wp_layout_flow_run_stream(WpLayoutGlobals *wl, WpLayoutFlowStats *stats) { WpLayoutGlobals cursor; WpLayoutGlobals runtime; WpLayoutFlowStats local_stats; bool ok = true; if (wl == NULL || stats == NULL) { return false; } wp_layout_flow_stats_clear(&local_stats); cursor = *wl; runtime = *wl; runtime.measure_extent_bound_50c6 = runtime.measure_extent_bound_50c6 == 0U ? 4096U : runtime.measure_extent_bound_50c6; runtime.emit_record_line_extent = runtime.emit_record_line_extent == 0U ? 8192U : runtime.emit_record_line_extent; runtime.measure_extent_50c8 = runtime.measure_extent_50c8 == 0U ? runtime.emit_record_line_extent : runtime.measure_extent_50c8; runtime.span_metric_delta_4bda = runtime.span_metric_delta_4bda == 0 ? 1 : runtime.span_metric_delta_4bda; runtime.span_metric_delta_4bd8 = runtime.span_metric_delta_4bd8 == 0 ? 1 : runtime.span_metric_delta_4bd8; runtime.record_offset_checkpoint_4b4c = runtime.record_offset_checkpoint_4b4c == 0U ? 0x20U : runtime.record_offset_checkpoint_4b4c; while (cursor.record_used_bytes > 0) { WpRecord rec; int before = cursor.record_used_bytes; wp_parser_consume_record(&cursor, &rec); if (rec.length == 0U || cursor.record_used_bytes >= before) { wp_record_free(&rec); ok = false; break; } if (!wp_layout_flow_apply_record(&runtime, &rec, &local_stats)) { ok = false; } wp_record_free(&rec); } local_stats.final_emit_flags = runtime.emit_flags; local_stats.final_parse_dirty_flags = runtime.parse_dirty_flags; local_stats.final_status_dirty_flags = runtime.status_dirty_flags; local_stats.final_status_extend = runtime.status_extend_4976; local_stats.final_parser_runtime_flags = runtime.parser_runtime_flags; local_stats.final_variant_anchor = (uint16_t)runtime.variant_anchor; local_stats.final_variant_total = (int16_t)(uint16_t)runtime.variant_total; local_stats.final_cursor = runtime.cursor; local_stats.final_record_aux = runtime.record_aux_4b34; local_stats.final_span_aux = (uint16_t)runtime.span_aux_word_53f6; *stats = local_stats; return ok && local_stats.incomplete_records == 0U && local_stats.mismatched_trailers == 0U; } bool wp_layout_flow_run_loaded_file(WpLoadedFile *file, WpLayoutFlowStats *stats) { WpLayoutGlobals wl; if (file == NULL || stats == NULL) { return false; } memset(&wl, 0, sizeof(wl)); if (!wp_file_bind_primary_stream(file, &wl, 4096U)) { return false; } return wp_layout_flow_run_stream(&wl, stats); } bool wp_layout_flow_run_file(const char *filename, WpLayoutFlowStats *stats) { WpLoadedFile file; bool ok; if (filename == NULL || stats == NULL) { return false; } if (!wp_file_load_body(filename, &file)) { return false; } ok = wp_layout_flow_run_loaded_file(&file, stats); wp_file_free(&file); return ok; } /* Raw wrappers for monolith compatibility */ uint8_t __cdecl16near layout_variant_balance_check(WpLayoutGlobals *wl) { WpLayoutFlowVariantCheckResult result; /* Wrapper needs to pass current record if available, or NULL to signal auto-consume */ (void)wp_layout_flow_variant_balance_check(wl, NULL, &result); return (uint8_t)(result.secondary_probe_would_run ? 1 : 0); } uint16_t __cdecl16near layout_emit_measure_line_iteration(WpLayoutGlobals *wl) { WpLayoutFlowEmitMeasureResult result; (void)wp_layout_flow_emit_measure_iteration(wl, NULL, &result); return (uint16_t)result.metric; } void __cdecl16near layout_aux_word_increment(WpLayoutGlobals *wl, uint8_t in_al) { WpLayoutFlowAuxResult result; (void)wp_layout_flow_aux_word_increment(wl, in_al, &result); } uint16_t __cdecl16near layout_record_flags_merge(WpLayoutGlobals *wl) { WpLayoutFlowRecordFlagsResult result; (void)wp_layout_flow_record_flags_merge(wl, 0, &result); return result.checkpoint_matched ? 1 : 0; } uint16_t __cdecl16near layout_stream_offset_bump(WpLayoutGlobals *wl, uint16_t in_ax) { WpLayoutFlowStreamOffsetResult result; (void)wp_layout_flow_stream_offset_bump(wl, in_ax, 0, &result); return (uint16_t)result.input_ax; } void __cdecl16near layout_apply_variant_carry_flow(WpLayoutGlobals *wl, uint8_t pass_count, uint8_t dh_subcommand) { WpLayoutFlowCarryResult result; (void)wp_layout_flow_apply_variant_carry_flow(wl, pass_count, dh_subcommand, false, &result); } uint16_t __cdecl16near layout_decrement_cursor_and_span_aux_route_stage_1(WpLayoutGlobals *wl) { int step = layout_get_active_span_total_width(wl); wl->record_aux_4b34 -= 1; wl->cursor -= step; wl->span_aux_word_53f6 = (uint16_t)(wl->span_aux_word_53f6 - 1); return (uint16_t)wl->cursor; } uint16_t __cdecl16near layout_decrement_cursor_and_span_aux_route_stage_2(WpLayoutGlobals *wl) { return layout_decrement_cursor_and_span_aux_route_stage_1(wl); } uint16_t __cdecl16near layout_increment_cursor_and_span_aux_route_stage_1(WpLayoutGlobals *wl) { int step = layout_get_active_span_total_width(wl); wl->record_aux_4b34 += 1; wl->cursor = (uint32_t)wl->cursor + (uint32_t)step; wl->span_aux_word_53f6 = (uint16_t)(wl->span_aux_word_53f6 + 1); return (uint16_t)wl->cursor; } uint16_t __cdecl16near layout_increment_cursor_and_span_aux_route_stage_2(WpLayoutGlobals *wl) { return layout_increment_cursor_and_span_aux_route_stage_1(wl); } void __cdecl16near layout_sub_ax_from_variant_anchor_and_total(WpLayoutGlobals *wl) { int sub = (int)wl->cursor; int vt0 = wl->variant_total; int va = (int)(int16_t)wl->variant_anchor; va -= sub; wl->variant_anchor = (undefined2)va; wl->variant_total -= sub; if (vt0 <= wl->variant_total) { wl->parse_dirty_flags |= 8; } } uint16_t __cdecl16near layout_sub_metrics_line_accum_from_variant_anchor_and_total(WpLayoutGlobals *wl) { int m = wl->metrics_line_accum_5037; int vt0 = wl->variant_total; int va = (int)(int16_t)wl->variant_anchor; va -= m; wl->variant_anchor = (undefined2)va; wl->variant_total -= m; if (vt0 <= wl->variant_total) { wl->parse_dirty_flags |= 8; } return 0; } void __cdecl16near layout_heap_control_branch(WpLayoutGlobals *wl) { if ((wl->heap_control_flags_4bf1 & 1) != 0) { wl->render_dirty_flags |= 8; } } void __cdecl16near layout_update_variant_anchor_state(WpLayoutGlobals *wl) { wl->variant_anchor = (uint16_t)wl->carry_width; wl->variant_total = wl->carry_width; wl->status_dirty_flags |= 0x48; } void __cdecl16near layout_reset_cursor_and_record_aux(WpLayoutGlobals *wl) { uint16_t cursor = layout_reset_cursor_aliases_variant_table(wl); wl->cursor = cursor; wl->measured_span_cursor = cursor; wl->cursor_alias_shadow_4b3e = cursor; wl->processed_segments = 0; wl->record_aux_4b34 = wl->carry_width / 2; }
Commit message
This repository is read-only for this account.
Repository snapshot
Current branch
main
Visibility
public
Your access
Read
Remote
None
File activity
View file history