Changeset View
Changeset View
Standalone View
Standalone View
head/sys/dev/bhnd/tools/nvram_map_gen.awk
Show First 20 Lines • Show All 1,513 Lines • ▼ Show 20 Lines | for (_i = 0; _i < _nall_entries; _i++) { | ||||
# Sanity check variable ID assignment | # Sanity check variable ID assignment | ||||
if (get(_var, p_vid) == "") | if (get(_var, p_vid) == "") | ||||
errorx("missing variable ID for " obj_to_string(_var)) | errorx("missing variable ID for " obj_to_string(_var)) | ||||
array_append(_entries, _entry) | array_append(_entries, _entry) | ||||
} | } | ||||
# Sort entries by variable ID, ascending | # Sort entries by (variable ID, revision range), ascending | ||||
array_sort(_entries, prop_path_create(p_var, p_vid)) | array_sort(_entries, prop_path_create(p_var, p_vid), | ||||
prop_path_create(p_revisions, p_start), | |||||
prop_path_create(p_revisions, p_end)) | |||||
# Emit all entry binding opcodes | # Emit all entry binding opcodes | ||||
emit("static const uint8_t " _varname "[] = {\n") | emit("static const uint8_t " _varname "[] = {\n") | ||||
output_depth++ | output_depth++ | ||||
_nentries = array_size(_entries) | _nentries = array_size(_entries) | ||||
for (_i = 0; _i < _nentries; _i++) { | for (_i = 0; _i < _nentries; _i++) { | ||||
_entry = array_get(_entries, _i) | _entry = array_get(_entries, _i) | ||||
▲ Show 20 Lines • Show All 760 Lines • ▼ Show 20 Lines | function array_get(array, idx) { | ||||
return (_g_arrays[array,OBJ_PROP,idx]) | return (_g_arrays[array,OBJ_PROP,idx]) | ||||
} | } | ||||
# | # | ||||
# Sort an array, using standard awk comparison operators over its values. | # Sort an array, using standard awk comparison operators over its values. | ||||
# | # | ||||
# If `prop_path` is non-NULL, the corresponding property path (or property ID) | # If `prop_path*` is non-NULL, the corresponding property path (or property ID) | ||||
# will be fetched from each array element and used as the sorting value. | # will be fetched from each array element and used as the sorting value. | ||||
# | # | ||||
function array_sort(array, prop_path, _size) { | # If multiple property paths are specified, the array is first sorted by | ||||
# the first path, and then any equal values are sorted by the second path, | |||||
# and so on. | |||||
# | |||||
function array_sort(array, prop_path0, prop_path1, prop_path2, _size) { | |||||
obj_assert_class(array, Array) | obj_assert_class(array, Array) | ||||
if (_size != null) | |||||
errorx("no more than three property paths may be specified") | |||||
_size = array_size(array) | _size = array_size(array) | ||||
if (_size <= 1) | if (_size <= 1) | ||||
return | return | ||||
_qsort(array, prop_path, 0, _size-1) | _qsort(array, prop_path0, prop_path1, prop_path2, 0, _size-1) | ||||
} | } | ||||
function _qsort_get_key(array, idx, prop_path, _v) { | function _qsort_get_key(array, idx, prop_path, _v) { | ||||
_v = array_get(array, idx) | _v = array_get(array, idx) | ||||
if (prop_path == null) | if (prop_path == null) | ||||
return (_v) | return (_v) | ||||
return (prop_get_path(_v, prop_path)) | return (prop_get_path(_v, prop_path)) | ||||
} | } | ||||
function _qsort(array, prop_path, first, last, _qpivot, _qpivot_val, _qleft, | function _qsort_compare(array, lhs_idx, rhs_val, ppath0, ppath1, ppath2, | ||||
_qleft_val, _qright, _qright_val) | _lhs_val, _rhs_prop_val) | ||||
{ | { | ||||
_lhs_val = _qsort_get_key(array, lhs_idx, ppath0) | |||||
if (ppath0 == null) | |||||
_rhs_prop_val = rhs_val | |||||
else | |||||
_rhs_prop_val = prop_get_path(rhs_val, ppath0) | |||||
if (_lhs_val == _rhs_prop_val && ppath1 != null) { | |||||
_lhs_val = _qsort_get_key(array, lhs_idx, ppath1) | |||||
_rhs_prop_val = prop_get_path(rhs_val, ppath1) | |||||
if (_lhs_val == _rhs_prop_val && ppath2 != null) { | |||||
_lhs_val = _qsort_get_key(array, lhs_idx, ppath2) | |||||
_rhs_prop_val = prop_get_path(rhs_val, ppath2) | |||||
} | |||||
} | |||||
if (_lhs_val < _rhs_prop_val) | |||||
return (-1) | |||||
else if (_lhs_val > _rhs_prop_val) | |||||
return (1) | |||||
else | |||||
return (0) | |||||
} | |||||
function _qsort(array, ppath0, ppath1, ppath2, first, last, _qpivot, | |||||
_qleft, _qleft_val, _qright, _qright_val) | |||||
{ | |||||
if (first >= last) | if (first >= last) | ||||
return | return | ||||
# select pivot element | # select pivot element | ||||
_qpivot = int(first + int((last-first+1) * rand())) | _qpivot = int(first + int((last-first+1) * rand())) | ||||
_qleft = first | _qleft = first | ||||
_qright = last | _qright = last | ||||
_qpivot_val = _qsort_get_key(array, _qpivot, prop_path) | _qpivot_val = array_get(array, _qpivot) | ||||
# partition | # partition | ||||
while (_qleft <= _qright) { | while (_qleft <= _qright) { | ||||
while (_qsort_get_key(array, _qleft, prop_path) < _qpivot_val) | while (_qsort_compare(array, _qleft, _qpivot_val, ppath0, ppath1, | ||||
ppath2) < 0) | |||||
{ | |||||
_qleft++ | _qleft++ | ||||
} | |||||
while (_qsort_get_key(array, _qright, prop_path) > _qpivot_val) | while (_qsort_compare(array, _qright, _qpivot_val, ppath0, ppath1, | ||||
ppath2) > 0) | |||||
{ | |||||
_qright-- | _qright-- | ||||
} | |||||
# swap | # swap | ||||
if (_qleft <= _qright) { | if (_qleft <= _qright) { | ||||
_qleft_val = array_get(array, _qleft) | _qleft_val = array_get(array, _qleft) | ||||
_qright_val = array_get(array, _qright) | _qright_val = array_get(array, _qright) | ||||
array_set(array, _qleft, _qright_val) | array_set(array, _qleft, _qright_val) | ||||
array_set(array, _qright, _qleft_val) | array_set(array, _qright, _qleft_val) | ||||
_qleft++ | _qleft++ | ||||
_qright-- | _qright-- | ||||
} | } | ||||
} | } | ||||
# sort the partitions | # sort the partitions | ||||
_qsort(array, prop_path, first, _qright) | _qsort(array, ppath0, ppath1, ppath2, first, _qright) | ||||
_qsort(array, prop_path, _qleft, last) | _qsort(array, ppath0, ppath1, ppath2, _qleft, last) | ||||
} | } | ||||
# | # | ||||
# Join all array values with the given separator | # Join all array values with the given separator | ||||
# | # | ||||
# If `prop_path` is non-NULL, the corresponding property path (or property ID) | # If `prop_path` is non-NULL, the corresponding property path (or property ID) | ||||
# will be fetched from each array value and included in the result, rather than | # will be fetched from each array value and included in the result, rather than | ||||
▲ Show 20 Lines • Show All 1,856 Lines • Show Last 20 Lines |