From 6a404b822799372dbabd3447aa11622a228f0a96 Mon Sep 17 00:00:00 2001 From: IntoTheNight Date: Thu, 13 Jul 2023 19:50:44 +0530 Subject: [PATCH] another shrimple macro! (NOTE: I was unable to test on my Nix system for some reason, QEMU just refuses to work even though I ran it within the Nix Shell environment) --- kernel/src/arch/x86_64/cpuid.rs | 21 ++++----------------- kernel/src/utils.rs | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/kernel/src/arch/x86_64/cpuid.rs b/kernel/src/arch/x86_64/cpuid.rs index e0ed015f..85b8640a 100644 --- a/kernel/src/arch/x86_64/cpuid.rs +++ b/kernel/src/arch/x86_64/cpuid.rs @@ -1,3 +1,5 @@ +use crate::cpu_features; + use { alloc::vec::Vec, core::{arch::asm, fmt, ops::Deref, slice, str}, @@ -772,23 +774,8 @@ impl Master { // TODO: Macroify this and also include all of the cpu features from self pub fn features(&self) -> Vec<(&str, bool)> { let mut fv = Vec::new(); - let apic = self.apic(); - let avx = self.avx(); - let avx2 = self.avx2(); - let x2 = self.x2apic(); - - let gb_pages = self.gigabyte_pages(); - let rdseed = self.rdseed(); - let rdrand = self.rdrand(); - - fv.push(("apic", apic)); - fv.push(("avx", avx)); - fv.push(("avx2", avx2)); - fv.push(("gigabyte pages", gb_pages)); - fv.push(("rdrand", rdrand)); - fv.push(("rdseed", rdseed)); - fv.push(("x2apic", x2)); - fv + cpu_features!(self, fv); + return fv; } master_attr_reader!(version_information, VersionInformation); diff --git a/kernel/src/utils.rs b/kernel/src/utils.rs index f733eec8..ce78fbce 100644 --- a/kernel/src/utils.rs +++ b/kernel/src/utils.rs @@ -25,4 +25,29 @@ macro_rules! device_tree { $devtree.devices.insert(each_device_type.to_string(), Vec::new()); } }; +} + +// NOTE: this only reduces the code duplication in source code not in generated code! +// Written by Yours Truly: Munir +/// A simple macro to reduce code duplication when we insert cpu features into the array/vector +#[macro_export] +macro_rules! cpu_features { + ($cpu_master_object:expr, $result_vec:expr) => { + // checks for cpu features and returns bool for each feature + let apic = $cpu_master_object.apic(); + let avx = $cpu_master_object.avx(); + let avx2 = $cpu_master_object.avx2(); + let x2 = $cpu_master_object.x2apic(); + let gb_pages = $cpu_master_object.gigabyte_pages(); + let rdseed = $cpu_master_object.rdseed(); + let rdrand = $cpu_master_object.rdrand(); + + $result_vec.push(("apic", apic)); + $result_vec.push(("avx", avx)); + $result_vec.push(("avx2", avx2)); + $result_vec.push(("gigabyte pages", gb_pages)); + $result_vec.push(("rdrand", rdrand)); + $result_vec.push(("rdseed", rdseed)); + $result_vec.push(("x2apic", x2)); + }; } \ No newline at end of file